Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Log4J2 appenders at runtime programmatically?

Is it possible to add Log4J2 appenders programmatically using the specifications from the XML configuration?

I plan to define it all in the log4j2.xml and then pick appenders situationally like this (won't compile):

if (arg[0].equals("log") ) {     Logger.getLogger("loggerNameFromXMLConfig").addAppender("appenderNameFromXMLConfig"); } else {     //... } 
like image 775
Brian Johnson Avatar asked Mar 15 '13 20:03

Brian Johnson


People also ask

How do I change the log4j configuration 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).

How do you change the log level in Log4j2 at runtime?

You can set a logger's level with the class Configurator from Log4j Core. BUT be aware that the Configurator class is not part of the public API. If you wish to change the root logger level, do something like this : LoggerContext ctx = (LoggerContext) LogManager.

How do you implement Log4j2?

There are many ways to use Log4j2 configuration in you application. Using a configuration file written in XML, JSON, YAML or properties file. Programmatically, by creating a configuration factory and configuration implementation. Programmatically, by calling APIs exposed in the configuration interface.

What are Log4j2 Appenders?

In the log4j2 architecture, an appender is basically responsible for sending log messages to a certain output destination. Here are some of the most useful types of appenders that the library provides: ConsoleAppender – logs messages to the System console. FileAppender – writes log messages to a file.


2 Answers

There have been a number of requests to support better programmatic configuration for Log4j 2. Sorry it took so long. As of Log4j 2.4, API was added to log4j-core to facilitate programmatic configuration.

The new ConfigurationBuilder API allows users to construct component definitions. With this API, there is no need to work directly with actual configuration objects (like LoggerConfig and FileAppender) which require a lot of knowledge on how Log4j works under the hood. Component definitions are added to the ConfigurationBuilder, and once all the definitions have been collected all the actual configuration objects (like Loggers and Appenders) are constructed. It feels a bit like the XML configuration syntax, except that you are writing Java code.

Note that the new ConfigurationBuilder API allows user code to create a new configuration or completely replace the existing configuration. If your use case is different, and you want to programmatically modify (rather than replace) an existing configuration after Log4j was started, then you will need to work with actual configuration objects. In that case, please see the Programmatically Modifying the Current Configuration after Initialization section of the manual.

like image 158
Remko Popma Avatar answered Oct 12 '22 03:10

Remko Popma


Edit: for the newest versions of log4j2, see https://stackoverflow.com/a/33472893/1899566 instead.

I get the impression they don't want you doing this, but this works for me:

if (arg[0].equals("log") ) {   org.apache.logging.log4j.Logger logger     = org.apache.logging.log4j.LogManager.getLogger("loggerNameFromXMLConfig");   org.apache.logging.log4j.core.Logger coreLogger     = (org.apache.logging.log4j.core.Logger)logger;   org.apache.logging.log4j.core.LoggerContext context     = (org.apache.logging.log4j.core.LoggerContext)coreLogger.getContext();   org.apache.logging.log4j.core.config.BaseConfiguration configuration     = (org.apache.logging.log4j.core.config.BaseConfiguration)context.getConfiguration();    coreLogger.addAppender(configuration.getAppender("appenderNameFromXMLConfig")); } else {   //... } 
like image 23
Robert Fleming Avatar answered Oct 12 '22 03:10

Robert Fleming