Gemtastic
Gemtastic

Reputation: 6433

Logging to file with winston fails when in other module

I'm attempting to log to file with winston, and I have a module for this task looking like this:

var logger = require('winston');

logger.add(logger.transports.File, {filename: '../../logs/logs.log', logstash: true});
logger.remove(logger.transports.Console);

function log(type, message) {
    logger.log(type, message, function(err, level, msg){
        if(err){
            console.log('error:', err);
        }
    });
}

module.exports = {
    log: log,
    logger: logger
};

I've tried exporting the logger object in itself and my own log function.

The first time I visit localhost I get nothing (no logs). The second time I get "Error: Transport is in a failed state."

But if I run this module with a call to my log() function, it works just fine and logs to file as intended.

How do I make winston log to file from another module? Or how do I make a winston logger that logs to file.

Logging to console always works.

Full error message:

error: { [Error: Transport is in a failed state.]
  transport: 
   { domain: null,
     _events: { error: [Function] },
     _maxListeners: undefined,
     silent: false,
     raw: false,
     name: 'file',
     formatter: undefined,
     level: undefined,
     handleExceptions: false,
     exceptionsLevel: 'error',
     humanReadableUnhandledException: false,
     filename: 'logs.log',
     _basename: 'logs.log',
     dirname: '../../logs',
     options: { flags: 'a', highWaterMark: 24 },
     json: true,
     logstash: true,
     colorize: false,
     maxsize: null,
     rotationFormat: false,
     zippedArchive: false,
     maxFiles: null,
     prettyPrint: false,
     label: null,
     timestamp: true,
     eol: '\n',
     tailable: false,
     depth: null,
     showLevel: true,
     maxRetries: 2,
     stringify: undefined,
     _size: 0,
     _created: 0,
     _buffer: [],
     _draining: false,
     _opening: false,
     _failures: 2,
     _archive: null,
     _onError: [Function],
     opening: false,
     _stream: 
      { _writableState: [Object],
        writable: true,
        domain: null,
        _events: [Object],
        _maxListeners: Infinity,
        path: '../../logs/logs.log',
        fd: null,
        flags: 'a',
        mode: 438,
        start: undefined,
        pos: undefined,
        bytesWritten: 0,
        destroyed: true },
     _isStreams2: true } }

Upvotes: 0

Views: 1461

Answers (3)

Elad
Elad

Reputation: 2377

because logger is essential to the all system, my approach is to put the logger on the global variable, and then you can log from every page / module in your app.

Upvotes: 0

erickyi2006
erickyi2006

Reputation: 226

Sharing my discovery: I was shocked when my logger failed to log on a new function (very tiny) this morning. The logger was working before.

The reason: the log() in file.js (in winston library) could not write as the this.opening flag is still true. I added a small timeout routine to wait and the timeout allows the open to complete.

Hope this helps.

Upvotes: 1

Dushyant Bangal
Dushyant Bangal

Reputation: 6403

I just realized the question was posted 9 months ago. Anyways...

I think you are increasing the complications.

Here's how I did it:

// logger.js
var winston = require('winston');

var logger = new (winston.Logger)({
    transports: [
        new(winston.transports.File)({
            filename:'PATH_TO_LOG_FILE',
            handleExceptions: true,
            prettyPrint:true
        })
        // In case you need more transports uncomment:
        //,
        //new (winston.transports.Console)({
        //    level:'silly',
        //    handleExceptions: true,
        //    prettyPrint:true
        //})
    ],exitOnError:false
});

module.exports=logger;

for using logger in other files, just:

var logger = require("PATH_TO_LOGGER");
logger.error("Unable to do stuff",err);

Upvotes: 1

Related Questions