I used two logger Winston & NestJS Logger
NestJS Logger is easily show class_name But Winston isn't
I want to use Winston Logger showing class_name
- logger.js
'use strict'
const logDir = 'logs'
const getLabel = function (callingModule: NodeModule) {
const parts = callingModule.filename.split(path.sep)
return parts[parts.length - 1]
}
const logFormat = printf(({ timestamp, label, level, message, ...metadata }) => {
let msg = `${timestamp}` + ` ${label} ${level}: [${getLabel(module)}] ${message}`
if (metadata.length > 0) {
msg += JSON.stringify(metadata)
}
return msg
})
const colorizer = winston.format.colorize()
const logger = winston.createLogger({
level: 'debug',
format: combine(
label({
label: 'Test_v1'
}),
timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
colorize({
all: true,
level: true,
message: true,
colors: { info: 'green', error: 'red', http: 'blue' }
}),
logFormat
),
transports: [
new winston.transports.Console(),
new winstonDaily({
level: 'info',
datePattern: 'YYYY-MM-DD',
dirname: logDir,
filename: `%DATE%.log`,
maxFiles: 30,
zippedArchive: true
})
]
})
export default logger
- whatever.ts
and i call logger like this
import log from '@/common/logger'
...
log.info("test")
my expected result is
2024-03-07 16:50:46 Test_v1 info: [whatever] test
but i got
2024-03-07 16:50:46 Test_v1 info: [logger.js] test
Should I put it in as a parameter directly?


I have two answers for you:
so this will outputs:
[Nest] 52 - 03/08/2024, 7:19:06 PM LOG [AppService] Very important stuffBut if you still want to trace file then:
I`m not sure how to correct use it to winston but one obvious way will be:
-- logger.ts
not a best solution, but it will works and output this:
2024-03-07 16:50:46 Test_v1 info: [logger.js] [app/app.module.js:25] Sirius stuffI guess you can just edit your
getLabeland you will not need to overwriteexport