From: Jérôme Benoit Date: Wed, 31 May 2023 21:52:33 +0000 (+0200) Subject: feat: add configuration tunable for logging enablement X-Git-Tag: v1.2.16~26 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ae389044af7cfc9661bc53658ea405f4f1ae84ae;p=e-mobility-charging-stations-simulator.git feat: add configuration tunable for logging enablement Signed-off-by: Jérôme Benoit --- diff --git a/README.md b/README.md index 91ebf65c..e3a202ea 100644 --- 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 | diff --git a/src/types/ConfigurationData.ts b/src/types/ConfigurationData.ts index 276feabd..a23940cf 100644 --- a/src/types/ConfigurationData.ts +++ b/src/types/ConfigurationData.ts @@ -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; }; diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 6f53ef3b..b67afd44 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -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', diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index e2b2da5a..637beb5b 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -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,