A comprehensive logging utility class that provides various log levels, message formatting, and log accumulation capabilities.

Features:

  • Multiple log levels (Debug, Info, Warn, Error, Assert)
  • Rich formatting with timestamps
  • Log accumulation for later retrieval
  • Configurable log level filtering
// Configure logger
Logger.logLevel = LogLevel.Info;
Logger.isRichLog = true;
Logger.isAccumulateLog = true;

// Use logging methods
Logger.info("Application started");
Logger.warn("This is a warning");
Logger.error("An error occurred");

// Retrieve accumulated logs
const logs = Logger.getAccumulatedLog();

Constructors

Properties

isAccumulateLog: boolean = false

Whether to store log messages in memory for later retrieval.

isRichLog: boolean = false

Whether to format log messages with timestamps and log level prefixes.

logLevel: LogLevel = LogLevel.Warn

The minimum log level that will be output. Messages below this level are ignored.

Methods

  • Performs a console assertion and logs a message if the condition is false. Assert messages are used for debugging and testing critical assumptions.

    Parameters

    • condition: boolean

      The condition to test; if false, the assertion fails

    • message: string

      The message to log when the assertion fails

    Returns undefined | string

    The formatted message if logged, undefined if filtered out

    Logger.assert(user !== null, "User object should not be null at this point");
    
  • Logs a debug message to the console if the current log level permits it. Debug messages provide detailed information useful for troubleshooting and development.

    Parameters

    • message: string

      The debug message to log

    Returns undefined | string

    The formatted message if logged, undefined if filtered out

    Logger.debug("Processing user input: " + JSON.stringify(input));
    
  • Logs an error message to the console if the current log level permits it. Error messages indicate serious problems that should be addressed immediately.

    Parameters

    • message: string

      The error message to log

    Returns undefined | string

    The formatted message if logged, undefined if filtered out

    Logger.error("Database connection failed");
    
  • Retrieves all accumulated log messages as formatted strings. Only available when isAccumulateLog is enabled.

    Returns string[]

    An array of formatted log message strings

    Logger.isAccumulateLog = true;
    Logger.info("First message");
    Logger.warn("Second message");

    const logs = Logger.getAccumulatedLog();
    console.log(logs); // Array of formatted log messages
  • Logs an informational message to the console if the current log level permits it. Info messages provide general information about application flow and state.

    Parameters

    • message: string

      The informational message to log

    Returns undefined | string

    The formatted message if logged, undefined if filtered out

    Logger.info("User authentication successful");
    
  • Logs a warning message to the console if the current log level permits it. Warning messages indicate potential issues that should be monitored.

    Parameters

    • message: string

      The warning message to log

    Returns undefined | string

    The formatted message if logged, undefined if filtered out

    Logger.warn("API response time is slower than expected");