import { config, isTest } from "./config"; import winston, { LoggerOptions } from "winston"; const loggerOptions: LoggerOptions = { level: config.LOG_LEVEL, format: winston.format.json(), transports: [new winston.transports.Console()], silent: isTest() && !process.env.FORCE_TEST_LOGGING, }; const internalLogger = winston.createLogger(loggerOptions); const loggerFn = (level: string) => (msg: string | Record) => internalLogger.log(level, msg); const logger = { debug: (msgFn: () => string | Record) => { if (config.LOG_LEVEL !== "debug") { return internalLogger; } const msg = msgFn(); return internalLogger.log("debug", msg); }, info: loggerFn("info"), warn: loggerFn("warn"), error: (err: Error, msg?: string | Record) => { const errMsg = err.toString(); const logObj: any = { error: errMsg, stackTrace: err.stack, errorCode: (err as any).code, }; if (typeof msg === "string") { logObj.message = msg; } else { Object.assign(logObj, msg); } logObj.message ??= errMsg; return internalLogger.log("error", logObj); }, }; process.on("uncaughtException", (err: any) => { logger.error(err, "uncaughtException"); process.exit(1); }); process.on("unhandledRejection", (err: any) => { logger.error(err, "unhandledRejection"); process.exit(1); }); export default logger;