Logger
Summary
The Logger class provides a comprehensive logging utility with various log levels, message formatting, and styling options. It supports logging messages with different severity levels (e.g., info, warn, error) and allows customization of text and background colors using the chalk library. Additionally, it includes options for applying text styles like bold, italic, and underline, as well as custom colors. The class also supports including stack trace information in log messages for better debugging.
Example Usage
javascript
Logger.log('This is a log message');
Logger.writeInfo('This is an info message');
Logger.writeWarn('This is a warning message');
Logger.writeError('This is an error message');
Logger.writeSuccess('This is a success message');
Logger.writeDebug('This is a debug message');
const styledMessage = Logger.bold('This is a bold message');
console.log(styledMessage);
const customColorMessage = Logger.customColor('#FF5733', 'This is a custom color message');
console.log(customColorMessage);
Logger.styledLog(['bold', 'underline'], 'This is a styled log message');Code Analysis
Main functionalities
- Logging messages at various levels (log, info, warn, error, success, debug).
- Formatting messages with timestamps and optional stack trace information.
- Styling messages with text styles (bold, italic, underline, strikethrough).
- Customizing text and background colors using hexadecimal and RGB values.
Methods
log(message, ...optionalParams): Logs a message at the 'log' level.writeInfo(message, ...optionalParams): Logs a message at the 'info' level with blue color.info(message, ...optionalParams): Returns a formatted 'info' message.writeWarn(message, ...optionalParams): Logs a message at the 'warn' level with yellow color.warn(message, ...optionalParams): Returns a formatted 'warn' message.writeError(message, ...optionalParams): Logs a message at the 'error' level with red color.error(message, ...optionalParams): Returns a formatted 'error' message.writeSuccess(message, ...optionalParams): Logs a message at the 'success' level with green color.success(message, ...optionalParams): Returns a formatted 'success' message.writeDebug(message, ...optionalParams): Logs a message at the 'debug' level with gray color.debug(message, ...optionalParams): Returns a formatted 'debug' message.bold(message): Applies bold style to a message.italic(message): Applies italic style to a message.underline(message): Applies underline style to a message.strikethrough(message): Applies strikethrough style to a message.bgBlack(message): Applies black background to a message.bgRed(message): Applies red background to a message.bgGreen(message): Applies green background to a message.bgYellow(message): Applies yellow background to a message.bgBlue(message): Applies blue background to a message.bgMagenta(message): Applies magenta background to a message.bgCyan(message): Applies cyan background to a message.bgWhite(message): Applies white background to a message.customColor(color, message): Applies a custom color to a message using hexadecimal color code.customRgbColor(r, g, b, message): Applies a custom RGB color to a message.customBgColor(color, message): Applies a custom background color to a message using hexadecimal color code.customBgRgbColor(r, g, b, message): Applies a custom background color to a message using RGB color values.styledLog(styles, message, ...optionalParams): Applies multiple styles to a message and logs it to the console._formatMessage(level, message): Formats the log message with timestamp, log level, and optional trace information.
Fields
includeTrace: A static boolean field that determines whether to include stack trace information in log messages.