Created
September 1, 2018 08:42
-
-
Save keidrun/28f2b53b534ac63f48673c6c4b659304 to your computer and use it in GitHub Desktop.
Morgan and Winston examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { morgan, LOG_FORMAT } = require('./morgan'); | |
| const logFormat = | |
| !process.env.NODE_ENV || process.env.NODE_ENV === 'development' | |
| ? 'dev' | |
| : LOG_FORMAT; | |
| module.exports = options => | |
| morgan(logFormat, { | |
| skip(req, res) { | |
| return res.statusCode >= 400; | |
| }, | |
| stream: process.stdout, | |
| ...options, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { morgan, LOG_FORMAT } = require('./morgan'); | |
| const logFormat = | |
| !process.env.NODE_ENV || process.env.NODE_ENV === 'development' | |
| ? 'dev' | |
| : LOG_FORMAT; | |
| module.exports = options => | |
| morgan(logFormat, { | |
| skip(req, res) { | |
| return res.statusCode < 400; | |
| }, | |
| stream: process.stderr, | |
| ...options, | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { createLogger, format, transports } = require('winston'); | |
| const level = process.env.LOG_LEVEL || 'debug'; | |
| const { combine, timestamp, label, printf } = format; | |
| const logFormat = printf( | |
| info => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`, | |
| ); | |
| const logger = loggerLabel => | |
| createLogger({ | |
| level, | |
| format: combine( | |
| label({ label: loggerLabel || 'default' }), | |
| timestamp(), | |
| logFormat, | |
| ), | |
| transports: [new transports.Console()], | |
| }); | |
| module.exports = logger; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const morgan = require('morgan'); | |
| morgan.token('id', req => req.id); | |
| // combined + req.id | |
| const LOG_FORMAT = | |
| ':remote-addr - :remote-user [:date[clf]] [:id] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'; | |
| module.exports = { morgan, LOG_FORMAT }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment