parent
f90f47aa54
commit
742c543600
@ -0,0 +1,19 @@ |
|||||||
|
import winston from 'winston'; |
||||||
|
|
||||||
|
const logger = winston.createLogger({ |
||||||
|
level: 'info', |
||||||
|
format: winston.format.json(), |
||||||
|
defaultMeta: { service: 'user-service' }, |
||||||
|
transports: [ |
||||||
|
new winston.transports.File({ filename: 'error.log', level: 'error' }), |
||||||
|
new winston.transports.File({ filename: 'combined.log' }) |
||||||
|
] |
||||||
|
}); |
||||||
|
|
||||||
|
// If we're not in production, log to the `console` with the format:
|
||||||
|
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
|
||||||
|
if (process.env.NODE_ENV !== 'production') { |
||||||
|
logger.add(new winston.transports.Console({ |
||||||
|
format: winston.format.simple() |
||||||
|
})); |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
import expressWinston from 'express-winston'; |
||||||
|
|
||||||
|
// Request logger middleware
|
||||||
|
app.use(expressWinston.logger({ |
||||||
|
transports: [ |
||||||
|
new winston.transports.Console() |
||||||
|
], |
||||||
|
format: winston.format.combine( |
||||||
|
winston.format.colorize(), |
||||||
|
winston.format.json() |
||||||
|
), |
||||||
|
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
|
||||||
|
msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
|
||||||
|
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
|
||||||
|
colorize: false, // Color the text and status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red).
|
||||||
|
ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
|
||||||
|
})); |
||||||
|
|
||||||
|
// Your routes go here
|
||||||
|
|
||||||
|
// Error logger middleware (should be after all your routes)
|
||||||
|
app.use(expressWinston.errorLogger({ |
||||||
|
transports: [ |
||||||
|
new winston.transports.Console() |
||||||
|
], |
||||||
|
format: winston.format.combine( |
||||||
|
winston.format.colorize(), |
||||||
|
winston.format.json() |
||||||
|
) |
||||||
|
})); |
Loading…
Reference in new issue