Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vspedr
Last active May 10, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vspedr/96a76a36bb761762ce477eebf49e5817 to your computer and use it in GitHub Desktop.
Save vspedr/96a76a36bb761762ce477eebf49e5817 to your computer and use it in GitHub Desktop.
winston + morgan logging middleware
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;
// 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