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?
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With