magiclogger - v0.1.0
    Preparing search index...

    Class Logger

    Main Logger class that provides a unified logging interface.

    This class automatically detects the runtime environment (Node.js or Browser) and instantiates the appropriate underlying logger implementation. It manages transports for flexible log delivery to various destinations and provides multiple styling APIs for rich text formatting.

    Logger

    // Basic usage with styling
    const logger = new Logger({ useColors: true });

    // Standard logging
    logger.info('Server started');
    logger.error('Connection failed');

    // Styled logging with multiple APIs
    logger.info(logger.s.green.bold('✓ Success'));
    logger.error(logger.fmt`@red.bold{Error:} ${message}`);
    logger.warn(logger.parts([['Warning:', 'yellow', 'bold']]));
    logger.info('<green>Success:</> Operation complete');
    Index

    Constructors

    • Creates a new Logger instance with the specified options.

      Parameters

      • Optionaloptions: boolean | LoggerOptions = {}

        Logger configuration options or verbose flag

      • OptionalwriteToDisk: boolean

        Whether to write to disk (backward compatibility)

      • OptionaluseColors: boolean

        Whether to use colors (backward compatibility)

      Returns Logger

    Accessors

    • get fmt(): TemplateFormatter

      Template literal formatter for inline styling. Pre-initialized for performance.

      Returns TemplateFormatter

      Template formatter function

      const user = 'john';
      logger.info(logger.fmt`@green.bold{User ${user}} logged in`);
      logger.error(logger.fmt`@red{Error:} @yellow{${errorMessage}}`);
    • get logDir(): string

      Gets the log directory path (deprecated).

      Returns string

      Log directory path

      Use transports instead

    • get logFile(): null | string

      Gets the current log file path (deprecated).

      Returns null | string

      Current log file path or null

      Use transports instead

    • get logRetentionDays(): number

      Gets the log retention days setting (deprecated).

      Returns number

      Number of days to retain logs

      Use transports instead

    • get s(): IStyleBuilder

      Chainable style builder for creating styled strings. Pre-initialized for performance.

      Returns IStyleBuilder

      Chainable style builder

      // Chain multiple styles
      logger.info(logger.s.red.bold('Error:') + ' Failed');

      // Create reusable styles
      const error = logger.s.red.bold;
      logger.error(error('Critical failure'));
    • get style(): IStyleBuilder

      Alias for the style builder (s). Provides a more descriptive name for the chainable style API.

      Returns IStyleBuilder

      Chainable style builder

    • get useColors(): boolean

      Gets the colors enabled setting.

      Returns boolean

      Whether colors are enabled

    • get verbose(): boolean

      Gets the verbose mode setting.

      Returns boolean

      Whether verbose mode is enabled

    • get writeToDisk(): boolean

      Gets the write-to-disk setting (deprecated).

      Returns boolean

      Whether file logging is enabled

      Use transports instead

    Methods

    • Add to existing context (merges with existing).

      Parameters

      • context: Record<string, unknown>

        Context to merge

      Returns void

    • Add tags to existing tags.

      Parameters

      • tags: string[]

        Tags to add

      Returns void

    • Adds a transport to the logger.

      Parameters

      Returns Promise<void>

    • Applies a preset style to text.

      Parameters

      • text: string
      • preset:
            | "info"
            | "success"
            | "warning"
            | "error"
            | "debug"
            | "important"
            | "highlight"
            | "muted"
            | "special"
            | "code"
            | "header"

      Returns string

    • Prints text in a decorative box with customizable borders.

      Parameters

      • text: string

        Text to display inside the box (supports multiline)

      • options: {
            border?: "single" | "double" | "rounded" | "heavy";
            borderColor?: string[];
            color?: string[];
            padding?: number;
        } = {}

        Box formatting options

        • Optionalborder?: "single" | "double" | "rounded" | "heavy"

          Border style: 'single' | 'double' | 'rounded' | 'heavy' (default: 'single')

        • OptionalborderColor?: string[]

          Colors for the box borders

        • Optionalcolor?: string[]

          Colors for the text inside the box

        • Optionalpadding?: number

          Padding around the text (default: 1)

      Returns void

      logger.box('Success!', {
      border: 'double',
      borderColor: ['green'],
      color: ['green', 'bold']
      });
    • Cleans up old log files (deprecated).

      Returns void

      Use file transports instead

    • Clears all stored logs from browser storage (deprecated).

      Returns void

      Use browser transports instead

    • Closes the logger and all transports.

      Returns Promise<void>

    • Creates a reusable color function (legacy method).

      Parameters

      • ...colors: string[]

      Returns (text: string) => string

    • Applies colors to text using ANSI escape codes.

      Parameters

      • text: string
      • colors: string[]

      Returns string

    • Applies different colors to specific parts of a message (legacy method).

      Parameters

      • message: string
      • colorMap: Record<string, ColorName[]>

      Returns string

      Use parts() or styleByIndex() for better control

    • Counts the number of times this method is called with the same label.

      Parameters

      • label: string = 'default'

        Label for the counter (default: 'default')

      Returns void

      logger.count('api-calls'); // Logs: "api-calls: 1"
      logger.count('api-calls'); // Logs: "api-calls: 2"
      logger.count('api-calls'); // Logs: "api-calls: 3"
      logger.countReset('api-calls');
      logger.count('api-calls'); // Logs: "api-calls: 1"
    • Resets a counter to zero.

      Parameters

      • label: string = 'default'

        Label of the counter to reset (default: 'default')

      Returns void

      logger.count('errors'); // "errors: 1"
      logger.count('errors'); // "errors: 2"
      logger.countReset('errors');
      logger.count('errors'); // "errors: 1"
    • Logs a custom message with custom colors (legacy method).

      Parameters

      • msg: string
      • colors: string[] = ...
      • prefix: string = 'LOG'

      Returns void

      Use standard log methods with transports for better control

    • Logs a debug message. Enhanced to support angle bracket syntax