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