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 | |
864e5f8d JB |
10 | const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>( |
11 | ConfigurationSection.log, | |
12 | ); | |
6bf6769e | 13 | let transports: transport[]; |
864e5f8d JB |
14 | if (logConfiguration.rotate === true) { |
15 | const logMaxFiles = logConfiguration.maxFiles; | |
16 | const logMaxSize = logConfiguration.maxSize; | |
6bf6769e | 17 | transports = [ |
e7aeea18 | 18 | new DailyRotateFile({ |
9bf0ef23 | 19 | filename: insertAt( |
864e5f8d | 20 | logConfiguration.errorFile!, |
e7aeea18 | 21 | '-%DATE%', |
864e5f8d | 22 | logConfiguration.errorFile!.indexOf('.log'), |
e7aeea18 JB |
23 | ), |
24 | level: 'error', | |
9988696d JB |
25 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
26 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 JB |
27 | }), |
28 | new DailyRotateFile({ | |
864e5f8d | 29 | filename: insertAt(logConfiguration.file!, '-%DATE%', logConfiguration.file!.indexOf('.log')), |
9988696d JB |
30 | ...(logMaxFiles && { maxFiles: logMaxFiles }), |
31 | ...(logMaxSize && { maxSize: logMaxSize }), | |
e7aeea18 | 32 | }), |
6bf6769e JB |
33 | ]; |
34 | } else { | |
35 | transports = [ | |
5d049829 | 36 | new TransportType.File({ |
864e5f8d | 37 | filename: logConfiguration.errorFile, |
5d049829 JB |
38 | level: 'error', |
39 | }), | |
40 | new TransportType.File({ | |
864e5f8d | 41 | filename: logConfiguration.file, |
5d049829 | 42 | }), |
6bf6769e JB |
43 | ]; |
44 | } | |
7dde0b73 | 45 | |
ae389044 | 46 | export const logger = createLogger({ |
864e5f8d JB |
47 | silent: !logConfiguration.enabled, |
48 | level: logConfiguration.level, | |
a37fc6dc JB |
49 | format: format.combine( |
50 | format.splat(), | |
51 | (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)(), | |
52 | ), | |
17e9e8ce | 53 | transports, |
9f2e3130 | 54 | }); |
7dde0b73 JB |
55 | |
56 | // | |
57 | // If enabled, log to the `console` with the format: | |
58 | // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` | |
59 | // | |
864e5f8d | 60 | if (logConfiguration.console) { |
e7aeea18 | 61 | logger.add( |
0d8140bd | 62 | new TransportType.Console({ |
a37fc6dc JB |
63 | format: format.combine( |
64 | format.splat(), | |
65 | (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)(), | |
66 | ), | |
5edd8ba0 | 67 | }), |
e7aeea18 | 68 | ); |
7dde0b73 | 69 | } |