Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node winston cannot support multiple file transport?

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',
like image 796
user2749751 Avatar asked Apr 17 '14 08:04

user2749751


People also ask

What are the transport options available in Winston by default?

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.

What are the core transports in Winston?

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).

Is Winston good for node?

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.

What are the file writing options supported by the file transport?

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 ).


1 Answers

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.

like image 184
user2749751 Avatar answered Oct 31 '22 22:10

user2749751