I have a winston logger, and I want to set the filename of the file where the logger is being executed as the label object of the logger info object.
e.g:
[info]:[callingFileName.js] - 19.06.2019 14:09:19: [message]:...
Here is my code:
'use strict';
const { createLogger, format, transports } = require('winston');
const winston = require('winston');
const path = require('path');
var appRoot = require('app-root-path');
const { splat, combine, timestamp, colorize, printf, label } = winston.format;
const o = {
a: [1, 2, [[
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ' +
'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'test',
'foo']], 4],
b: new Map([['za', 1], ['zb', 'test']])
};
var options = {
debugfile: {
level: 'debug',
filename: `${appRoot}/logs/debug.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
errorfile: {
level: 'error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
colorize: true,
handleExceptions: true,
prettyPrint: true,
json: true,
}
};
const customFormat = printf(info => `[${info.level}]:[${info.label}] - ${info.timestamp}: [message]: ${info.message} ${info.meta? JSON.stringify(info.meta) : ''}`);
const simplelogger = createLogger({
name: "debug-console",
format: combine(
colorize({all:false}),
timestamp({
format: 'DD.MM.YYYY HH:mm:ss'
}),
label({
label: path.basename(process.mainModule.filename)
}),
splat(),
customFormat
),
transports: [
new transports.Console(options.console),
new transports.File( options.errorfile),
new transports.File( options.debugfile)
]
});
The label part is here:
label({ label: path.basename(process.mainModule.filename) }),
but I have the problem, that when I start my program, I run npm run index.js
, so it always logs the calling file as index.js label: [info]:[index.js] - 19.06.2019 14:09:19: [message]: ...
. Is it somehow possible to set the label dynamically as the filename where I am running my logger.log()
function? Or do I have to create a new class / instance everytime that I am in a new file other than index.js ?
EDIT: I switched to npm logat module, which solved my problems immediately with 3 lines of code. However, is there any simple solution with winston? I just saw I don't need winston anymore with logat.