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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With