X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FLogger.ts;h=7d80e1aece51349349abd0766f8af5ae2b196833;hb=2665ed1ef62a9fc9b6eec417f3ec7c33305789cf;hp=736e9b89732f73435683c014fca1fd6b2ce909c4;hpb=6af9012e5b9ef2ed6f4fe8a9696b40ac0e8da4d0;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index 736e9b89..7d80e1ae 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -1,27 +1,74 @@ -import Configuration from './Configuration'; -import Winston from 'winston'; +import type { FormatWrap } from 'logform' +import { createLogger, format, type transport } from 'winston' +import TransportType from 'winston/lib/winston/transports/index.js' +import DailyRotateFile from 'winston-daily-rotate-file' -const logger = Winston.createLogger({ - level: Configuration.getLogLevel(), - format: Winston.format.combine(Winston.format.splat(), Winston.format[Configuration.getLogFormat()]()), - transports: [ - // - // - Write to all logs with level `info` and below to `combined.log` - // - Write all logs error (and below) to `error.log`. - // - new Winston.transports.File({ filename: Configuration.getErrorFile(), level: 'error' }), - new Winston.transports.File({ filename: Configuration.getLogFile() }), - ], -}); +import { Configuration } from './Configuration.js' +import { insertAt } from './Utils.js' +import { ConfigurationSection, type LogConfiguration } from '../types/index.js' + +const logConfiguration = Configuration.getConfigurationSection( + ConfigurationSection.log +) +let 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 }) + }) + ] +} else { + transports = [ + new TransportType.File({ + filename: logConfiguration.errorFile, + level: 'error' + }), + new TransportType.File({ + filename: logConfiguration.file + }) + ] +} + +export const logger = createLogger({ + silent: logConfiguration.enabled === false, + level: logConfiguration.level, + format: format.combine( + format.splat(), + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)() + ), + transports +}) // // If enabled, log to the `console` with the format: // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` // -if (Configuration.getConsoleLog()) { - logger.add(new Winston.transports.Console({ - format: Winston.format.combine(Winston.format.splat(), Winston.format[Configuration.getLogFormat()]()), - })); +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)() + ) + }) + ) } - -export default logger;