瀏覽代碼

only create each logger once

Ben Phelps 2 年之前
父節點
當前提交
b72dca0e2e
共有 1 個文件被更改,包括 23 次插入17 次删除
  1. 23 17
      src/utils/logger.js

+ 23 - 17
src/utils/logger.js

@@ -1,6 +1,6 @@
 /* eslint-disable no-console */
 import { join } from "path";
-import { format as utilFormat } from "node:util"
+import { format as utilFormat } from "node:util";
 
 import winston from "winston";
 
@@ -15,10 +15,10 @@ function init() {
       transform: (info, opts) => {
         // combine message and args if any
         // eslint-disable-next-line no-param-reassign
-        info.message =  utilFormat(info.message, ...info[Symbol.for('splat')]  ||  []);
+        info.message = utilFormat(info.message, ...(info[Symbol.for("splat")] || []));
         return info;
-      }
-    }
+      },
+    };
   }
 
   function messageFormatter(logInfo) {
@@ -33,56 +33,62 @@ function init() {
       return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
     }
     return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
-  };
+  }
 
   winstonLogger = winston.createLogger({
-    level: process.env.LOG_LEVEL || 'info',
+    level: process.env.LOG_LEVEL || "info",
     transports: [
       new winston.transports.Console({
         format: winston.format.combine(
-          winston.format.errors({ stack: true}),
+          winston.format.errors({ stack: true }),
           combineMessageAndSplat(),
           winston.format.timestamp(),
           winston.format.colorize(),
           winston.format.printf(messageFormatter)
         ),
         handleExceptions: true,
-        handleRejections: true
+        handleRejections: true,
       }),
 
       new winston.transports.File({
         format: winston.format.combine(
-          winston.format.errors({ stack: true}),
+          winston.format.errors({ stack: true }),
           combineMessageAndSplat(),
           winston.format.timestamp(),
           winston.format.printf(messageFormatter)
         ),
         filename: `${configPath}/logs/homepage.log`,
         handleExceptions: true,
-        handleRejections: true
+        handleRejections: true,
       }),
-    ]
+    ],
   });
 
   // patch the console log mechanism to use our logger
-  const consoleMethods = ['log', 'debug', 'info', 'warn', 'error']
-  consoleMethods.forEach(method => {
+  const consoleMethods = ["log", "debug", "info", "warn", "error"];
+  consoleMethods.forEach((method) => {
     // workaround for https://github.com/winstonjs/winston/issues/1591
     switch (method) {
-      case 'log':
+      case "log":
         console[method] = winstonLogger.info.bind(winstonLogger);
         break;
       default:
         console[method] = winstonLogger[method].bind(winstonLogger);
         break;
     }
-  })
+  });
 }
 
+const loggers = {};
+
 export default function createLogger(label) {
   if (!winstonLogger) {
     init();
   }
 
-  return winstonLogger.child({ label });
-}
+  if (!loggers[label]) {
+    loggers[label] = winstonLogger.child({ label });
+  }
+
+  return loggers[label];
+}