Created
July 15, 2025 17:22
-
-
Save gameshler/cfa0a544c1db216bd70e5a134e6c1f3d to your computer and use it in GitHub Desktop.
A clean, production-ready logger using Winston in TypeScript. Supports colorized console logging in development and daily log rotation in production.
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
| /** | |
| * Usage: | |
| * import { logger } from './logger'; | |
| * logger.info("Server started"); | |
| * logger.error("Something went wrong"); | |
| */ | |
| import { createLogger, format, transports } from "winston"; | |
| import "winston-daily-rotate-file"; | |
| import path from "path"; | |
| const NODE_ENV = process.env.NODE_ENV | |
| const logFormat = format.combine( | |
| format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), | |
| format.printf(({ level, message, timestamp }) => { | |
| return `${timestamp} [${level.toUpperCase()}]: ${message}`; | |
| }) | |
| ); | |
| export const logger = createLogger({ | |
| level: NODE_ENV === "production" ? "info" : "debug", | |
| format: logFormat, | |
| transports: [ | |
| new transports.Console({ | |
| format: format.combine(format.colorize(), logFormat), | |
| }), | |
| new transports.DailyRotateFile({ | |
| dirname: path.resolve("logs"), | |
| filename: "%DATE%-app.log", | |
| datePattern: "YYYY-MM-DD", | |
| maxSize: "20m", | |
| maxFiles: "14d", | |
| zippedArchive: true, | |
| }), | |
| ], | |
| exitOnError: false, | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment