Last active
May 10, 2018 21:09
-
-
Save vspedr/96a76a36bb761762ce477eebf49e5817 to your computer and use it in GitHub Desktop.
winston + morgan logging middleware
This file contains 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
import config from '../config'; | |
import winston from 'winston'; | |
const logger = new (winston.Logger)({ | |
level: config.log.level, | |
transports: [ | |
new winston.transports.Console({ | |
colorize: true, | |
timestamp: true, | |
}) | |
] | |
}); | |
export const getStream = (level) => ({ | |
write: (message) => logger[level](message.trim()) | |
}); | |
export default logger; |
This file contains 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
// HTTP request logging middleware | |
// This one handles successful responses (status < 400) | |
app.use(morgan('tiny', { | |
stream: getStream('info'), | |
skip: function (req, res) { | |
return res.statusCode >= 400 | |
}, | |
})); | |
// This one handles error responses (status >= 400) | |
app.use(morgan('tiny', { | |
stream: getStream('error'), | |
skip: function (req, res) { | |
return res.statusCode < 400 | |
}, | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment