3b0ad0bda212a489518698d72d3ebf6a25ae957b
1 import type { FormatWrap
} from
'logform'
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'
7 import { ConfigurationSection
, type LogConfiguration
} from
'../types/index.js'
8 import { Configuration
} from
'./Configuration.js'
9 import { insertAt
} from
'./Utils.js'
11 const logConfiguration
= Configuration
.getConfigurationSection
<LogConfiguration
>(
12 ConfigurationSection
.log
14 let transports
: transport
[]
15 if (logConfiguration
.rotate
=== true) {
16 const logMaxFiles
= logConfiguration
.maxFiles
17 const logMaxSize
= logConfiguration
.maxSize
21 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
22 logConfiguration
.errorFile
!,
24 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
25 logConfiguration
.errorFile
!.indexOf('.log')
28 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
29 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
32 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
33 filename
: insertAt(logConfiguration
.file
!, '-%DATE%', logConfiguration
.file
!.indexOf('.log')),
34 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
35 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
40 new TransportType
.File({
41 filename
: logConfiguration
.errorFile
,
44 new TransportType
.File({
45 filename
: logConfiguration
.file
,
50 export const logger
= createLogger({
51 format
: format
.combine(
53 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
54 (format
[logConfiguration
.format
! as keyof FormatWrap
] as FormatWrap
)()
56 level
: logConfiguration
.level
,
57 silent
: logConfiguration
.enabled
=== false,
62 // If enabled, log to the `console` with the format:
63 // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
65 if (logConfiguration
.console
=== true) {
67 new TransportType
.Console({
68 format
: format
.combine(
70 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
71 (format
[logConfiguration
.format
! as keyof FormatWrap
] as FormatWrap
)()