I want to show error logs in one file and all logs in another file. To do that, I wrote two file transports which give the following error on compilation:
'use strict';
var winston = require('winston'),
config = require('./config');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({level:'debug',handleExceptions: true,prettyPrint: true,silent:false,timestamp: true,colorize: true,json: false}),
new (winston.transports.File)({ filename: './server/logs/bv_common.log',level:'debug',maxsize: 1024000,maxFiles: 10, handleExceptions: true,json: false}),
new (winston.transports.File)({ filename: './server/logs/bv_error.log',level:'debug',maxsize: 1024000,maxFiles: 10, handleExceptions: true,json: false,level:'error'})
]
});
module.exports = logger;
Result:
[ 'Error: Transport already attached: file',
The following transport options are available in Winston by default: Console : output logs to the Node.js console. File : store log messages to one or more files. HTTP : stream logs to an HTTP endpoint. Stream : output logs to any Node.js stream.
There are several core transports included in winston, which leverage the built-in networking and file I/O offered by Node.js core. level: Level of messages that this transport should log (default: level set on parent logger). silent: Boolean flag indicating whether to suppress output (default false).
Based on the three-part score I introduced in Unit 7 ( quality, popularity, and maintenance) Winston’s score is 81 (98/82/67, respectively). Winston is easy to use and incredibly popular, and you are very likely to run across it as a professional Node developer.
The File transport supports a variety of file writing options. If you are looking for daily log rotation see DailyRotateFile level: Level of messages that this transport should log (default: level set on parent logger). silent: Boolean flag indicating whether to suppress output (default false). eol: Line-ending character to use. (default: os.EOL ).
var logger = new (winston.Logger)({
exitOnError: false, //don't crash on exception
transports: [
new (winston.transports.Console)({level:'debug',handleExceptions: true,prettyPrint: true,silent:false,timestamp: true,colorize: true,json: false}),
new (winston.transports.File)({ filename: './server/logs/' + config.appname +'_common.log',name:'file.all',level:'debug',maxsize: 1024000,maxFiles: 10, handleExceptions: true,json: false}),
new (winston.transports.File)({ filename: './server/logs/' + config.appname +'_error.log',name:'file.error',level:'error',maxsize: 1024000,maxFiles: 10, handleExceptions: true,json: false})
]
});
The above code , especially with name parameter for shared transport, we can use multiple file transports for loggers.
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