本文介绍了为什么log4追加成员为null?无法初始化log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个log4j.properties文件

# Root logger
log4j.rootLogger=INFO, sift
log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer
log4j.configDebug = true

# Sift appender
log4j.appender.sift=org.apache.log4j.sift.MDCSiftingAppender
log4j.appender.sift.key=session_id
log4j.appender.sift.default=no_session_id
log4j.appender.sift.appender=org.apache.log4j.FileAppender
log4j.appender.sift.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.sift.appender.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %m%n
log4j.appender.sift.appender.file=/var/log/rr/$\\{session_id\\}.log
log4j.appender.sift.appender.append=true

但是当我尝试登录时会得到NPE

But I get NPE when i try to log

logger.error("example1");

我想知道如何使"apender"成员不为空?

What am i missing how to make the "apender" member not null?

这是简短的main的堆栈跟踪:

here is the stack trace for the short main:

Exception in thread "main" java.lang.NullPointerException
    at org.apache.log4j.sift.MDCSiftingAppender.getAppender(MDCSiftingAppender.java:109)
    at org.apache.log4j.sift.MDCSiftingAppender.append(MDCSiftingAppender.java:79)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.error(Category.java:305)
    at com.waze.rr_logger.SiftExampleLog4j.log(SiftExampleLog4j.java:14)
    at com.waze.rr_logger.SiftExampleLog4j.main(SiftExampleLog4j.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

主要:

public class SiftExampleLog4j {

    static org.apache.log4j.Logger logger = Logger.getLogger(SiftExampleLog4j.class);

    public void log() {
        BasicConfigurator.configure();
        org.apache.log4j.MDC.put("session_id","MyGooApp");

        logger.error("example1");

        org.apache.log4j.MDC.put("session_id","MyFooApp");

        logger.error("example2");
    }

    public static void main (String[] args){
    new SiftExampleLog4j().log();
    }
}

推荐答案

然后尝试此操作并删除那些MDC引用:

Try this then and remove those MDC references:

public class SiftExampleLog4j {

    static org.apache.log4j.Logger logger = Logger.getLogger(SiftExampleLog4j.class);

    public void log() {
        logger.error("example1");
        logger.error("example2");
    }

    public static void main (String[] args){
    new SiftExampleLog4j().log();
    }
}

然后尝试一次添加MDC位.我认为您需要为MDC添加更多代码.另外,它会引发异常,因此您需要一个try/catch处理程序.

Then try adding in MDC bit at a time. I think you need to add a lot more code for MDC. Plus it throws an exception, so you'll need a try/catch handler.

这篇关于为什么log4追加成员为null?无法初始化log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 22:34