]>
Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blob - src/utils/Logger.ts
c494211eab72de8827113b3c9863a4775c302e70
1 import type { FormatWrap
} from
'logform'
7 type Logger
as WinstonLogger
,
8 transports
as WinstonTransports
,
10 import DailyRotateFile from
'winston-daily-rotate-file'
12 import { ConfigurationSection
, type LogConfiguration
} from
'../types/index.js'
13 import { Configuration
} from
'./Configuration.js'
14 import { insertAt
, isEmpty
, isNotEmptyString
} from
'./Utils.js'
16 let loggerInstance
: undefined | WinstonLogger
18 const getLoggerInstance
= (): WinstonLogger
=> {
19 if (loggerInstance
!== undefined) {
22 const logConfiguration
= Configuration
.getConfigurationSection
<LogConfiguration
>(
23 ConfigurationSection
.log
25 const logTransports
: transport
[] = []
26 if (logConfiguration
.rotate
=== true) {
27 const logMaxFiles
= logConfiguration
.maxFiles
28 const logMaxSize
= logConfiguration
.maxSize
29 if (isNotEmptyString(logConfiguration
.errorFile
)) {
33 logConfiguration
.errorFile
,
35 logConfiguration
.errorFile
.indexOf('.log')
38 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
39 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
43 if (isNotEmptyString(logConfiguration
.file
)) {
47 logConfiguration
.file
,
49 logConfiguration
.file
.indexOf('.log')
51 ...(logMaxFiles
!= null && { maxFiles
: logMaxFiles
}),
52 ...(logMaxSize
!= null && { maxSize
: logMaxSize
}),
57 if (isNotEmptyString(logConfiguration
.errorFile
)) {
59 new WinstonTransports
.File({
60 filename
: logConfiguration
.errorFile
,
65 if (isNotEmptyString(logConfiguration
.file
)) {
67 new WinstonTransports
.File({
68 filename
: logConfiguration
.file
,
73 const logFormat
= format
.combine(
75 (format
[(logConfiguration
.format
?? 'simple') as keyof FormatWrap
] as FormatWrap
)()
77 loggerInstance
= createLogger({
79 level
: logConfiguration
.level
,
81 logConfiguration
.enabled
=== false ||
82 isEmpty(logTransports
) ||
83 process
.env
.NODE_ENV
=== 'test',
84 transports
: logTransports
,
87 // If enabled, log to the `console` with the format:
88 // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
90 if (logConfiguration
.console
=== true) {
92 new WinstonTransports
.Console({
100 export const logger
= new Proxy({} as WinstonLogger
, {
101 get (target
, property
, receiver
): unknown
{
102 if (Reflect
.has(target
, property
)) {
103 return Reflect
.get(target
, property
, receiver
) as unknown
105 return Reflect
.get(getLoggerInstance(), property
, receiver
) as unknown
107 getOwnPropertyDescriptor (target
, property
) {
109 Reflect
.getOwnPropertyDescriptor(target
, property
) ??
110 Reflect
.getOwnPropertyDescriptor(getLoggerInstance(), property
)
114 return Reflect
.getPrototypeOf(getLoggerInstance())
116 has (target
, property
) {
117 return Reflect
.has(target
, property
) || Reflect
.has(getLoggerInstance(), property
)
119 set (target
, property
, value
, receiver
) {
120 return Reflect
.set(target
, property
, value
, receiver
)