b13ec0477ab9314aca3859f264e62c40ae531929
1 import type { FormatWrap
} from
'logform'
2 import { createLogger
, format
, type transport
} from
'winston'
3 import TransportType from
'winston/lib/winston/transports/index.js'
4 import DailyRotateFile from
'winston-daily-rotate-file'
6 import { ConfigurationSection
, type LogConfiguration
} from
'../types/index.js'
7 import { Configuration
} from
'./Configuration.js'
8 import { insertAt
} from
'./Utils.js'
10 const logConfiguration
= Configuration
.getConfigurationSection
<LogConfiguration
>(
11 ConfigurationSection
.log
13 let transports
: transport
[]
14 if (logConfiguration
.rotate
=== true) {
15 const logMaxFiles
= logConfiguration
.maxFiles
16 const logMaxSize
= logConfiguration
.maxSize
20 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
21 logConfiguration
.errorFile
!,
23 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
24 logConfiguration
.errorFile
!.indexOf('.log')
27 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
28 ...(logMaxSize
!= null && { maxSize
: logMaxSize
})
31 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32 filename
: insertAt(logConfiguration
.file
!, '-%DATE%', logConfiguration
.file
!.indexOf('.log')),
33 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
34 ...(logMaxSize
!= null && { maxSize
: logMaxSize
})
39 new TransportType
.File({
40 filename
: logConfiguration
.errorFile
,
43 new TransportType
.File({
44 filename
: logConfiguration
.file
49 export const logger
= createLogger({
50 silent
: logConfiguration
.enabled
=== false,
51 level
: logConfiguration
.level
,
52 format
: format
.combine(
54 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
55 (format
[logConfiguration
.format
! as keyof FormatWrap
] as FormatWrap
)()
61 // If enabled, log to the `console` with the format:
62 // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
64 if (logConfiguration
.console
=== true) {
66 new TransportType
.Console({
67 format
: format
.combine(
69 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70 (format
[logConfiguration
.format
! as keyof FormatWrap
] as FormatWrap
)()