bithavoc / express-winston

express.js middleware for winstonjs
https://www.npmjs.com/package/express-winston
MIT License
796 stars 187 forks source link

Add support using existing winston logger instance #205

Closed DManavi closed 5 years ago

DManavi commented 5 years ago

Hi,

I want to use this package in one of my projects. I've already created and configured the winston logger with my prefered configuration and transports.

Now, I need a method that creates a middleware to log request/response and errors using the instance of the logger that created before.

rosston commented 5 years ago

I may be misunderstanding what you're asking for, but express-winston already supports the winstonInstance option on both logger and errorLogger. Does that do what you want?

DManavi commented 5 years ago

English is not my mother tongue, please excuse any errors on my part.

I mean that I've created an instance of the winston logger before (Some lines above of app.use(expressWinston....). Now, I want to now that is it possible to pass this instance to the middleware? Not this way app.use(expressWinston....{ configuration-goes-here });

rosston commented 5 years ago

Yes, I believe what you want is possible. Here is some rough example code that seems like it does what you want:

const express = require('express');
const expressWinston = require('express-winston');
const winston = require('winston');

const app = express();
const router = express.Router();
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

app.use(expressWinston.logger({
  winstonInstance: logger
}));

app.use(router);

app.use(expressWinston.errorLogger({
  winstonInstance: logger
}));
DManavi commented 5 years ago

Great! Thats it. I think it's better to put this example in Readme.md file.