【问题标题】:Log4j Issue while using RollingFileAppender,maxsize,maxbackupindex and close使用 RollingFileAppender、maxsize、maxbackupindex 和关闭时出现 Log4j 问题
【发布时间】:2016-04-29 08:45:42
【问题描述】:

我使用了RollingFileAppender 和 logger 类将日志打印到文件中,我没有单独的配置文件。

public void logBIHAuditMessage(String message, Level level, String Location) {

    try {
        RollingFileAppender fileAppender = new RollingFileAppender(new PatternLayout(
                "%d{DATE} %p %M %m %n"), "C:\\Logfiles\\Audit.log", true);

        fileAppender.setMaxFileSize("5MB");
        fileAppender.setMaxBackupIndex(5);
        fileAppender.activateOptions();
        LOG.addAppender(fileAppender);
        LOG.info(message);
        fileAppender.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

logBIHAuditMessage 在 Apache camel 应用程序中的多个位置被调用。

由于使用fileAppender.close();,我遇到了错误

10:36:39,715 错误 [stderr](Camel (camel-9) 线程 #20 - JmsConsumer[BIH_A1_Req_INHQ])log4j:ERROR Attempted to append to closed appender named [null]。

如果我不关闭文件附加程序,则会多次打印相同的日志。

如果我不使用 synchronized 关键字,一些日志也会被打印多次,您能告诉我可能是什么问题吗?

【问题讨论】:

    标签: java logging log4j apache-camel


    【解决方案1】:

    Log4J 中有两个 log4j.properties

    您将一个保留在类路径中,而其他正在加载的可能是使用PropertyConfigurator.configure(..)). 以编程方式进行的,您可以删除其中任何一个。

    或者,您可以通过以下代码覆盖它。

    BasicConfigurator.resetConfiguration();
    PropertyConfigurator.configure(props);
    

    【讨论】:

    • 嗨,谢谢,我已经删除了同步关键字并添加了重置配置,它只打印一次。但在使用 fileAppender.close() 时出现以下错误; 15:25:14,434 错误 [stderr](Camel (camel-22) 线程 #29 - JmsConsumer [BIH_A1_Req_INHQ])log4j:错误尝试附加到名为 [null] 的已关闭附加程序。仍然出现同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多