6

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',
Pops
  • 30,199
  • 37
  • 136
  • 151
user2749751
  • 405
  • 7
  • 22

2 Answers2

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

user2749751
  • 405
  • 7
  • 22
9

You are getting this error because you are not providing a 'name' attribute to your file transports.

SpacePope
  • 1,423
  • 1
  • 15
  • 21