sente
sente

Reputation: 2377

nodejs / winston logging -- I want several log files, one with all requests, one with only POST's, another GET's (details inside)

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' }));

Upvotes: 3

Views: 4384

Answers (2)

Tom Grant
Tom Grant

Reputation: 2045

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

Upvotes: 3

floatingLomas
floatingLomas

Reputation: 8737

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.

Upvotes: 2

Related Questions