3

The code below logs all requests to two files, one of which is JSON formatted. This has the side effect of logging each request to the console twice. Why is that? I am sure I'm using Winston incorrectly, any feedback would be appreciated.

var winston = require('winston');


winston.loggers.add('main_nojson', {
    file: {
        filename: '/home/stu/logs/winston_txt.log',
        json: false
    }
});

winston.loggers.add('main_json', {
    file: {
        filename: '/home/stu/logs/winston_json.log',
        json: true
    }
});

var winlog1 = winston.loggers.get('main_nojson');
var winlog2 = winston.loggers.get('main_json');

var winstonStream = {
    write: function(message, encoding){
        winlog1.info(message);
        winlog2.info(message);
    }
};

app.use(express.logger({stream:winstonStream,  format: ':remote-addr - [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :response-time' }));
sente
  • 2,327
  • 2
  • 18
  • 24

2 Answers2

3

For anyone who may stumble across this old question from google or elsewhere:

An alternative approach is to remove the Console transport in a similar fashion to how you add them:

To add a File transport:

winston.add(winston.transports.File, { filename: 'logs/log.log' });

and to answer to OP's question, similarly to remove a Console transport, thereby suppressing the console output:

winston.remove(winston.transports.Console);
Tom Grant
  • 2,027
  • 18
  • 22
2

Try this:

winston.loggers.add('main_json', {
    console: {
        silent: true
    },
    file: {
    filename: '/home/stu/logs/winston_json.log',
        json: true
    }
});

Both console and file transports take a silent option to suppress output; by adding it to one of your loggers - or both, for that matter - it will suppress the console output, but retain the file output.

floatingLomas
  • 8,553
  • 2
  • 21
  • 27