logctx
Version:
A Pino-based logger with context-aware logging using async_hooks
62 lines (61 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextualLogger = void 0;
// src/ContextualLogger.ts
const Context_1 = require("./Context");
const Logger_1 = require("./Logger");
/**
* A class that provides a contextual logger.
* The logger can be used to log messages with additional context.
* @template T - The type of the context.
*/
class ContextualLogger {
get log() {
const ctx = (0, Context_1.getContext)();
return {
/**
* Logs a message at the TRACE level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
trace: (msg, meta) => Logger_1.baseLogger.trace({ ...ctx, ...meta }, msg),
/**
* Logs a message at the FATAL level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
fatal: (msg, meta) => Logger_1.baseLogger.fatal({ ...ctx, ...meta }, msg),
/**
* Logs a message at the INFO level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
info: (msg, meta) => Logger_1.baseLogger.info({ ...ctx, ...meta }, msg),
/**
* Logs a message at the ERROR level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
error: (msg, meta) => Logger_1.baseLogger.error({ ...ctx, ...meta }, msg),
/**
* Logs a message at the WARN level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
warn: (msg, meta) => Logger_1.baseLogger.warn({ ...ctx, ...meta }, msg),
/**
* Logs a message at the DEBUG level.
* @param msg The message to log.
* @param meta Optional metadata to include in the log.
* @returns The result of the logging operation.
*/
debug: (msg, meta) => Logger_1.baseLogger.debug({ ...ctx, ...meta }, msg),
};
}
}
exports.ContextualLogger = ContextualLogger;