bun-logger - v1.1.0
    Preparing search index...

    Class Logger

    A custom Logger class that provides enhanced logging capabilities with log levels, scoping, and formatting.

    It wraps around the native console methods and adds features like colored output, timestamps, and grouping. The log level can be configured globally or per instance, allowing for flexible logging based on the environment or specific needs.

    getLogLevel for details on how log levels are determined and configured.

    Example usage:

    const logger = new Logger('MyApp', 'info');
    logger.debug('This is a debug message'); // Will not be logged due to log level
    logger.info('This is an info message'); // Will be logged with timestamp and scope
    logger.warn('This is a warning'); // Will be logged with timestamp and scope
    logger.error('This is an error'); // Will be logged with timestamp and scope
    Index

    Constructors

    • Create a new default logger instance with default log level

      Returns Logger

    • Creates a new logger instance with the specified scope.

      The scope is a string that will be prefixed to all log messages from this Logger instance, allowing for easier identification of the source of log messages The scope can also be set or changed later using the Logger.setScope method.

      Parameters

      • scope: string

        Sets the scope for this Logger instance, which will be prefixed to all log messages. If an empty string is provided, no scope will be used.

      Returns Logger

      This constructor cannot be used to name the scope with the same name as a log level (e.g. info, warn, error) to avoid confusion with log level configuration. If such a name is used, it will be treated as a log level configuration instead of a scope, and the scope will default to an empty string.

      Logger.setScope for details on how scope is determined and configured.

    • Creates a new logger instance with the specified log level.

      If a string is provided, it will be processed according to the rules defined in the getLogLevel function If a Partial<LogLevel> object is provided, it will override the corresponding log levels while keeping the others at their default values.

      Parameters

      • logLevel: LogLevelString | Partial<LogLevel>

        the log level to set for this Logger instance. This can be a string representing the desired log level or a Partial object to specify individual log levels. If a string is provided, it will be processed according to the rules defined in the getLogLevel function, which also considers environment variables for configuration. If a Partial object is provided, it will override the corresponding log levels while keeping the others at their default or previously set values.

      Returns Logger

      getLogLevel for details on how log levels are determined and configured.

    • Create a new logger instance with the specified scope and log level.

      The scope is a string that will be prefixed to all log messages from this Logger instance, allowing for easier identification of the source of log messages. The scope can also be set or changed later using the Logger.setScope method.

      If a string is provided, it will be processed according to the rules defined in the getLogLevel function If a Partial<LogLevel> object is provided, it will override the corresponding log levels while keeping the others at their default values.

      Parameters

      Returns Logger

      Logger.setScope for details on how scope is determined and configured.

    Methods

    • Assert the condition and log the data if assertion fails.

      If condition is true no message will be logged

      Log level: errors

      Parameters

      • condition: boolean

        The condition to assert

      • ...data: unknown[]

        The data to print if assertion fails

      Returns void

    • Clears the console

      Returns void

      This method will clear all logs regardless of the log level and scope

    • Increments the counter for the given label and logs it to the console.

      Log level: info

      Parameters

      • label: string = 'default'

        The label for counter, default is default

      Returns void

      The counter will not increment if log level is set to a level that does not include info (e.g. warn, error, none). If the counter for the given label does not exist, it will be initialized to 0 before being incremented. The log message will always automatically include the scope of the logger

    • Resets the counter for the given label.

      Log level: info

      Parameters

      • label: string = 'default'

        The label for the counter, default is default

      Returns void

      The counter will not reset if log level is set to a level that does not include info (e.g. warn, error, none). If the counter for the given label does not exist, this method will have no effect. The counter log will always automatically include the scope of the logger

    • Print a debug message

      Log level: verbose

      Parameters

      • ...data: unknown[]

        The data to print

      Returns void

    • This will print a given object using console.dir

      Prints JSON data of an object

      Log level: info

      Parameters

      • item: unknown

        The object to print

      • Optionaloptions: { colors?: boolean; depth?: boolean; showHidden?: boolean }

        Options to pass to console.dir

      Returns void

    • This will print a given object using console.dirxml

      Print XML data of an object

      Log level: info

      Parameters

      • ...data: unknown[]

        The object to print

      Returns void

    • Print an error message

      Log level: errors

      Parameters

      • ...data: unknown[]

        The data to print

      Returns void

    • Gets the current scope for this Logger instance.

      Returns string

      Scope name

    • Start a new group

      Log level: info

      Parameters

      • Optionallabel: string

        The group label

      Returns void

      This will create a group regardless of the log level, but the group label will only be printed if log level includes info. The group is not ended automatically, so you need to call groupEnd to end the group. The groups are tracked regardless of the scope

    • Start a new collapsed group

      Collapsed groups don't include indentation for their content

      Log level: info

      Parameters

      • Optionallabel: string

        The group label

      Returns void

      This will create a collapsed group regardless of the log level, but the group label will only be printed if log level includes info. The group is not ended automatically, so you need to call groupEnd to end the group. The groups are tracked regardless of the scope

    • End the current group

      Log level: info

      Returns void

      This will end the current group regardless of the log level, but the log message indicating the end of the group will only be printed if log level includes info. If there is no group to end, this method will have no effect. The groups are tracked regardless of the scope

    • Print an info message

      Log level: info

      Parameters

      • ...data: unknown[]

        The data to be printed

      Returns void

    • Log a message

      Log level: info

      Parameters

      • ...data: unknown[]

        The data to be printed

      Returns void

    • Experimental

      Start profiling with a given label

      You can end profiling using the Logger.profileEnd method

      Log level: info

      Parameters

      • label: string = 'default'

        The label for profiling, default is default

      Returns void

      The label will automatically include the scope of the logger The profile will not start if log level is set to a level that does not include info (e.g. warn, error, none). This method uses performance.now() and not console.profile as the latter isn't currently implemented

    • Experimental

      End profiling with a given label and log the duration

      You can start profiling using the Logger.profile method

      Log level: info

      Parameters

      • label: string = 'default'

        The label for profiling, default is default

      Returns void

      The label will always automatically include the scope of the logger The profile will not end if log level is set to a level that does not include info (e.g. warn, error, none). If no active profile with the given label is found, a warning message will be logged instead This method uses performance.now() and not console.profileEnd as the latter isn't currently implemented.

    • Sets the scope for this Logger instance, which will be prefixed to all log messages.

      If an empty string is provided, no scope will be used.

      Parameters

      • scope: string

        Scope name

      Returns void

    • Print a table to the console

      Log level: info

      Parameters

      • data: Record<string, unknown>[]

        The data for table

      • Optionalcolumns: string[]

        The columns to include in the table, if not provided all columns will be included

      Returns void

    • Print a table to the console

      Log level: info

      Parameters

      • data: unknown[]

        The data to be printed

      Returns void

    • Start a timer with the given label

      The timer can be ended using the Logger.timeEnd method. The Logger.timeLog method can be used to log the current duration without ending the timer.

      Log level: info

      Parameters

      • label: string = 'default'

        Label for measuring time, default is default

      Returns void

      The timer label will automatically include the scope of the logger. The timer will not start if log level is set to a level that does not include info (e.g. warn, error, none). If a timer with the same label already exists, it will be overwritten with the new start time.

    • End a timer with the given label

      If there is no active timer with the given label, this method will have no effect. The timer can be started using the Logger.time method, and the current duration can be logged without ending the timer using the Logger.timeLog method.

      Log level: info

      Parameters

      • label: string = 'default'

        The label for measuring time, default is default

      Returns void

      The timer label will automatically include the scope of the logger. The timer will not end if log level is set to a level that does not include info (e.g. warn, error, none).

    • Log current duration of a timer with the given label without ending the timer

      If there is no active timer with the given label, this method will have no effect. The timer can be started using the Logger.time method, and it can be ended using the Logger.timeEnd method.

      Log level: info

      Parameters

      • label: string = 'default'

        The label for measuring time, default is default

      Returns void

      The timer label will automatically include the scope of the logger. The timer will not log if log level is set to a level that does not include info (e.g. warn, error, none).

    • Experimental

      This doesn't do anything at the moment

      Passed the label to console.timeStamp

      Parameters

      • label: string = 'default'

      Returns void

      The label will automatically include the scope of the logger.

    • Print a stack trace with the given data

      Log level: verbose

      Parameters

      • ...data: unknown[]

        The data to be printed

      Returns void

      The stack trace might be indented incorectly The stack tree will include the logger implementation

    • Print a warning message

      Log level: warnings

      Parameters

      • ...data: unknown[]

        The data to be printed

      Returns void