@samofprog/nestjs-logstack
Version:
Configurable Winston logger for NestJS with daily file rotation and console output.
40 lines • 1.93 kB
JavaScript
import * as winston from "winston";
import "winston-daily-rotate-file";
import { WinstonModule } from "nest-winston";
import * as path from "path";
const logDir = path.join(process.cwd(), "logs");
export class LoggerUtils {
buildLogger(maxSize = "20m", maxFiles = "14d") {
return WinstonModule.createLogger(this.createDefaultWinstonConfig(maxSize, maxFiles));
}
createDefaultWinstonConfig(maxSize = "20m", maxFiles = "14d") {
return {
transports: [
this.createDailyRotateTransport("info", maxSize, maxFiles),
this.createDailyRotateTransport("warn", maxSize, maxFiles),
this.createDailyRotateTransport("error", maxSize, maxFiles),
new winston.transports.Console({
stderrLevels: ["error"],
format: winston.format.combine(winston.format.errors({ stack: true }), winston.format.timestamp(), winston.format.colorize({ all: true }), winston.format.printf(({ timestamp, level, message, stack, context }) => {
var _a;
const stackTrace = stack ? `\n${stack}` : "";
const ctx = context ? `[${context}] ` : "";
return `${timestamp} ${level}: ${ctx}${message} ${!((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.includes("prod")) ? stackTrace : ""}`;
})),
}),
],
};
}
createDailyRotateTransport(level, maxSize, maxFiles) {
return new winston.transports.DailyRotateFile({
level,
filename: `${logDir}/${level}-%DATE%.log`,
datePattern: "YYYY-MM-DD",
zippedArchive: true,
maxSize,
maxFiles,
format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
});
}
}
//# sourceMappingURL=index.js.map