Skip to content

Instantly share code, notes, and snippets.

@vassalloandrea
Last active January 12, 2021 06:17
Show Gist options
  • Save vassalloandrea/2867f35028dc7e28eea82677ba1172ba to your computer and use it in GitHub Desktop.
Save vassalloandrea/2867f35028dc7e28eea82677ba1172ba to your computer and use it in GitHub Desktop.
Winston logger configuration
import winston from 'winston'
const levels = {
error: 0,
warn: 1,
info: 2,
http: 3,
debug: 4,
}
const level = () => {
const env = process.env.NODE_ENV || 'development'
const isDevelopment = env === 'development'
return isDevelopment ? 'debug' : 'warn'
}
const colors = {
error: 'red',
warn: 'yellow',
info: 'green',
http: 'magenta',
debug: 'white',
}
winston.addColors(colors)
const format = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
winston.format.colorize({ all: true }),
winston.format.printf(
(info) => `${info.timestamp} ${info.level}: ${info.message}`,
),
)
const transports = [
new winston.transports.Console(),
new winston.transports.File({
filename: 'logs/error.log',
level: 'error',
}),
new winston.transports.File({ filename: 'logs/all.log' }),
]
const Logger = winston.createLogger({
level: level(),
levels,
format,
transports,
})
export default Logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment