如何使用pojo找出类级Log4J appender

时间:2017-03-13 19:17:08

标签: java log4j

我们说有以下配置

...
log4j.rootCategory=DEBUG, CONSOLE
log4j.logger.com.app.me.Myclass=FILE
....

我知道如何找到根类别appender CONSOLE

Logger rootLogger = Logger.getRootLogger();
Enumeration<Appender> appenders = rootLogger.getAllAppenders();
while(appenders.hasMoreElements()){
    //....
}

但是,如何获得类级别的追加器?我尝试在MyClass

中使用代码
Logger classLogger= Logger.getLogger(Myclass.class);
Enumeration<Appender> appenders = classLogger.getAllAppenders();
while(appenders.hasMoreElements()){
    //....
}

appenders始终不包含appender。

1 个答案:

答案 0 :(得分:1)

这样做

log4j.logger.com.app.me.Myclass=DEBUG, myappender
log4j.additivity.com.app.me.Myclass=false

log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.datePattern='-'dd'.log'
log4j.appender.myappender.File=log/mylog.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.logfile.layout.ConversionPattern=%-6r %d{ISO8601} %-5p %40.40c %x - %m\n