I'm migrating log4j 1.2.8 to log4j 2.3. Everything works fine, beside that I'm not finding any any alternative for the PropertyConfigurator.
Is there another class to take care of what the PropertyConfigurator did before?
15 Answers
Maybe this can help you?
How do I reconfigure log4j2 in code with a specific configuration file? See the below example. Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release.
// import org.apache.logging.log4j.core.LoggerContext;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());** 1 My solution consisted simply in following the instructions in the Log4J site , that is, I replaced
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version>
</dependency>with
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-1.2-api</artifactId> <version>2.14.1</version> </dependency>No compilation error, even with class org.apache.log4j.PropertyConfigurator.
This was easier than attempting to migrate to
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version>
</dependency>which, as pointed out by @Christian, has no replacement for class PropertyConfigurator. Admittedly, by not migrating I won't benefit from the new capabilities of Log4j 2.
2Log4j 2 currently supports configuration with XML, JSON or YAML. While properties files may also be supported in the near future the syntax will certainly be different from Log4j 1.
Here my solution to the same problem:
web.xml
<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <context-param> <param-name>isLog4jContextSelectorNamed</param-name> <param-value>false</param-value> </context-param> <!-- ... and so on -->
</web-app>LoggerHelper.java
import java.io.File; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.LoggerContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class LoggerHelper { public static Logger INSTANCE; public static void initialize() { String fileName = "<PATH>/log4j.xml"; LoggerContext context = (LoggerContext)LogManager.getContext(false); context.setConfigLocation(new File(fileName).toURI()); INSTANCE = LoggerFactory.getLogger(LoggerHelper.class); String logMessage = String.format("Initialized (%s).", fileName); System.out.println(logMessage); INSTANCE.info(logMessage); } } If you want to change some properties in the log4j2.properties file and make it effect immediatly. Here is the method: private static void changeSomeProperties(String pathToPropertiesFile) { File log4j2PropertiesFile= new File(pathToPropertiesFile, "log4j2.properties"); Properties properties = new Properties(); try (FileInputStream fileInputStream = new FileInputStream(log4j2PropertiesFile); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) { properties.load(bufferedInputStream); } catch (Exception e) { //handle exception } properties.setProperty("property.logDirectory", "someLogPath"); File updatedLog4j2PropertiesFile = new File(pathToPropertiesFile, "updatedlog4j2.properties"); try (OutputStream output = new FileOutputStream(updatedLog4j2PropertiesFile)) { properties.store(output, null); } catch (IOException e) { //handle exception } LoggerContext loggerContext = LoggerContext.getContext(false);
loggerContext.setConfigLocation(updatedLog4j2PropertiesFile.toURI()); loggerContext.reconfigure();
}