Utilities, helpers, formatters, transports for winston logger.
Collection of log levels and colors. Currently supports the following ones.
- RFC 3164
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 }) })],
// ...
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" }
Log Level | Severity | Description |
---|---|---|
0 | Emergency (emerg ) |
System is unusable |
1 | Alert (alert ) |
Action must be taken immediately |
2 | Critical (crit ) |
Critical conditions |
3 | Error (error ) |
Error conditions |
4 | Warning (warn ) |
Warning conditions |
5 | Notice (notice ) |
Normal but significant condition |
6 | Informational (info ) |
Informational messages |
7 | Debug (debug ) |
Debug-level messages |
Severity | Color |
---|---|
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 |
Log Level | Severity | Description |
---|---|---|
0 | Emergency (emerg ) |
System is unusable |
1 | Alert (alert ) |
Action must be taken immediately |
2 | Critical (crit ) |
Critical conditions |
3 | Error (error ) |
Error conditions |
4 | Warning (warn ) |
Warning conditions |
5 | Notice (notice ) |
Normal but significant condition |
6 | Informational (info ) |
Informational messages |
7 | Debug (debug ) |
Debug-level messages |
Severity | Color |
---|---|
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 |
Collection of formatters. Currently supports the following ones.
- piiRedact
piiRedact
uses fast-redact
for redacting data. Kindly check the fast-redact
documentation for more info.
⚠️ UsestructuredClone
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
}
Configuration for piiRedact formatter
Example
// Default value
const config = {
paths = [],
censor = '[REDACTED]',
};
Config Name | Type | Description |
---|---|---|
paths | Array | Object paths to be redacted. Refer path - Array |
censor | String | The value to be replaced after redacting. Default is [REDACTED] . |