Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - logging / Use morgan and winston

we use morgan in order to log our express transformation:

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

Also, we use winston in order to log our other logging:

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 any way to combine the two loggers together? the situation now is that morgan is write to my standard output, when winston writes to /var/log/log-file.log.

I wish that the logger file will combine from the express transformation information, and from the other information I want (logger.info())..

like image 918
Or Smith Avatar asked Jan 12 '15 16:01

Or Smith


People also ask

How do you use Morgan and Winston?

use(require("morgan")("combined", { "stream": logger. stream })); This will set up Winston to write a log to the console as well as a file. Then you can use the last expression to pass output from the morgan middleware into winston.

Why Morgan is used in Node js?

Morgan: Morgan is an HTTP request level Middleware. It is a great tool that logs the requests along with some other information depending upon its configuration and the preset used. It proves to be very helpful while debugging and also if you want to create Log files. Prerequisites: Basic understanding of Nodejs.


2 Answers

This article does an excellent job 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 set up Winston to write a log to the console as well as a file. Then you can use the last expression to pass output from the morgan middleware into winston.

like image 182
lindsaymacvean Avatar answered Oct 01 '22 17:10

lindsaymacvean


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 })); 
like image 33
Mahyar SEPEHR Avatar answered Oct 01 '22 19:10

Mahyar SEPEHR