Commit | Line | Data |
---|---|---|
66a7748d | 1 | import type { FormatWrap } from 'logform' |
0749233f | 2 | |
66a7748d JB |
3 | import { createLogger, format, type transport } from 'winston' |
4 | import TransportType from 'winston/lib/winston/transports/index.js' | |
5 | import DailyRotateFile from 'winston-daily-rotate-file' | |
8114d10e | 6 | |
4c3f6c20 | 7 | import { ConfigurationSection, type LogConfiguration } from '../types/index.js' |
66a7748d JB |
8 | import { Configuration } from './Configuration.js' |
9 | import { insertAt } from './Utils.js' | |
7ec46a9a | 10 | |
864e5f8d | 11 | const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>( |
66a7748d JB |
12 | ConfigurationSection.log |
13 | ) | |
14 | let transports: transport[] | |
864e5f8d | 15 | if (logConfiguration.rotate === true) { |
66a7748d JB |
16 | const logMaxFiles = logConfiguration.maxFiles |
17 | const logMaxSize = logConfiguration.maxSize | |
6bf6769e | 18 | transports = [ |
e7aeea18 | 19 | new DailyRotateFile({ |
9bf0ef23 | 20 | filename: insertAt( |
66a7748d | 21 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
864e5f8d | 22 | logConfiguration.errorFile!, |
e7aeea18 | 23 | '-%DATE%', |
66a7748d JB |
24 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
25 | logConfiguration.errorFile!.indexOf('.log') | |
e7aeea18 JB |
26 | ), |
27 | level: 'error', | |
66a7748d | 28 | ...(logMaxFiles != null && { maxFiles: logMaxFiles }), |
d1f5bfd8 | 29 | ...(logMaxSize != null && { maxSize: logMaxSize }), |
e7aeea18 JB |
30 | }), |
31 | new DailyRotateFile({ | |
66a7748d | 32 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
864e5f8d | 33 | filename: insertAt(logConfiguration.file!, '-%DATE%', logConfiguration.file!.indexOf('.log')), |
66a7748d | 34 | ...(logMaxFiles != null && { maxFiles: logMaxFiles }), |
d1f5bfd8 JB |
35 | ...(logMaxSize != null && { maxSize: logMaxSize }), |
36 | }), | |
66a7748d | 37 | ] |
6bf6769e JB |
38 | } else { |
39 | transports = [ | |
5d049829 | 40 | new TransportType.File({ |
864e5f8d | 41 | filename: logConfiguration.errorFile, |
d1f5bfd8 | 42 | level: 'error', |
5d049829 JB |
43 | }), |
44 | new TransportType.File({ | |
d1f5bfd8 JB |
45 | filename: logConfiguration.file, |
46 | }), | |
66a7748d | 47 | ] |
6bf6769e | 48 | } |
7dde0b73 | 49 | |
ae389044 | 50 | export const logger = createLogger({ |
a37fc6dc JB |
51 | format: format.combine( |
52 | format.splat(), | |
66a7748d JB |
53 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
54 | (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)() | |
a37fc6dc | 55 | ), |
0749233f JB |
56 | level: logConfiguration.level, |
57 | silent: logConfiguration.enabled === false, | |
d1f5bfd8 | 58 | transports, |
66a7748d | 59 | }) |
7dde0b73 JB |
60 | |
61 | // | |
62 | // If enabled, log to the `console` with the format: | |
63 | // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` | |
64 | // | |
66a7748d | 65 | if (logConfiguration.console === true) { |
e7aeea18 | 66 | logger.add( |
0d8140bd | 67 | new TransportType.Console({ |
a37fc6dc JB |
68 | format: format.combine( |
69 | format.splat(), | |
66a7748d JB |
70 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
71 | (format[logConfiguration.format! as keyof FormatWrap] as FormatWrap)() | |
d1f5bfd8 | 72 | ), |
66a7748d JB |
73 | }) |
74 | ) | |
7dde0b73 | 75 | } |