Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston/Node.js how add a Transport only for certain environment?

I have the follow configuration:

var winston = require('winston');
var Mail = require('winston-mail').Mail;

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({ level: 'info', colorize: true }),
    new (winston.transports.File)({ level: 'info', filename: './logs/logs.log',
    new (Mail)({
              to: '[email protected]',
              from: '[email protected]',
              subject: 'Errors occurred',
              level: 'error',
              host: 'smtp.xxxxx.xx',
              username: '[email protected]', 
              password: 'xxxxx',
              port: xxx
            })
  ],
  exceptionHandlers: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: './logs/exceptions.log' }),
    new (Mail)({
              to: '[email protected]',
              from: '[email protected]',
              subject: 'Errors occurred',
              level: 'error',
              host: 'smtp.xxxxx.xx',
              username: '[email protected]', 
              password: 'xxxxx',
              port: xxx
            })
 ]
});

I'd like add the Mail transport only for production environment not in staging or development.

I set a different log level as follow:

if (process.env.NODE_ENV === 'development') {
  logger.transports.console.level = 'debug';
  logger.transports.file.level = 'debug';
}

There is a way to do that also with transport?

like image 380
Zauker Avatar asked Apr 15 '16 08:04

Zauker


People also ask

What is a logger transport?

Transport data loggers are used to monitor the shipment of various types of sensitive products. Depending on model, they record different parameters like temperature, humidity, shock, vibration or tilt.

What is a Winston transport?

In winston a transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, one may want error logs to be stored in a persistent remote location (like a database), but all logs output to the console or a local file.

Is Winston a middleware?

Usage. express-winston provides middlewares for request and error logging of your express. js application. It uses 'whitelists' to select properties from the request and (new in 0.2.

What is Winston logger in node js?

Winston provides logging levels. They indicate the log priority; this gives you the ability to sort out critical logs from logs requiring less attention. For example: {error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5} . In this case, an error log is given priority over a verbose log.


1 Answers

The transports configuration is just a plain old Array. So we can use all the usual tricks on it. In this case, you want a conditional push().

const winston = require('winston');
const transports = [
    new winston.transports.Console({
        level    : 'info',
        colorize : true
    }),
    new winston.transports.File({
        level    : 'info',
        filename : './logs/logs.log'
    })
];

if (process.env.NODE_ENV === 'production') {
    const Mail = require('winston-mail').Mail;

    transports.push(new Mail({
        to       : '[email protected]',
        from     : '[email protected]',
        subject  : 'Errors occurred',
        level    : 'error',
        host     : 'smtp.xxxxx.xx',
        username : '[email protected]', 
        password : 'xxxxx',
        port     : 1234
    }));
}

const logger = new winston.Logger({
    transports
});

This code should be usable as is on Node.js >= 4, after filling in the mail configuration placeholders.

The same principle can be applied to the exceptionHandlers.

like image 197
Seth Holladay Avatar answered Oct 20 '22 19:10

Seth Holladay