import { globalLogger } from "@config/logger";
import { AsyncLocalStorage } from "node:async_hooks";
export const asyncLocalStorage = new AsyncLocalStorage();
/**
* Get the logger from the async local storage context
* @returns {Logger} Logger instance with request ID, or base logger if no context
*/
export function getLogger() {
const context = asyncLocalStorage.getStore();
if (!context) {
return globalLogger;
}
return context.logger;
}
/**
* Get the request ID from the async local storage context
* @returns {string | undefined} Request ID string or undefined if not in context
*/
export function getRequestId() {
const context = asyncLocalStorage.getStore();
return context?.requestId;
}
Source