问题
I am trying to create a simple logger class with Winston. I created two loggers named Logger
and Error
which extends from Logger
.
The Logger
class by default creates all the logs in al-logs.log
and Error
class when extending from Logger
adds an additional transport to create all error
logs inside error-logs.log
. But no logs are created inside error-logs.log
. What am I missing?
Here is what I did:
First, I created the Logger
class:
class Logger {
constructor(serviceName, label = "") {
this.logger = winston.createLogger({
level: "debug",
format: winston.format.combine(
winston.format.label({ label }),
winston.format.timestamp(),
winston.format.json()
),
defaultMeta: {
service: serviceName
},
transports: [
new winston.transports.File({ filename: 'all-logs.log' })
],
exceptionHandlers: [
new winston.transports.File({ filename: 'exceptions.log' })
]
});
}
}
Then I created a class named Error
that extends the above Logger
class:
class Error extends Logger {
constructor(serviceName, label = "") {
super(serviceName, label);
this.logger.level = "error";
this.logger.transports.push(new winston.transports.File({ filename: 'error-logs.log', level: this.logger.level }))
}
}
Then I created a method named console.error
:
const errorLogger = new Error("test-service");
console.error = (...args) => {
errorLogger.logger.error(args.join(" "))
}
Now, if I do:
console.error("some error")
it does not get logged to the error-logs.log
file but just gets logged to the all-logs.log
from the Logger
class. Why does not anything gets logged to the error-logs.log
file?
来源:https://stackoverflow.com/questions/57985123/error-logs-are-not-passed-to-the-error-log-file-when-debug-logs-does-why-does-t