2.0.2 • Published 1 year ago

colored-beautiful-logger v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Colored Beautiful Logger

The colored-beautiful-logger library provides a flexible and customizable logger for your Node.js applications. It offers various log levels, colors, and formatting options to enhance readability and debugging experience.

Installation

To install the colored-beautiful-logger, run the following command in your project directory:

npm install colored-beautiful-logger

Usage/Examples

To use the logger, import Logger into your project. You can use default colors, RGB colors, or ANSI colors in custom labels.

For ANSI color references, see here.

import { Logger, LoggerOptions } from 'colored-beautiful-logger';

const options: LoggerOptions = {
  logFolderPath: './logs',
  logFileName: 'app.log',
  logRotationInterval: 60000, // 1 minute
  saveLogFile: true,
  customLabels: [
    // Take ANSI color reference from https://talyian.github.io/ansicolors/
    // Available color list:
    // ["BLACK", "RED", "GREEN", "YELLOW", "BLUE", "CYAN", "WHITE", "BLACKBG", "REDBG", "GREENBG", 
    // "YELLOWBG", "BLUEBG", "MAGENTABG", "CYANBG", "WHITEBG", "RESET", RGBColor, AnsiColor]
    { label: 'custom', color: { ansiCode: '\x1b[38;5;9m' } },
    { label: 'unique', color: 'RED' },
    { label: 'insideFunction', color: { R: 255, G: 204, B: 229, isBackground: true } }
  ],
  printTimestamp: true,
  printLabelName: true,
  printCallerFunctionLocation: true,
};

const { logger } = new Logger(options);

let test = () => {
  logger.insideFunction('Inside a function');
};
test();
logger.info('This is an informational message');
logger.warning('This is a warning message');
logger.error('This is an error message');
logger.custom('This is a custom message');
logger.critical('This is a critical message');
logger.alert('This is an alert message');
logger.log('This is a log message');
logger.notify('This is a notify message');
logger.success('This is a success message');
logger.unique('We are happy');

Result

Console Output Image

API Reference

The colored-beautiful-logger provides the following interfaces and methods:

Logger Methods

  • logger.error(message: any): Logs an error message.
  • logger.warning(message: any): Logs a warning message.
  • logger.info(message: any): Logs an informational message.
  • logger.success(message: any): Logs a success message.
  • logger.log(message: any): Logs a general message.
  • logger.notify(message: any): Logs a notification message.
  • logger.alert(message: any): Logs an alert message.
  • logger.critical(message: any): Logs a critical message.
  • logger.yourCustomLabelName(message: any): Logs a message with a custom label.

new Logger (options: LoggerOptions)

A base class to initialize the Logger.

const { logger } = new Logger(options);

Parameters

NameTypeArgumentDescription
LoggerOptionsLoggerOptions<optional>Set your options as you want to see logs

LoggerOptions

Interface representing options for configuring the logger.

Type:

  • object

Example:

const options: LoggerOptions = {
  logFolderPath: './logs',
  logFileName: 'app.log',
  logRotationInterval: 60000, // 1 minute
  saveLogFile: true,
  customLabels: [
    { label: 'custom', color: { ansiCode: '\x1b[38;5;9m' } },
    { label: 'unique', color: 'RED' },
    { label: 'insideFunction', color: { R: 255, G: 204, B: 229, isBackground: true } }
  ],
  printTimestamp: true,
  printLabelName: true,
  printCallerFunctionLocation: true,
};

Properties:

NameTypeArgumentDefaultDescription
logFolderPathstring<optional>The path to the folder where logs will be saved.
logFileNamestring<optional>The name of the log file.
logRotationIntervalnumber<optional>24 * 60 * 60 * 1000The interval for log rotation, in milliseconds.
saveLogFileboolean<optional>falseWhether to save the log file.
customLabelscustomLabels[]<optional>Custom labels to use in logging.
printTimestampboolean<optional>falseWhether to print a timestamp in each log entry.
printLabelNameboolean<optional>falseWhether to print the label name in each log entry.
printCallerFunctionLocationboolean<optional>falseWhether to print the caller function location in each log entry.

customLabels

Interface representing custom labels with associated colors.

Type:

  • object

Example:

customLabels: [
  { label: 'custom', color: { ansiCode: '\x1b[38;5;9m' } },
  { label: 'unique', color: 'RED' },
  { label: 'insideFunction', color: { R: 255, G: 204, B: 229, isBackground: true } }
];

Properties:

NameTypeArgumentDescription
colorCOLOR<optional>The color associated with the label.
labelstringoptionalThe custom label text.

COLOR

Values:

NameTypeValueDescription
BLACKstring\x1b[30mBlack Color
REDstring\x1b[31mRED Color
GREENstring\x1b[32mGREEN Color
YELLOWstring\x1b[33mYELLOW Color
BLUEstring\x1b[34mBLUE Color
MAGENTAstring\x1b[35mMAGENTA Color
CYANstring\x1b[36mCYAN Color
WHITEstring\x1b[37mWhite Color
BLACKBGstring\x1b[40mBackground Black Color
REDBGstring\x1b[41mBackground Red Color
GREENBGstring\x1b[42mBackground Green Color
YELLOWBGstring\x1b[43mBackground Yellow Color
BLUEBGstring\x1b[44mBackground Blue Color
MAGENTABGstring\x1b[45mBackground Magenta Color
CYANBGstring\x1b[46mBackground Cyan Color
WHITEBGstring\x1b[47mBackground White Color
RESETstring\x1b[0mBackground Reset Color
AnsiColorobjectColor in ANSI Code
{ ansiCode: string }
RGBColorobjectRGB Color values
{ R: number, G: number, B: number, isBackground <optional>: boolean }

Notes

  • If you get an error message in the browser console stating that Property 'yourlabelname' comes from an index signature, so it must be accessed with ['yourlabelname'], then add this line in your tsconfig.json file: "noPropertyAccessFromIndexSignature": false.
  • Log file save option is not supported in the browser environment.
  • The color of terminal console output and browser console output can be different.

Changelog

See Changelog here for more information.

Color Reference

For ANSI color reference, see here.

Authors

Contributing

Contributions are always welcome!

If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Refrences