]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: cleanup logger singleton initialization
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 17:09:35 +0000 (19:09 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 17:09:35 +0000 (19:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/utils/Logger.ts

index 8ee1d8abdf94ccfee707948526900b54394c3508..e8b3af6450e5a3d64ee042413fc3c3677a72ccdd 100644 (file)
@@ -1,60 +1,70 @@
 import type { FormatWrap } from 'logform'
 
-import { createLogger, format, type transport } from 'winston'
+import { createLogger, format, type transport, transports as WinstonTransports } from 'winston'
 import DailyRotateFile from 'winston-daily-rotate-file'
-import TransportType from 'winston/lib/winston/transports/index.js'
 
 import { ConfigurationSection, type LogConfiguration } from '../types/index.js'
 import { Configuration } from './Configuration.js'
-import { insertAt } from './Utils.js'
+import { insertAt, isNotEmptyString } from './Utils.js'
 
 const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>(
   ConfigurationSection.log
 )
-let transports: transport[]
+const transports: transport[] = []
 if (logConfiguration.rotate === true) {
   const logMaxFiles = logConfiguration.maxFiles
   const logMaxSize = logConfiguration.maxSize
-  transports = [
-    new DailyRotateFile({
-      filename: insertAt(
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        logConfiguration.errorFile!,
-        '-%DATE%',
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        logConfiguration.errorFile!.indexOf('.log')
-      ),
-      level: 'error',
-      ...(logMaxFiles != null && { maxFiles: logMaxFiles }),
-      ...(logMaxSize != null && { maxSize: logMaxSize }),
-    }),
-    new DailyRotateFile({
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      filename: insertAt(logConfiguration.file!, '-%DATE%', logConfiguration.file!.indexOf('.log')),
-      ...(logMaxFiles != null && { maxFiles: logMaxFiles }),
-      ...(logMaxSize != null && { maxSize: logMaxSize }),
-    }),
-  ]
+  if (isNotEmptyString(logConfiguration.errorFile)) {
+    transports.push(
+      new DailyRotateFile({
+        filename: insertAt(
+          logConfiguration.errorFile,
+          '-%DATE%',
+          logConfiguration.errorFile.indexOf('.log')
+        ),
+        level: 'error',
+        ...(logMaxFiles != null && { maxFiles: logMaxFiles }),
+        ...(logMaxSize != null && { maxSize: logMaxSize }),
+      })
+    )
+  }
+  if (isNotEmptyString(logConfiguration.file)) {
+    transports.push(
+      new DailyRotateFile({
+        filename: insertAt(logConfiguration.file, '-%DATE%', logConfiguration.file.indexOf('.log')),
+        ...(logMaxFiles != null && { maxFiles: logMaxFiles }),
+        ...(logMaxSize != null && { maxSize: logMaxSize }),
+      })
+    )
+  }
 } else {
-  transports = [
-    new TransportType.File({
-      filename: logConfiguration.errorFile,
-      level: 'error',
-    }),
-    new TransportType.File({
-      filename: logConfiguration.file,
-    }),
-  ]
+  if (isNotEmptyString(logConfiguration.errorFile)) {
+    transports.push(
+      new WinstonTransports.File({
+        filename: logConfiguration.errorFile,
+        level: 'error',
+      })
+    )
+  }
+  if (isNotEmptyString(logConfiguration.file)) {
+    transports.push(
+      new WinstonTransports.File({
+        filename: logConfiguration.file,
+      })
+    )
+  }
 }
 
+const logFormat = format.combine(
+  format.splat(),
+  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+  (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)()
+)
+
 const logger = createLogger({
-  format: format.combine(
-    format.splat(),
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)()
-  ),
+  format: logFormat,
   level: logConfiguration.level,
-  silent: logConfiguration.enabled === false,
+  silent: logConfiguration.enabled === false || transports.length === 0,
   transports,
 })
 
@@ -64,12 +74,8 @@ const logger = createLogger({
 //
 if (logConfiguration.console === true) {
   logger.add(
-    new TransportType.Console({
-      format: format.combine(
-        format.splat(),
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)()
-      ),
+    new WinstonTransports.Console({
+      format: logFormat,
     })
   )
 }