Commit | Line | Data |
---|---|---|
63ec69b7 | 1 | import type { FormatWrap } from 'logform'; |
ae389044 | 2 | import { createLogger, format, type transport } from 'winston'; |
b768993d | 3 | import TransportType from 'winston/lib/winston/transports/index.js'; |
78202038 | 4 | import DailyRotateFile from 'winston-daily-rotate-file'; |
8114d10e | 5 | |
878e026c | 6 | import { Configuration } from './Configuration'; |
9bf0ef23 | 7 | import { insertAt } from './Utils'; |
5d049829 | 8 | import { ConfigurationSection, type LogConfiguration } from '../types'; |
7ec46a9a | 9 | |
6bf6769e | 10 | let transports: transport[]; |
5d049829 JB |
11 | if ( |
12 | Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).rotate === true | |
13 | ) { | |
14 | const logMaxFiles = Configuration.getConfigurationSection<LogConfiguration>( | |
15 | ConfigurationSection.log, | |
16 | ).maxFiles; | |
17 | const logMaxSize = Configuration.getConfigurationSection<LogConfiguration>( | |
18 | ConfigurationSection.log, | |
19 | ).maxSize; | |
6bf6769e | 20 | transports = [ |
e7aeea18 | 21 | new DailyRotateFile({ |
9bf0ef23 | 22 | filename: insertAt( |
5d049829 JB |
23 | Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log) |
24 | .errorFile!, | |
e7aeea18 | 25 | '-%DATE%', |
5d049829 JB |
26 | Configuration.getConfigurationSection<LogConfiguration>( |
27 | ConfigurationSection.log, | |
28 | ).errorFile!.indexOf('.log'), | |
e7aeea18 JB |
29 | ), |
30 | level: 'error', | |
9988696d JB |
31 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
32 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 JB |
33 | }), |
34 | new DailyRotateFile({ | |
9bf0ef23 | 35 | filename: insertAt( |
5d049829 | 36 | Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).file!, |
e7aeea18 | 37 | '-%DATE%', |
5d049829 JB |
38 | Configuration.getConfigurationSection<LogConfiguration>( |
39 | ConfigurationSection.log, | |
40 | ).file!.indexOf('.log'), | |
e7aeea18 | 41 | ), |
9988696d JB |
42 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
43 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 | 44 | }), |
6bf6769e JB |
45 | ]; |
46 | } else { | |
47 | transports = [ | |
5d049829 JB |
48 | new TransportType.File({ |
49 | filename: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log) | |
50 | .errorFile, | |
51 | level: 'error', | |
52 | }), | |
53 | new TransportType.File({ | |
54 | filename: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log) | |
55 | .file, | |
56 | }), | |
6bf6769e JB |
57 | ]; |
58 | } | |
7dde0b73 | 59 | |
ae389044 | 60 | export const logger = createLogger({ |
5d049829 JB |
61 | silent: !Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log) |
62 | .enabled, | |
63 | level: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).level, | |
64 | format: format.combine( | |
65 | format.splat(), | |
66 | ( | |
67 | format[ | |
68 | Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).format! | |
69 | ] as FormatWrap | |
70 | )(), | |
71 | ), | |
17e9e8ce | 72 | transports, |
9f2e3130 | 73 | }); |
7dde0b73 JB |
74 | |
75 | // | |
76 | // If enabled, log to the `console` with the format: | |
77 | // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` | |
78 | // | |
5d049829 | 79 | if (Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).console) { |
e7aeea18 | 80 | logger.add( |
0d8140bd | 81 | new TransportType.Console({ |
c0f4be74 JB |
82 | format: format.combine( |
83 | format.splat(), | |
5d049829 JB |
84 | ( |
85 | format[ | |
86 | Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log) | |
87 | .format! | |
88 | ] as FormatWrap | |
89 | )(), | |
c0f4be74 | 90 | ), |
5edd8ba0 | 91 | }), |
e7aeea18 | 92 | ); |
7dde0b73 | 93 | } |