Files
Online-Topup-V1/application/helpers/loki.js
T
2026-09-14 14:10:53 +07:00

97 lines
2.3 KiB
JavaScript

const moment = require("moment");
const winston = require("winston");
const loki = require("winston-loki");
require("winston-daily-rotate-file");
require("dotenv").config();
const { combine, timestamp, printf, colorize, align, json } = winston.format;
let lokiUrl = process.env.LOKI_URL;
const logLevels = {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5,
};
var log;
const fileRotateTransport = new winston.transports.DailyRotateFile({
filename: `./lokiLog/%DATE%-onlinetopup.log`,
datePattern: "YYYY-MM-DD",
maxFiles: "1d",
});
if (
process.env.NODE_ENV == "production" ||
process.env.NODE_ENV == "PRODUCTION"
) {
log = 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" },
transports: [
new loki({
host: lokiUrl,
labels: { app: "topuponline" },
json: true,
format: json(),
replaceTimestamp: true,
onConnectionError: (err) => console.error(err),
}),
new winston.transports.Console({
format: combine(colorize()),
}),
],
});
} else {
log = winston.createLogger({
transports: [
new loki({
host: lokiUrl,
labels: { app: "topuponline" },
handleExceptions: true,
handleRejections: true,
clearOnError: true,
json: true,
format: json(),
replaceTimestamp: true,
onConnectionError: (err) => console.log(`Loki error ${err}`),
}),
new winston.transports.Console({
format: combine(colorize()),
}),
],
});
}
if (process.env.NODE_ENV != "testing" && process.env.NODE_ENV != "TESTING") {
log.add(
new winston.transports.Console({
// format: winston.format.cli(),
format: combine(
colorize({ all: true }),
timestamp({
format: "YYYY-MM-DD HH:mm:ss.SSS",
}),
align(),
printf((info) => `[${info.timestamp}] ${info.level}: ${info.message}`),
),
}),
);
}
module.exports = {
log,
};