Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Winston in several modules?

I have several modules - let's say server.js, module1.js,...,moduleN.js.

I would like define the log file in my server.js:

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

and then use it in all my modules.

What is the best way to do that? I could exports.winston=winston; in each module and then set it in the server.js, but is there any better solution?

Thank you in advance!

like image 851
Alexander Avatar asked Jan 25 '13 22:01

Alexander


People also ask

Is Winston a middleware?

Usage. express-winston provides middlewares for request and error logging of your express. js application. It uses 'whitelists' to select properties from the request and (new in 0.2.

How do you implement Winston in node JS?

Here's a guide that will help you understand NPM in detail. Install the Winston package using the command npm install winston . Logging with Winston is simple, with just four steps, as shown in the example below, and you have your log recorded. Add the Winston module with a require() function.

Is Winston logger synchronous?

After a quick review of the source on github, I'd say that no, winston is not truly async in all aspects. It does emit , but EventEmitter is not async. Methods have an async-style signature (w/ callback ), but are not always async.

What is Winston logs?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).


2 Answers

The default logger concept handles this nicely.

Winston defines a default logger that any straight require (and subsequent require) to winston will retrieve. Thus you simply configure this default logger once, and it's available for subsequent module use via vanilla require('winston') in its glorious tweaked multi-transport mode.

e.g. here is my complete logging setup that defines 3 transports. I swap Loggly for MongoDB sometimes.

server.js

var logger=require('./log.js');  // requires winston and configures transports for winstons default logger- see code below. 

all other .js files

var logger=require('winston'); // this retrieves default logger which was configured in log.js logger.info("the default logger with my tricked out transports is rockin this module"); 

log.js - this is a one time configuration of the DEFAULT logger

var logger = require('winston'); var Loggly = require('winston-loggly').Loggly; var loggly_options={ subdomain: "mysubdomain", inputToken: "efake000-000d-000e-a000-xfakee000a00" } logger.add(Loggly, loggly_options); logger.add(winston.transports.File, { filename: "../logs/production.log" }); logger.info('Chill Winston, the logs are being captured 3 ways- console, file, and Loggly'); module.exports=logger; 

Alternatively for more complex scenarios you can use winston containers and retrieve the logger from a named container in other modules. I haven't used this.

My only issue with this was a missing logs directories on my deployment host which was easily fixed.

Hope this helps.

like image 158
Nick Avatar answered Sep 28 '22 07:09

Nick


What I do ( which may not be the best way ) is use a 'global' module where I export all the stuff that I use through my applications. For instance:

//Define your winston instance winston.add(winston.transports.File, { filename: 'mylogfile.log' }); exports.logger = winston;  exports.otherGlobals = .... 

Now just require this globally used module from your other modules

var Global = require(/path/to/global.js); 

Because the file is cached after the first time it is loaded (which you can verify by including a log statement in your global; it will only log once), there's very little overhead in including it again. Putting it all into one file is also easier than requiring ALL your globally used modules on every page.

like image 40
Nick Mitchinson Avatar answered Sep 28 '22 08:09

Nick Mitchinson