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
- Get the current timestamp using
moment. - Retrieve the stack trace using
stackTrace.get(). - Extract the function name from the stack trace.
- If
includeTraceis false, format the message with timestamp, level, and function name (if available). - If
includeTraceis 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.