lily
    Preparing search index...

    Class Logger

    main Logger class. provides hierarchical logging with customizable transports, formatters, and scopes. supports child loggers for organized logging across application components.

    const logger = new Logger('myapp');
    logger.info('hello world');
    const dbLogger = logger.child('database');
    dbLogger.error('connection failed');
    const logger = new Logger('app');
    logger.addTransport(new FileTransport({ filename: 'app.log' }));
    logger.info('logged to both console and file');
    Index

    Constructors

    • creates a new Logger instance.

      Parameters

      • scope: string | string[] = []

        the scope name(s) for this logger. can be a string or array of strings

      • options: LoggerOptions = {}

        configuration options including level, timestamp, colourization, and metadata

      Returns Logger

      const logger = new Logger('api');
      
      const logger = new Logger(['app', 'database', 'users']);
      
      const logger = new Logger('debug-logger', {
      level: LogLevel.DEBUG,
      colourize: false,
      metadata: { version: '1.0.0' }
      });

    Methods

    • adds a transport to this logger instance. transports determine where log entries are sent (console, file, network, etc.).

      Parameters

      Returns void

      logger.addTransport(new FileTransport({ filename: 'app.log' }));
      logger.addTransport(new HttpTransport({ url: 'https://api.example.com/logs' }));
    • creates a child logger with additional scope. child loggers inherit all transports from their parent.

      Parameters

      • scope: string | string[]

        additional scope to append to the current scope

      • options: LoggerOptions = {}

        options to override from parent logger

      Returns Logger

      new Logger instance with extended scope

      const apiLogger = logger.child('api');
      apiLogger.info('api request received'); // logs with scope ['app', 'api']
      const authLogger = apiLogger.child(['auth', 'login']);
      // results in scope: ['app', 'api', 'auth', 'login']
      const debugLogger = logger.child('debug', { level: LogLevel.DEBUG });
      
    • removes all transports from this logger instance. after calling this, log entries will not be output anywhere until new transports are added.

      Returns void

    • logs a debug message. used for debugging information during development.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.debug('user data received', userData);
      
    • logs an error message. used for error conditions that don't require immediate attention.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.error('failed to process request', error);
      
    • logs a fatal message. used for severe error events that may lead to application termination.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.fatal('database connection lost', { attempts: 3 });
      
    • logs an info message. used for general informational messages.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.info('server started on port 3000');
      
    • removes a specific transport from this logger instance.

      Parameters

      • transport: Transport

        the transport instance to remove

      Returns void

    • logs a trace message. used for very detailed debugging information.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.trace('entering function', { functionName: 'processUser' });
      
    • logs a warning message. used for potentially harmful situations.

      Parameters

      • message: string

        the message to log

      • ...args: unknown[]

        additional arguments to include

      Returns void

      logger.warn('deprecated api endpoint used', { endpoint: '/old-api' });
      
    • creates a new logger instance with additional metadata. useful for adding context information to all log entries from this logger.

      Parameters

      • metadata: Record<string, unknown>

        additional metadata to merge with existing metadata

      Returns Logger

      new logger instance with combined metadata

      const requestLogger = logger.withMetadata({
      requestId: 'req-123',
      userId: 'user-456'
      });

      requestLogger.info('processing request');
      // all logs will include the requestId and userId metadata
    • sets the global log level for all logger instances. affects all existing and future logger instances unless they have an explicit level set.

      Parameters

      • level: LogLevel

        the log level to set globally

      Returns void

      Logger.setGlobalLevel(LogLevel.DEBUG);
      // all loggers will now log debug messages and above