Skip to content

Instantly share code, notes, and snippets.

@syehoonkim
Last active May 19, 2025 02:02
Show Gist options
  • Select an option

  • Save syehoonkim/68ee4084cd0d57f1dbc3ef3861de6bd1 to your computer and use it in GitHub Desktop.

Select an option

Save syehoonkim/68ee4084cd0d57f1dbc3ef3861de6bd1 to your computer and use it in GitHub Desktop.
Node.js WInston logger
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