Skip to content

Logger._formatMessage

Summary

The _formatMessage method formats a log message by adding a timestamp, log level, and optional trace information such as the function name, file name, and line number.

Example Usage

javascript
import { Logger } from 'utility/Logger.js';

Logger.includeTrace = true; // optional for more information
const formattedMessage = Logger._formatMessage('INFO', 'This is a log message');
console.log(formattedMessage);

Code Analysis

Inputs

  • level: The log level (e.g., INFO, WARN, ERROR).
  • message: The log message to be formatted.

Flow

  1. Get the current timestamp using moment.
  2. Retrieve the stack trace using stackTrace.get().
  3. Extract the function name from the stack trace.
  4. If includeTrace is false, format the message with timestamp, level, and function name (if available).
  5. If includeTrace is true, also include file name and line number in the formatted message.

Outputs

  • A formatted log message string with timestamp, log level, and optional trace information.