Log4J 1.2 PropertyConfigurator -> Log4J2
Asked Answered
C

2

10

Currently, our application uses Log4J 1.2 and configures it using either

File file = ...
PropertyConfigurator.configure(file.getAbsolutePath());

or

URL url = ...
PropertyConfigurator.configure(url);

I know that the property file format has changed from 1.2 to 2, but what would be a similar way to configure Log4J 2 using a property file at an arbitrary file or URL?

Churning answered 5/1, 2018 at 14:0 Comment(2)
You're asking if it's better to use a file or a URL?Blamable
No, we use both for different applications.Churning
O
4

From Log4J 2's documentation:

// 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());

Make sure to refer to org.apache.logging.log4j.core.LoggerContext (defined in the log4j-core artifact, not the log4j-api one) and not to org.apache.logging.log4j.spi.LoggerContext.

Obregon answered 5/1, 2018 at 14:12 Comment(4)
Unfortunately, LoggerContext has no method setConfigLocation: logging.apache.org/log4j/2.0/log4j-api/apidocs/org/apache/… Maybe the warning Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release became true. Shouldn't something simple like that be public API?Churning
@ThomasS. You're linking to org.apache.logging.log4j.spi.LoggerContext. The code mentions org.apache.logging.log4j.core.LoggerContext in log4j-core.Catchword
Ah, the same class name used in different packages. That is tricky. Thanks.Churning
How to do this if my code is using a Properties object instead of a file for configurationIndisposed
O
7

You can use PropertiesConfigurationBuilder as follows:

// Custom-loaded properties.
Properties props = ... 
// Beware it should be org.apache.logging.log4j.core.LoggerContext class,
// not the one ins spi package!
// Not sure about the meaning of "false".
LoggerContext context = (LoggerContext)LogManager.getContext(false);
Configuration config = new PropertiesConfigurationBuilder()
            .setConfigurationSource(ConfigurationSource.NULL_SOURCE)
            .setRootProperties(props)
            .setLoggerContext(context)
            .build();
 context.setConfiguration(config);
 Configurator.initialize(config);

It's true that using the core classes looks like a hack but the author himself uses them in his tutotrial: https://logging.apache.org/log4j/log4j-2.3/manual/customconfig.html .

Overnice answered 13/1, 2020 at 10:21 Comment(0)
O
4

From Log4J 2's documentation:

// 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());

Make sure to refer to org.apache.logging.log4j.core.LoggerContext (defined in the log4j-core artifact, not the log4j-api one) and not to org.apache.logging.log4j.spi.LoggerContext.

Obregon answered 5/1, 2018 at 14:12 Comment(4)
Unfortunately, LoggerContext has no method setConfigLocation: logging.apache.org/log4j/2.0/log4j-api/apidocs/org/apache/… Maybe the warning Be aware that this LoggerContext class is not part of the public API so your code may break with any minor release became true. Shouldn't something simple like that be public API?Churning
@ThomasS. You're linking to org.apache.logging.log4j.spi.LoggerContext. The code mentions org.apache.logging.log4j.core.LoggerContext in log4j-core.Catchword
Ah, the same class name used in different packages. That is tricky. Thanks.Churning
How to do this if my code is using a Properties object instead of a file for configurationIndisposed

© 2022 - 2024 — McMap. All rights reserved.