Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve List of Log4J Appenders at Run Time

Is it possible to retrieve a list of all appenders configured in log4j at run time?

I'll flesh out the scenario a little more. Given the following config how would I retrieve all appenders (stdout and altstdout)?

log4j.rootLogger=error, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.altstdout=org.apache.log4j.ConsoleAppender
log4j.appender.altstdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.altstdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
like image 633
binarymelon Avatar asked Dec 24 '12 18:12

binarymelon


People also ask

How do you get all Appenders in Log4j2?

Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();

How do I view log4j logs?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging. Note: Do not change the name and location of the server log.


1 Answers

If you want access to all appenders configured for all loggers you must do something like this

for (Enumeration loggers=LogManager.getCurrentLoggers(); loggers.hasMoreElements(); )  {
    Logger logger = (Logger) loggers.nextElement();
    for (Enumeration appenders=logger.getAllAppenders(); appenders.hasMoreElements(); )  {
        Appender appender = (Appender) appenders.nextElement();
        ...

I don't know why log4j has no method like LogManager.getAllAppenders(), but it looks like
disadvantage.

like image 87
And390 Avatar answered Oct 17 '22 16:10

And390