Files
Online-Topup-V1/application/helpers/winston.logger.js
T

142 lines
4.1 KiB
JavaScript

const moment = require("moment");
const winston = require("winston");
const { formatLog, writeStdoutLine } = require("./stdout-log");
require("winston-daily-rotate-file");
require("dotenv").config();
const { combine, timestamp, printf, colorize, align, json } = winston.format;
function stdoutMessage(message) {
if (typeof message !== "string") return "Application log";
// Legacy log calls append whole payloads after a separator.
const payloadStart = message.search(/[:|]\s+(?:\{|\[\s*[{\]"\d]|<\?xml\b)/i);
const summary = payloadStart === -1 ? message : message.slice(0, payloadStart);
return summary.trim().replace(/\bBearer\s+[\w.+/=-]+/gi, "Bearer [redacted]");
}
class StdoutTransport extends winston.Transport {
log(info, done) {
writeStdoutLine(formatLog({
level: info[Symbol.for("level")],
event: "application_log",
service: info.service,
message: stdoutMessage(info.message),
}));
done();
}
}
const logLevels = {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5,
};
var logger;
//
// Replaces the previous transports with those in the
// new configuration wholesale.
//
// const DailyRotateFile = require("winston-daily-rotate-file");
// logger.configure({
// level: "verbose",
// transports: [new DailyRotateFile()],
// });
const fileRotateTransport = new winston.transports.DailyRotateFile({
filename: `application/logs/%DATE%-onlinetopup.log`,
datePattern: "YYYY-MM-DD",
maxFiles: "60d",
});
const fileRotateTransportDebug = new winston.transports.DailyRotateFile({
filename: `application/logs/debug/%DATE%-onlinetopup.log`,
datePattern: "YYYY-MM-DD",
maxFiles: "30d",
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (
process.env.NODE_ENV == "production" ||
process.env.NODE_ENV == "PRODUCTION"
) {
logger = winston.createLogger({
levels: logLevels,
level: process.env.LOG_LEVEL || "info",
exitOnError: false,
// format: combine(timestamp(), json()),
format: combine(
// colorize({ all: true }),
timestamp({
format: "YYYY-MM-DD HH:mm:ss.SSS",
}),
align(),
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
),
defaultMeta: { service: "topuponline-service" },
transports: [
fileRotateTransport,
fileRotateTransportDebug,
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `onlinetopup.log`
//
new winston.transports.File({
filename: `application/logs/error.log`,
level: "error",
}),
// new winston.transports.File({
// filename: `application/logs/${moment().format("YYYY-MM-DD")}-onlinetopup.log`,
// }),
],
});
} else {
logger = winston.createLogger({
levels: logLevels,
level: process.env.LOG_LEVEL || "debug",
exitOnError: false,
// format: combine(timestamp(), json()),
format: combine(
// colorize({ all: true }),
timestamp({
format: "YYYY-MM-DD HH:mm:ss.SSS",
}),
align(),
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`)
),
defaultMeta: { service: "topuponline-service" },
transports: [
fileRotateTransport,
fileRotateTransportDebug,
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `onlinetopup.log`
//
new winston.transports.File({
filename: `application/logs/error.log`,
level: "error",
}),
new winston.transports.File({
filename: `application/logs/debug/${moment().format("YYYY-MM-DD")}.log`,
level: "debug",
}),
// new winston.transports.File({
// filename: `application/logs/${moment().format("YYYY-MM-DD")}-onlinetopup.log`,
// }),
],
});
}
if (process.env.NODE_ENV != "testing" && process.env.NODE_ENV != "TESTING") {
logger.add(new StdoutTransport());
}
module.exports = {
logger,
};