Commit | Line | Data |
---|---|---|
8387cd31 JB |
1 | import { Console, File } from 'winston/lib/winston/transports'; |
2 | import { Logger, createLogger, format, transport } from 'winston'; | |
6bf6769e | 3 | |
6af9012e | 4 | import Configuration from './Configuration'; |
8387cd31 | 5 | import DailyRotateFile from 'winston-daily-rotate-file'; |
7ec46a9a | 6 | import Utils from './Utils'; |
7ec46a9a | 7 | |
6bf6769e JB |
8 | let transports: transport[]; |
9 | if (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 | 22 | let loggerInstance: Logger | null = null; |
28e6dd3a | 23 | const 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 | 38 | if (Configuration.getLogConsole()) { |
bc464bb1 | 39 | getLogger().add(new Console({ |
8387cd31 | 40 | format: format.combine(format.splat(), format[Configuration.getLogFormat()]()), |
7dde0b73 JB |
41 | })); |
42 | } | |
43 | ||
bc464bb1 | 44 | export default getLogger; |