• Prepends context to each log entry

    Returns

    A logger object that prepends the context when logging

    Example: single context, default formatting

    • const oneContextLogger = contextLogger('ctx1', rawLogger);
    • oneContextLogger.debug('hi') calls `rawLogger('[ctx1]', hi)

    Example: nested context, default formatting

    • const twoContextLogger = contextLogger('ctx2', oneContextLogger);
    • twoContextLogger.debug('hi') calls `rawLogger('[ctx1|ctx2]', hi)

    Example: nested context with custom formatter

    • const mergeLogger = contextLogger('ctx2', oneContextLogger, (ctx) => (${ctx.join('&')}) =>));
    • mergeLogger.debug('hi') calls `rawLogger('(ctx1&ctx2) =>', 'hi')

    Parameters

    • logger: Logger

      To which logger we are adding a context. Could be a simple logger or another context logger, in which case the new context is added along with the parent one.

    • context: string

      string that is prepended to the log messages. It is concatenated with the parent context if any.

    • contextStringify: ((ctx: string[]) => any) = ...

      Optional function to override how the context is stringified. It takes as parameter an array of contexts and returns a concatenated string. Default contextStringify example: ['ctx1', 'ctx'2] becomes "[ctx1|ctx2]".

        • (ctx: string[]): any
        • Parameters

          • ctx: string[]

          Returns any

    Returns Logger

Generated using TypeDoc