Commit | Line | Data |
---|---|---|
63ec69b7 | 1 | import type { FormatWrap } from 'logform'; |
5a2a53cf | 2 | import { type Logger, createLogger, format, type transport } from 'winston'; |
65b0c151 | 3 | import TransportType from 'winston/lib/winston/transports'; |
78202038 | 4 | import DailyRotateFile from 'winston-daily-rotate-file'; |
8114d10e | 5 | |
60a74391 | 6 | import { Configuration, Utils } from './internal'; |
7ec46a9a | 7 | |
6bf6769e | 8 | let transports: transport[]; |
d42ee3c9 | 9 | if (Configuration.getLogRotate() === true) { |
6bf6769e | 10 | const logMaxFiles = Configuration.getLogMaxFiles(); |
9988696d | 11 | const logMaxSize = Configuration.getLogMaxSize(); |
6bf6769e | 12 | transports = [ |
e7aeea18 JB |
13 | new DailyRotateFile({ |
14 | filename: Utils.insertAt( | |
15 | Configuration.getLogErrorFile(), | |
16 | '-%DATE%', | |
1895299d | 17 | Configuration.getLogErrorFile()?.indexOf('.log') |
e7aeea18 JB |
18 | ), |
19 | level: 'error', | |
9988696d JB |
20 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
21 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 JB |
22 | }), |
23 | new DailyRotateFile({ | |
24 | filename: Utils.insertAt( | |
25 | Configuration.getLogFile(), | |
26 | '-%DATE%', | |
1895299d | 27 | Configuration.getLogFile()?.indexOf('.log') |
e7aeea18 | 28 | ), |
9988696d JB |
29 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
30 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 | 31 | }), |
6bf6769e JB |
32 | ]; |
33 | } else { | |
34 | transports = [ | |
0d8140bd JB |
35 | new TransportType.File({ filename: Configuration.getLogErrorFile(), level: 'error' }), |
36 | new TransportType.File({ filename: Configuration.getLogFile() }), | |
6bf6769e JB |
37 | ]; |
38 | } | |
7dde0b73 | 39 | |
9f2e3130 JB |
40 | const logger: Logger = createLogger({ |
41 | level: Configuration.getLogLevel(), | |
63ec69b7 | 42 | format: format.combine(format.splat(), (format[Configuration.getLogFormat()] as FormatWrap)()), |
17e9e8ce | 43 | transports, |
9f2e3130 | 44 | }); |
7dde0b73 JB |
45 | |
46 | // | |
47 | // If enabled, log to the `console` with the format: | |
48 | // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` | |
49 | // | |
7ec46a9a | 50 | if (Configuration.getLogConsole()) { |
e7aeea18 | 51 | logger.add( |
0d8140bd | 52 | new TransportType.Console({ |
c0f4be74 JB |
53 | format: format.combine( |
54 | format.splat(), | |
63ec69b7 | 55 | (format[Configuration.getLogFormat()] as FormatWrap)() |
c0f4be74 | 56 | ), |
e7aeea18 JB |
57 | }) |
58 | ); | |
7dde0b73 JB |
59 | } |
60 | ||
268a74bb | 61 | export { logger }; |