1.2.2 • Published 2 years ago

logger-colors v1.2.2

Weekly downloads
2
License
MIT
Repository
-
Last release
2 years ago

logger-colors npm version npm licence npm installs npm bundle size

Utilities for printing and writing logs for Node.Js

         

Install

npm install -s logger-colors

Demo

import { Logger, LColor } from 'logger-colors';


let myLogger = new Logger({
    operationId: 'demo',
    centerColumns: 50,
});
myLogger.info(`Info`);
myLogger.error("Error");
myLogger.success("Success");
myLogger.warn("Warn");

myLogger.cyan("Cyan");
myLogger.magenta("Magenta");

myLogger.info(spacer(50));

const center = true;
myLogger.info(`Info-Center`, center);
myLogger.error("Error-Center", center);
myLogger.success("Success-Center", center);
myLogger.warn("Warn-Center", center);

myLogger.magenta(`a${LColor.c_green}b${LColor.c_yellow}c`, true);

myLogger.info(
    `${LColor.c_gray}Gray` +
    `${LColor.c_red} Red` +
    `${LColor.c_green} Green` +
    `${LColor.c_yellow} Yellow` +
    `${LColor.c_magenta} Magenta` +
    `${LColor.c_cyan} Cyan` +
    `${LColor.c_white} White`,
);

Will become:

logger-colors demo

         

Methods

  • Logger.info
methoddescription
logger.info(str, center=false)prints an info log (grey color)
logger.error(str, center=false)prints an error log (red color)
logger.success(str, center=false)prints an success log (green color)
logger.warn(str, center=false)prints an warn log (yellow color)
logger.magenta(str, center=false)prints an magenta color log
logger.cyan(str, center=false)prints an cyan color log

Options

optiontypedefaultdescription
timeZonestring'America/Mexico_City'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
timeFormatstring'DD-MM-YYYY HH:mm:ss.SSS'momentjs format string
languajestring'es'languaje string for date (moment locale)
centerColumnsnumber50This option centers the content of the log in the indicated, eg. 'a', centered at 5 will become ' a '
operationIdstringnullUsed for easy identification of logs, will be present after the datetime in each log
writeToFileWriteToFileOptionsOptions for writing the log to an external file

WriteToFileOptions

optiontypedefaultdescription
enabledbooleanfalseif enabled then the logs will be writted to the file
preserveColorsbooleantrueif the colors want to be preserved, this will look bad if you open the log file in a non-compatible terminal or if you want to process the log info, useful just for searching with cat/grep
fileNamebooleanlogname of the file to save
fileExtensionbooleantxtthe output will be text, the extension is just for the filename
directoryboolean/logsdirectory name, snippet %date% can be used and will be replaced with the current date when writing the file, eg. /etc/logs/my-logs-%date% ==> /etc/logs/my-logs-2021-01-01
createDirIfNotExistsbooleantruewhether or not the directory should be created if it does not exists

     

You can also prefix your string with one of this variables to have multiple colors in one line:

import { LColor } from 'logger-colors';
  • LColor.c_gray
  • LColor.c_green
  • LColor.c_red
  • LColor.c_yellow
  • LColor.c_magenta
  • LColor.c_cyan
  • LColor.c_white

Note: All the logs are printed internally using console.log, so this is not useful if you want to handle errors/debug logs with stdout and stderr, everything is sent to stdout.