]>
Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blob - src/utils/Logger.ts
e8b3af6450e5a3d64ee042413fc3c3677a72ccdd
1 import type { FormatWrap
} from
'logform'
3 import { createLogger
, format
, type transport
, transports
as WinstonTransports
} from
'winston'
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
, isNotEmptyString
} from
'./Utils.js'
10 const logConfiguration
= Configuration
.getConfigurationSection
<LogConfiguration
>(
11 ConfigurationSection
.log
13 const transports
: transport
[] = []
14 if (logConfiguration
.rotate
=== true) {
15 const logMaxFiles
= logConfiguration
.maxFiles
16 const logMaxSize
= logConfiguration
.maxSize
17 if (isNotEmptyString(logConfiguration
.errorFile
)) {
21 logConfiguration
.errorFile
,
23 logConfiguration
.errorFile
.indexOf('.log')
26 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
27 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
31 if (isNotEmptyString(logConfiguration
.file
)) {
34 filename
: insertAt(logConfiguration
.file
, '-%DATE%', logConfiguration
.file
.indexOf('.log')),
35 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
36 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
41 if (isNotEmptyString(logConfiguration
.errorFile
)) {
43 new WinstonTransports
.File({
44 filename
: logConfiguration
.errorFile
,
49 if (isNotEmptyString(logConfiguration
.file
)) {
51 new WinstonTransports
.File({
52 filename
: logConfiguration
.file
,
58 const logFormat
= format
.combine(
60 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61 (format
[logConfiguration
.format
! as keyof FormatWrap
] as FormatWrap
)()
64 const logger
= createLogger({
66 level
: logConfiguration
.level
,
67 silent
: logConfiguration
.enabled
=== false || transports
.length
=== 0,
72 // If enabled, log to the `console` with the format:
73 // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
75 if (logConfiguration
.console
=== true) {
77 new WinstonTransports
.Console({