Node.js - register / use morgan and winston

we use morgan to register our express conversion:

 var morgan = require('morgan'); morgan('combined'); // a format string morgan(':remote-addr :method :url :uuid'); // a custom function morgan(function (req, res) { return req.method + ' ' + req.url + ' ' + req.uuid; }) 

In addition, we use winston to register our other journals:

 var winston = require('winston'); var logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ level: 'info' }), new (winston.transports.File)({ filename: '/var/log/log-file.log' }) ] }); 

Is there a way to combine the two registrars together? The situation now is that morgan writes to my standard output when winston writes to /var/log/log-file.log .

I would like the registrar file to be combined from the express conversion information and from the other information I want ( logger.info() ) ..

+78
logging express winston
Jan 12 '15 at 16:29
source share
5 answers

This article is great for what you want to do.

http://tostring.it/2014/06/23/advanced-logging-with-nodejs/

For your specific code, you probably need something like this:

 var logger = new winston.Logger({ transports: [ new winston.transports.File({ level: 'info', filename: './logs/all-logs.log', handleExceptions: true, json: true, maxsize: 5242880, //5MB maxFiles: 5, colorize: false }), new winston.transports.Console({ level: 'debug', handleExceptions: true, json: false, colorize: true }) ], exitOnError: false }), logger.stream = { write: function(message, encoding){ logger.info(message); } }; app.use(require("morgan")("combined", { "stream": logger.stream })); 

This will install Winston to write the log to the console as well as to the file. You can then use the last expression to pass the output from morgan middleware to winston.

+118
Mar 03 '15 at 5:03
source share

In Typescript:

 let logger = new (winston.Logger)({ exitOnError: false, level: 'info', transports: [ new (winston.transports.Console)(), new (winston.transports.File)({ filename: 'app.log'}) ] }) class MyStream { write(text: string) { logger.info(text) } } let myStream = new MyStream() app.use(morgan('tiny', { stream: myStream })); 
+15
Feb 13 '17 at 16:15
source share

Update last line to remove warning

 app.use(require("morgan")("combined", { stream: logger.stream })); 
+7
Aug 12 '16 at 18:15
source share

Morgan has a bad habit of ending the message \n therefore, to streamline the situation, you may want to remove this before writing it to Winston.

You can do this in many ways, for example, on the format side in winston, or by updating your stream so that you don’t write \n

 class MyStream { write(text: string) { logger.info(text.replace(/\n$/, ''); } } let myStream = new MyStream() app.use(morgan('tiny', { stream: myStream })); 
0
Jun 28 '19 at 19:41
source share

for Typescript another way to do this without having to create a class

 let logger = new (winston.Logger)({ exitOnError: false, level: 'info', transports: [ new (winston.transports.Console)(), new (winston.transports.File)({ filename: 'app.log'}) ] }) const myStream = { write: (text: string) => { logger.info(text) } } app.use(morgan('combined', { stream: myStream })); 

This solution was found on this Github page https://github.com/winstonjs/winston/issues/1385 . However, it is important to note that there is a slight difference between our codes. Instead:

 app.use(morgan('combined', { myStream })); 

I use:

 app.use(morgan('combined', { stream: myStream })); 

This helped me since I'm not too good at creating classes.

0
Jul 12 '19 at 16:10
source share



All Articles