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; 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: "http://localhost:3100", labels: { app: "topuponline" }, json: true, format: json(), replaceTimestamp: true, onConnectionError: (err) => console.error(err), }), new transports.Console({ format: format.combine(format.simple(), format.colorize()), }), ], }); } else { log = winston.createLogger({ transports: [ new loki({ host: "http://localhost:3100", 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, };