From 7dd4d220dab903abcacd723166a85cf59bb6fc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Mag=C3=A1n=20Garc=C3=ADa?= Date: Thu, 3 Oct 2013 13:41:40 +0200 Subject: [PATCH] Minor bugfix. Transports can be added independently --- lib/logger.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 4302a08..867593e 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -153,13 +153,17 @@ function createWinston(cfg) { winstonLogger = new (winston.Logger)({ level: cfg.logLevel, - exitOnError: exitOnError, - transports: [ - new (winston.transports.Console)(cfg.Console), - new (winston.transports.File)(cfg.File) - ] + exitOnError: exitOnError }); + if (cfg.Console) { + winstonLogger.add(winston.transports.Console, cfg.Console); + } + + if (cfg.File) { + winstonLogger.add(winston.transports.File, cfg.File); + } + winstonLogger.setLevels(winston.config.syslog.levels); } @@ -168,7 +172,12 @@ function setConfig(newCfg) { "use strict"; config = newCfg; - config.File.handleExceptions = true; //Necessary to handle file exceptions + + //Only when Transport File is going to be used. + if (config.File) { + config.File.handleExceptions = true; //Necessary to handle file exceptions + } + createWinston(config); } @@ -198,7 +207,7 @@ function newLogger() { var message = createLogMessage.call(this, level, message, logObj).replace(/\n/g, ''); var tmpLogger = winstonLogger.log(level, message); - if (config.flushEachMessage && tmpLogger.transports.file._stream) + if (config.flushEachMessage && tmpLogger.transports.file && tmpLogger.transports.file._stream) tmpLogger.transports.file.flush(); return tmpLogger; };