Skip to content

Instantly share code, notes, and snippets.

@aayushchugh
Created October 13, 2025 13:46
Show Gist options
  • Select an option

  • Save aayushchugh/41042edec0858fd44306b13315e85a8e to your computer and use it in GitHub Desktop.

Select an option

Save aayushchugh/41042edec0858fd44306b13315e85a8e to your computer and use it in GitHub Desktop.
Logger to dump logs in cloudwatch
import winston from "winston";
import "winston-daily-rotate-file";
import WinstonCloudWatch from "winston-cloudwatch";
import { env } from "../config/env";
const isProduction = env.NODE_ENV === "production";
const environment = env.NODE_ENV || (isProduction ? "production" : "development");
const isCloudwatchEnabled = env.NODE_ENV === "production" || env.NODE_ENV === "stage";
type LogModule =
| "system"
| "event"
| "organizer"
| "user"
| "auth"
| "http"
| "db"
| "session"
| "s3"
| "open-phone"
| "stripe"
| "upload"
| "common"
| "webhook"
| "payments"
| "ticket"
| "cart"
| "admin"
| "mailer"
| "marketing"
| "analytics"
| "team"
| "sockets"
| "otp"
| "trackingLink"
| "transactionTicketQrs";
interface LogMeta {
module: LogModule;
action: string;
userId?: string;
[key: string]: any;
}
/**
* Logger class
*
* This class is used to log messages to the console and to a file.
* It also logs to CloudWatch if the environment is production.
*
*/
class Logger {
private static instance: Logger;
private infoLogger: winston.Logger;
private errorLogger: winston.Logger;
private auditLogger: winston.Logger;
private warnLogger: winston.Logger;
private constructor() {
const baseCloudwatchConfig = {
awsRegion: env.AWS_REGION || "us-east-1",
awsAccessKeyId: env.AWS_ACCESS_KEY_ID,
awsSecretKey: env.AWS_SECRET_ACCESS_KEY,
jsonMessage: true,
};
// Info Logger (for general application logs) - 3 days retention
this.infoLogger = winston.createLogger({
level: isProduction ? "info" : "debug",
defaultMeta: { module: "system", type: "info" },
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json(),
),
transports: [
...(isCloudwatchEnabled
? [
new WinstonCloudWatch({
...baseCloudwatchConfig,
retentionInDays: 3,
logGroupName: `/avenue/${environment}/api`,
logStreamName: `info-${new Date().toISOString().split("T")[0]}`,
}),
]
: []),
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
}),
],
});
// Error Logger (for errors) - 7 days retention
this.errorLogger = winston.createLogger({
level: "error",
defaultMeta: { module: "system", type: "error" },
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json(),
),
transports: [
...(isCloudwatchEnabled
? [
new WinstonCloudWatch({
...baseCloudwatchConfig,
retentionInDays: 7,
logGroupName: `/avenue/${environment}/api`,
logStreamName: `error-${new Date().toISOString().split("T")[0]}`,
}),
]
: []),
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
}),
],
});
// Warn Logger - 7 days retention
this.warnLogger = winston.createLogger({
level: "warn",
defaultMeta: { module: "system", type: "warn" },
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json(),
),
transports: [
...(isCloudwatchEnabled
? [
new WinstonCloudWatch({
...baseCloudwatchConfig,
retentionInDays: 7,
logGroupName: `/avenue/${environment}/api`,
logStreamName: `warn-${new Date().toISOString().split("T")[0]}`,
}),
]
: []),
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
}),
],
});
// Audit Logger (for audit logs) - 1 year retention
this.auditLogger = winston.createLogger({
level: "info",
defaultMeta: { module: "system", type: "audit" },
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json(),
),
transports: [
...(isCloudwatchEnabled
? [
new WinstonCloudWatch({
...baseCloudwatchConfig,
retentionInDays: 365,
logGroupName: `/avenue/${environment}/api`,
logStreamName: `audit-${new Date().toISOString().split("T")[0]}`,
}),
]
: []),
new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
}),
],
});
}
/**
* Get the singleton instance of the Logger
* @returns The singleton instance of the Logger
*/
public static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}
/**
* Log an info message
* @param message The message to log
* @param meta The metadata to log
*/
public info(message: string, meta: LogMeta): void {
this.infoLogger.info(message, { ...meta, timestamp: new Date().toISOString() });
}
/**
* Log an error message
* @param message The message to log
* @param meta The metadata to log
*/
public error(message: string, meta: LogMeta): void {
this.errorLogger.error(message, { ...meta, timestamp: new Date().toISOString() });
}
/**
* Log an audit message
* @param message The message to log
* @param meta The metadata to log
*/
public audit(message: string, meta: LogMeta): void {
this.auditLogger.info(message, { ...meta, timestamp: new Date().toISOString() });
}
/**
* Log a warning message
* @param message The message to log
* @param meta The metadata to log
*/
public warn(message: string, meta: LogMeta) {
this.warnLogger.warn(message, { ...meta, timestamp: new Date().toISOString() });
}
}
// Export singleton instance
const logger = Logger.getInstance();
export default logger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment