@samofprog/nestjs-logstack
Version:
Configurable Winston logger for NestJS with daily file rotation and console output.
77 lines • 3.47 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggerUtils = void 0;
const winston = __importStar(require("winston"));
require("winston-daily-rotate-file");
const nest_winston_1 = require("nest-winston");
const path = __importStar(require("path"));
const logDir = path.join(process.cwd(), "logs");
class LoggerUtils {
buildLogger(maxSize = "20m", maxFiles = "14d") {
return nest_winston_1.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()),
});
}
}
exports.LoggerUtils = LoggerUtils;
//# sourceMappingURL=index.js.map