Error logs are not passed to the error log file when debug logs does. Why does that happen?

烈酒焚心 提交于 2019-12-08 05:34:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!