Skip to content

Instantly share code, notes, and snippets.

@objectfoo
Created September 11, 2023 15:34
Show Gist options
  • Select an option

  • Save objectfoo/c2f915ae2b0555b710dee10f42c273ea to your computer and use it in GitHub Desktop.

Select an option

Save objectfoo/c2f915ae2b0555b710dee10f42c273ea to your computer and use it in GitHub Desktop.
import * as winston from "winston";
import path from "path";
import DailyRotateFile from "winston-daily-rotate-file";
const { json, timestamp, combine } = winston.format;
const { Console } = winston.transports;
export default function CreateLogger(appName: string, nodeEnv: string, logFolder: string): winston.Logger {
const dailyRotateFile: DailyRotateFile = new DailyRotateFile({
filename: path.join(logFolder, `ui-${appName}-%DATE%.log`),
datePattern: "YYYY-MM-DD-HH",
maxSize: "20m",
maxFiles: "14d",
auditFile: path.join(logFolder, `ui-${appName}-audit.json`),
format: combine(timestamp(), json()),
});
// do something when the files rotate
// dailyRotateFile.on("rotate", function(oldFilename: string, newFilename: string) {
// console.log(oldFilename, newFilename);
// });
const transports: winston.transport[] = [
dailyRotateFile
];
// add "test" to provide a way to disable testing spam
if (nodeEnv !== "production" && nodeEnv !== "test") {
const consoleTransport = new Console({ format: winston.format.simple() });
transports.push(consoleTransport);
}
return winston.createLogger({
defaultMeta: { service: `ui-${appName}` },
transports,
exceptionHandlers: transports,
rejectionHandlers: transports,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment