feat: add configuration tunable for logging enablement
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 31 May 2023 21:52:33 +0000 (23:52 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 31 May 2023 21:52:33 +0000 (23:52 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
README.md
src/types/ConfigurationData.ts
src/utils/Configuration.ts
src/utils/Logger.ts

index 91ebf65cb91d004029dd845fe7a5b1e9da368049..e3a202ea7226121850ab5060f091b162188ca3bf 100644 (file)
--- a/README.md
+++ b/README.md
@@ -103,6 +103,7 @@ But the modifications to test have to be done to the files in the build target d
 | supervisionUrls            |                                                  | []                                                                                                                                                                                                            | string \| string[]                                                                                                                                                                                                                  | string or array of global connection URIs to OCPP-J servers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
 | supervisionUrlDistribution | round-robin/random/charging-station-affinity     | charging-station-affinity                                                                                                                                                                                     | boolean                                                                                                                                                                                                                             | supervision urls distribution policy to simulated charging stations                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
 | logStatisticsInterval      |                                                  | 60                                                                                                                                                                                                            | integer                                                                                                                                                                                                                             | seconds between charging stations statistics output in the logs                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
+| logEnabled                 | true/false                                       | true                                                                                                                                                                                                          | boolean                                                                                                                                                                                                                             | enable logging                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
 | logConsole                 | true/false                                       | false                                                                                                                                                                                                         | boolean                                                                                                                                                                                                                             | output logs on the console                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
 | logFormat                  |                                                  | simple                                                                                                                                                                                                        | string                                                                                                                                                                                                                              | [winston](https://github.com/winstonjs/winston) log format                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
 | logRotate                  | true/false                                       | true                                                                                                                                                                                                          | boolean                                                                                                                                                                                                                             | enable daily log files rotation                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
index 276feabd0d8701191008289ac135bd9dc4658665..a23940cf330295cb609f28b7a3439ee05e26f6f6 100644 (file)
@@ -70,6 +70,8 @@ export type ConfigurationData = {
   /** @deprecated Moved to worker configuration section. */
   chargingStationsPerWorker?: number;
   logStatisticsInterval?: number;
+  logEnabled?: boolean;
+  logConsole?: boolean;
   logFormat?: string;
   logLevel?: string;
   logRotate?: boolean;
@@ -77,5 +79,4 @@ export type ConfigurationData = {
   logMaxSize?: number | string;
   logFile?: string;
   logErrorFile?: string;
-  logConsole?: boolean;
 };
index 6f53ef3bd92e796d7f9be4fd049df4b88f8be6d9..b67afd447ae0f2ebc7a6f80f53bdfa5f57dfdf8f 100644 (file)
@@ -234,6 +234,12 @@ export class Configuration {
     return Configuration.getWorker().processType === WorkerProcessType.dynamicPool;
   }
 
+  public static getLogEnabled(): boolean | undefined {
+    return Utils.hasOwnProp(Configuration.getConfig(), 'logEnabled')
+      ? Configuration.getConfig()?.logEnabled
+      : true;
+  }
+
   public static getLogConsole(): boolean | undefined {
     Configuration.warnDeprecatedConfigurationKey(
       'consoleLog',
index e2b2da5a581b653421d81001a14386aa145033b2..637beb5bd533bffe6f0fb91cc8124bef730ce381 100644 (file)
@@ -1,5 +1,5 @@
 import type { FormatWrap } from 'logform';
-import { type Logger, createLogger, format, type transport } from 'winston';
+import { createLogger, format, type transport } from 'winston';
 import TransportType from 'winston/lib/winston/transports/index.js';
 import DailyRotateFile from 'winston-daily-rotate-file';
 
@@ -38,7 +38,8 @@ if (Configuration.getLogRotate() === true) {
   ];
 }
 
-export const logger: Logger = createLogger({
+export const logger = createLogger({
+  silent: !Configuration.getLogEnabled(),
   level: Configuration.getLogLevel(),
   format: format.combine(format.splat(), (format[Configuration.getLogFormat()] as FormatWrap)()),
   transports,