Skip to content

Instantly share code, notes, and snippets.

@mallendeo
Created February 1, 2021 23:35
Show Gist options
  • Save mallendeo/5d7c4e4ce653fc15e63ae89b532c9013 to your computer and use it in GitHub Desktop.
Save mallendeo/5d7c4e4ce653fc15e63ae89b532c9013 to your computer and use it in GitHub Desktop.
Example winston configuration with file rotation
import 'winston-daily-rotate-file'
import stringify from 'json-stringify-safe'
import path from 'path'
import winston from 'winston'
import config from '@/config'
const dailyRotateTransport = new winston.transports.DailyRotateFile({
filename: path.join(config.logBase, `${config.appName}-%DATE%.log`),
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '30m',
maxFiles: '30d',
})
const prettyJson = winston.format.printf((info) => {
if (typeof info.message === 'object') {
info.message = stringify(info.message, null, 4)
}
return `[${config.appName}] [${info.timestamp}] ${info.level}: ${info.message}`
})
export const logger = winston.createLogger({
transports: [
new winston.transports.File({
level: 'info',
filename: path.join(config.logBase, `${config.appName}.log`),
handleExceptions: true,
maxsize: 10485760,
maxFiles: 1,
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
}),
dailyRotateTransport,
],
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.prettyPrint(),
winston.format.splat(),
winston.format.simple(),
winston.format.json(),
prettyJson,
),
exitOnError: false,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment