Add return type to getLogger
[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
bc464bb1 22let loggerInstance: Logger | null = null;
28e6dd3a 23const getLogger = (): Logger => {
bc464bb1
JB
24 if (!loggerInstance) {
25 loggerInstance = createLogger({
26 level: Configuration.getLogLevel(),
27 format: format.combine(format.splat(), format[Configuration.getLogFormat()]()),
28 transports: transports,
29 });
30 return loggerInstance;
31 }
32};
7dde0b73
JB
33
34//
35// If enabled, log to the `console` with the format:
36// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
37//
7ec46a9a 38if (Configuration.getLogConsole()) {
bc464bb1 39 getLogger().add(new Console({
8387cd31 40 format: format.combine(format.splat(), format[Configuration.getLogFormat()]()),
7dde0b73
JB
41 }));
42}
43
bc464bb1 44export default getLogger;