Interface that allows you to log records. This records are treated by handlers

Constructors

Properties

displayName?: string
key: string

Methods

  • Create a new Logger with the same key a this attached context

    Parameters

    • context: Record<string, unknown>

    Returns Logger

    const loggerMyService = new Logger('app:myService');
    function someAction(arg1) {
    const logger = loggerMyService.context({ arg1 });
    logger.enter(someAction);
    // do stuff
    logger.info('info');
    // do stuff
    logger.exit(someAction);
    }
  • Log an enter in a function

    Type Parameters

    • T extends Metadata
    • Fn extends ((...args: unknown[]) => unknown)

    Parameters

    • fn: Fn
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<ExtendedFunctionNameMetadata & T>

    Returns void

    class A {
    method(arg1) {
    logger.enter(method, { arg1 });
    // Do your stuff
    }
    }
  • Log an error message

    Type Parameters

    • T extends Metadata

    Parameters

    • messageOrError: string | Error
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<T>

    Returns void

    const logger = new Logger('something');
    try {
    throw new Error('Always throws here');
    } catch (error) {
    logger.error('caught error', { error });
    }
  • Log an exit in a function

    Type Parameters

    • T extends Metadata
    • Fn extends ((...args: unknown[]) => unknown)

    Parameters

    • fn: Fn
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<ExtendedFunctionNameMetadata & T>

    Returns void

    const logger = new Logger('myNamespace:A');
    class A {
    method(arg1) {
    // Do your stuff
    logger.exit(method, { arg1 });
    }
    }
  • Log a fatal message

    Type Parameters

    • T extends Metadata

    Parameters

    • messageOrError: string | Error
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<T>

    Returns void

    const logger = new Logger('something');
    try {
    throw new Error('Always throws here');
    } catch (error) {
    logger.error('caught error', { error });
    process.exit(1);
  • Like timeEnd, but with INFO level

    Type Parameters

    • T extends Metadata

    Parameters

    • time: number
    • message: string
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<T>

    Returns void

  • Type Parameters

    • T extends Metadata

    Parameters

    • Optionalmessage: string
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<T>
    • Optionallevel: number

    Returns number

    time to pass to timeEnd

  • Finds difference between when this method was called and when the respective time method was called, then logs out the difference and deletes the original record

    Type Parameters

    • T extends Metadata

    Parameters

    • startTime: number
    • message: string
    • Optionalmetadata: T
    • OptionalmetadataStyles: MetadataStyles<T>
    • Optionallevel: number
    • Optionaloptions: Options<T>

    Returns void

  • Wrap around a function to log enter and exit of a function

    Type Parameters

    • Fn extends ((...args: unknown[]) => unknown)

    Parameters

    • fn: Fn
    • callback: (() => void)
        • (): void
        • Returns void

    Returns void

    const logger = new Logger('myNamespace:A');
    class A {
    method() {
    logger.wrap(method, () => {
    // Do your stuff
    });
    }
    }
  • Type Parameters

    • T extends Metadata
    • Fn extends ((...args: unknown[]) => unknown)

    Parameters

    • fn: Fn
    • metadata: T
    • callback: (() => void)
        • (): void
        • Returns void

    Returns void

  • Type Parameters

    • T extends Metadata
    • Fn extends ((...args: unknown[]) => unknown)

    Parameters

    • fn: Fn
    • metadata: T
    • metadataStyles: MetadataStyles<T>
    • callback: (() => void)
        • (): void
        • Returns void

    Returns void