java - Log4j getAllAppenders() returns null enumeration -
i trying file name of log4j log file. tried solution given in following answer.
https://stackoverflow.com/a/4401387/1015678
enumeration e = logger.getrootlogger().getallappenders(); while ( e.hasmoreelements() ){ appender app = (appender)e.nextelement(); if ( app instanceof fileappender ){ system.out.println("file: " + ((fileappender)app).getfile()); } }
but, logger.getrootlogger().getallappenders()
returns null enumeration. (i took care use rootlogger
mentioned in answer)
following log4j2.xml
<?xml version="1.0" encoding="utf-8"?> <configuration status="debug"> <properties> <!-- used if log_file_name env variable not set --> <property name="log_file_name">my_app_log</property> </properties> <appenders> <rollingfile name="rollingfile-${web:contextpath}" filename="${sys:catalina.base}/logs/${env:log_file_name}.log" immediateflush="true" filepattern="${sys:catalina.base}/logs/${env:log_file_name}.log.%d{yyyy_mm_dd.hh_mm_ss}"> <patternlayout pattern="%d{yyyymmdd-hhmmss.sss}|%-5p|%t| %-100m (%c{1})%n"/> <policies> <onstartuptriggeringpolicy/> </policies> </rollingfile> </appenders> <loggers> <root level="debug" includelocation="false"> <appenderref ref="rollingfile-${web:contextpath}"/> </root> </loggers> </configuration>
any idea why?
i did grep
on whole source of apache-log4j-2.3-src
getallappenders
, found in 1 place getting implemented.
in category.java
@suppresswarnings("rawtypes") public enumeration getallappenders() { return nullenumeration.getinstance(); }
the above method returns enumeration having no elements in it.
the other in loggertest.java, execute test case.
/** * add appender , see if can retrieved. * skipping test appender interface isn't compatible legacy log4j. public void testappender1() { logger = logger.getlogger("test"); a1 = new listappender("testappender1"); logger.addappender(a1); enumeration enumeration = logger.getallappenders(); appender ahat = (appender) enumeration.nextelement(); assertequals(a1, ahat); } */ /** * add appender x, y, remove x , check if y * remaining appender. * skipping test appender interface isn't compatible legacy log4j. public void testappender2() { a1 = new fileappender(); a1.setname("testappender2.1"); a2 = new fileappender(); a2.setname("testappender2.2"); logger = logger.getlogger("test"); logger.addappender(a1); logger.addappender(a2); logger.removeappender("testappender2.1"); enumeration enumeration = logger.getallappenders(); appender ahat = (appender) enumeration.nextelement(); assertequals(a2, ahat); asserttrue(!enumeration.hasmoreelements()); } */
seeing above comments, looks deprecated, test case commented out. i'm not sure, tried find out if have declared anywhere, couldnt find.
so thought helpful, unless wasting time on debugging.
as explained in answer use getappenders api like
logger logger = logmanager.getlogger(); map<string, appender> appendermap = ((org.apache.logging.log4j.core.logger) logger).getappenders();
Comments
Post a Comment