Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the configured Log4J appenders at runtime?

I want to configure an appender at startup and then dynamically add and remove it from various loggers on demand. I'd prefer to have log4j configure this appender itself, and just grab a reference to it when needed. If that's not possible, I'll have to instantiate the appender myself and hold onto it.

like image 360
Seth Weiner Avatar asked Dec 15 '09 19:12

Seth Weiner


People also ask

Where can I find log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

How do I change the log4j properties at runtime?

Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.

How do I view log4j in a project?

Navigate into the "META-INF" sub-directory and open the file "MANIFEST. MF" in a text editor. Find the line starting with "Implementation-Version", this is the Log4j version.

How do I use console Appenders in Log4j2?

Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.


1 Answers

Appenders are generally added to the root logger. Here's some pseudocode

// get the root logger and remove the appender we want Logger logger = Logger.getRootLogger(); Appender appender = logger.getAppender("foo"); logger.removeAppender(appender)  // when we want to add it back... logger.addAppender(appender); 

I'm pretty sure you can do this on other loggers than the root logger as well, though I've never tried that.

like image 112
Alan Krueger Avatar answered Oct 14 '22 04:10

Alan Krueger