Last active
May 19, 2025 02:02
-
-
Save syehoonkim/68ee4084cd0d57f1dbc3ef3861de6bd1 to your computer and use it in GitHub Desktop.
Node.js WInston logger
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
| const winston = require("winston"); | |
| const winstonDaily = require("winston-daily-rotate-file"); | |
| const appRoot = require("app-root-path"); | |
| const { createLogger } = require("winston"); | |
| const logDir = `${appRoot}/logs`; | |
| const { combine, timestamp, label, printf } = winston.format; | |
| const logFormat = printf(({ level, message, label, timestamp }) => { | |
| return `${timestamp} [${label}] ${level}: ${message}`; | |
| }); | |
| const datePattern = "YYYY-MM-DD"; | |
| const filename = "%DATE%.log"; | |
| const maxSize = "20m"; | |
| const maxFiles = "3d"; | |
| const logger = createLogger({ | |
| format: combine( | |
| label({ label: "CAPTION_EXTRACTOR" }), | |
| timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), | |
| logFormat | |
| ), | |
| transports: [ | |
| new winstonDaily({ | |
| level: "info", | |
| datePattern: datePattern, | |
| dirname: logDir, | |
| filename: filename, | |
| maxSize: maxSize, | |
| maxFiles: maxFiles, | |
| }), | |
| new winstonDaily({ | |
| level: "error", | |
| datePattern: datePattern, | |
| dirname: logDir, | |
| filename: filename, | |
| maxSize: maxSize, | |
| maxFiles: maxFiles, | |
| }), | |
| ], | |
| }); | |
| module.exports = logger; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment