Ensure 1:1 mapping between charging station instance and its OCPP services
[e-mobility-charging-stations-simulator.git] / src / utils / Logger.ts
CommitLineData
8387cd31
JB
1import { Console, File } from 'winston/lib/winston/transports';
2import { Logger, createLogger, format, transport } from 'winston';
6bf6769e 3
6af9012e 4import Configuration from './Configuration';
8387cd31 5import DailyRotateFile from 'winston-daily-rotate-file';
7ec46a9a 6import Utils from './Utils';
7ec46a9a 7
6bf6769e
JB
8let transports: transport[];
9if (Configuration.getLogRotate()) {
10 const logMaxFiles = Configuration.getLogMaxFiles();
11 transports = [
8387cd31
JB
12 new DailyRotateFile({ filename: Utils.insertAt(Configuration.getLogErrorFile(), '-%DATE%', Configuration.getLogErrorFile().indexOf('.log')), level: 'error', maxFiles: logMaxFiles }),
13 new DailyRotateFile({ filename: Utils.insertAt(Configuration.getLogFile(), '-%DATE%', Configuration.getLogFile().indexOf('.log')), maxFiles: logMaxFiles }),
6bf6769e
JB
14 ];
15} else {
16 transports = [
8387cd31
JB
17 new File({ filename: Configuration.getLogErrorFile(), level: 'error' }),
18 new File({ filename: Configuration.getLogFile() }),
6bf6769e
JB
19 ];
20}
7dde0b73 21
9f2e3130
JB
22const logger: Logger = createLogger({
23 level: Configuration.getLogLevel(),
24 format: format.combine(format.splat(), format[Configuration.getLogFormat()]()),
25 transports: transports,
26});
7dde0b73
JB
27
28//
29// If enabled, log to the `console` with the format:
30// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
31//
7ec46a9a 32if (Configuration.getLogConsole()) {
9f2e3130 33 logger.add(new Console({
8387cd31 34 format: format.combine(format.splat(), format[Configuration.getLogFormat()]()),
7dde0b73
JB
35 }));
36}
37
9f2e3130 38export default logger;