0.1.0 • Published 9 days ago

@niveus/winston-utils v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

Npm package version Npm package monthly downloads GitHub issues GitHub contributors

winston-utils

Utilities, helpers, formatters, transports for winston logger.

Configs

Collection of log levels and colors. Currently supports the following ones.

  • google
  • RFC 3164

How to use config

Example usage of config.

const { google } = require('@niveus/winston-utils').config;
const winston = require('winston');
const { combine, colorize, json } = winston.format;


const logger = winston.createLogger({
    level: 'debug',
    levels: google.levels,  // Log Levels
    format: combine(
        colorize({ all: true }),
        json(),
    ),
    transports: [new winston.transports.Console()],
});

winston.addColors(google.colors);  // Log Colors

module.exports = logger;

If this is not adding the colors, try removing the colorize formatter and add it to the transports like below

// ...

transports: [new winston.transports.Console({ format: winston.format.colorize({ all: true }) })],

// ...

How To Use Logger

Each config adds its own custom log levels. Specific log levels can be called using it's corresponding method name. In the example below, it uses google config. Check the sevierity levels for the config used.

// uses google config
const logger = require('./path/to/logger');

logger.emerg('Emergency log', {custom: 'data'});
// LOG: { "level": "emerg", "message": "Emergency log", "custom": "data" }

logger.alert('Alert log', {custom: 'data'});
// LOG: { "level": "alert", "message": "Alert log", "custom": "data" }

Google Config

Log Levels
Log LevelSeverityDescription
0Emergency (emerg)System is unusable
1Alert (alert)Action must be taken immediately
2Critical (crit)Critical conditions
3Error (error)Error conditions
4Warning (warn)Warning conditions
5Notice (notice)Normal but significant condition
6Informational (info)Informational messages
7Debug (debug)Debug-level messages
Log Colors
SeverityColor
Emergency (emerg)bold white blackBG
Alert (alert)bold red yelloBG
Critical (crit)bold white redBG
Error (error)black redBG
Warning (warn)black magentaBG
Notice (notice)white blueBG
Informational (info)white greenBG
Debug (debug)black yellowBG

RFC 3164 Config

Log Levels
Log LevelSeverityDescription
0Emergency (emerg)System is unusable
1Alert (alert)Action must be taken immediately
2Critical (crit)Critical conditions
3Error (error)Error conditions
4Warning (warn)Warning conditions
5Notice (notice)Normal but significant condition
6Informational (info)Informational messages
7Debug (debug)Debug-level messages
Log Colors
SeverityColor
Emergency (emerg)bold white blackBG
Alert (alert)bold red yelloBG
Critical (crit)bold white redBG
Error (error)black redBG
Warning (warn)black magentaBG
Notice (notice)white blueBG
Informational (info)white greenBG
Debug (debug)black yellowBG

Formatters

Collection of formatters. Currently supports the following ones.

  • piiRedact

How to use piiRedact formatter

piiRedact uses fast-redact for redacting data. Kindly check the fast-redact documentation for more info.

⚠️ Use structuredClone or deep copy to log data in the log, or the redactor will mutate the data object.

Example code for using piiRedact.

// Logger file.

const { piiRedact } = require('@niveus/winston-utils').formatters;
const winston = require('winston');
const { combine, json } = winston.format;

// Configuration for the formatter.
const piiFormatterConfig = {
    paths: ['data.emailId'], // Or env based values, secrets manager, etc...
}

const piiRedactFormatter = formatters.piiRedact(piiFormatterConfig);

const logger = winston.createLogger({
    level: 'debug',
    format: combine(
        piiRedactFormatter,
        json(),
    ),
    transports: [new winston.transports.Console()],
});

module.expoers = logger;



// .... Somewhere else in the repository

const logger = require('./path/to/logger');

function resetPassword(data) {
    // data = {emailId: 'user@example.com'}
    logger.debug('user password reset', {data: structuredClone(data) });

    // LOG: { "level": "debug", "message": "user password reset", "data": { "emailId": "[REDACTED]" }}

    // ... rest of the code
}

piiRedact Config

Configuration for piiRedact formatter

Example

// Default value
const config = {
    paths = [],
    censor = '[REDACTED]',
};
Config NameTypeDescription
pathsArrayObject paths to be redacted. Refer path - Array
censorStringThe value to be replaced after redacting. Default is [REDACTED].
0.1.0

9 days ago

0.0.1

1 month ago