如何使用Logback登录同一类中的2个不同文件?

时间:2020-12-12 20:55:32

Our Java application uses SLF4J on top of Logback to log error messages. In our logback.xml, we have an appender defined for the error log, along with a logger that specifies the top level of our package hierarchy.

我们的Java应用程序在Logback之上使用SLF4J来记录错误消息。在我们的logback.xml中,我们为错误日志定义了一个appender,以及一个指定包层次结构顶层的记录器。

We are adding functionality to log data to a different log file. I created a class to handle this logging, and I added a new appender to the logback.xml as well as a new logger. The new logger specifies the fully qualified package name of the new class that I created (along with additivity="false"), so that only that specific class will write to the new log file when it invokes the SLF4J logger.

我们正在添加将数据记录到不同日志文件的功能。我创建了一个类来处理这个日志记录,我在logback.xml中添加了一个新的appender以及一个新的logger。新记录器指定我创建的新类的完全限定包名(以及additivity =“false”),以便只有该特定类在调用SLF4J记录器时才会写入新日志文件。

Then I realized that if the new class fails to write to the new log file for whatever reason, I should have it log an error message to our original error log file. But how can that class write to the original error log file, when its SLF4J logger writes to the new log file? Is there a way for that class to get a handle to a logger to the original error log file?

然后我意识到如果新类由于某种原因无法写入新的日志文件,我应该让它将错误消息记录到我们的原始错误日志文件中。但是当它的SLF4J记录器写入新的日志文件时,该类如何写入原始错误日志文件?有没有办法让该类获取原始错误日志文件的记录器句柄?

1 个解决方案

#1


It might be easiest to make the new class have two loggers: one would be it's own's and the other would be the logger used to log errors. It's not forbidden for a class to use two loggers simultaniously (but for different purposes):

使新类有两个记录器可能是最简单的:一个是它自己的记录器,另一个记录器用于记录错误。一个班级不允许同时使用两个记录器(但用于不同目的):

public class NewClass {
    private static final Logger logger = LoggerFactory.getLogger(NewClass.class);
    private static final Logger errorLogger = LoggerFactory.getLogger(ErrorHandlingClass.class);

    public void respond() {
        logger.info("info file");
        try {
            doSomething();
        } catch (Exception e) {
            errorLogger.error("error doing something", e);
        }
    }

However, if you write to the "new log file" using SLF4J logger, I don't think you can detect that writing failed.

但是,如果您使用SLF4J记录器写入“新日志文件”,我认为您无法检测到写入失败。

#1


It might be easiest to make the new class have two loggers: one would be it's own's and the other would be the logger used to log errors. It's not forbidden for a class to use two loggers simultaniously (but for different purposes):

使新类有两个记录器可能是最简单的:一个是它自己的记录器,另一个记录器用于记录错误。一个班级不允许同时使用两个记录器(但用于不同目的):

public class NewClass {
    private static final Logger logger = LoggerFactory.getLogger(NewClass.class);
    private static final Logger errorLogger = LoggerFactory.getLogger(ErrorHandlingClass.class);

    public void respond() {
        logger.info("info file");
        try {
            doSomething();
        } catch (Exception e) {
            errorLogger.error("error doing something", e);
        }
    }

However, if you write to the "new log file" using SLF4J logger, I don't think you can detect that writing failed.

但是,如果您使用SLF4J记录器写入“新日志文件”,我认为您无法检测到写入失败。