Created
February 1, 2021 23:35
-
-
Save mallendeo/5d7c4e4ce653fc15e63ae89b532c9013 to your computer and use it in GitHub Desktop.
Example winston configuration with file rotation
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
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