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?
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?
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());**
My solution consisted simply in following the instructions in the Log4J site https://logging.apache.org/log4j/2.x/manual/migration.html, 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.
Log4j 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="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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();
}
The first line before any log4j2 code - configure sys prop to your Properties configuration factory
System.setProperty("log4j.configurationFactory", ThePropertiesConfigurationFactory.class.getName());
@Plugin(name = "ThePropertiesConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(8)
public class ThePropertiesConfigurationFactory extends ConfigurationFactory {
@Override
protected String[] getSupportedTypes() {
return new String[]{".properties", "*"};
}
@Override
public PropertiesConfiguration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
final Properties properties = new Properties();
try (final InputStream configStream = source.getInputStream()) {
//load config
properties.load(configStream);
//var loggersProperties = readPropertiesInFormatLog4j1AndTransformInLog4j2Properties();
//properties.putAll(loggersProperties);
} catch (final IOException ioe) {
throw new ConfigurationException("Unable to load " + source.toString(), ioe);
}
return new PropertiesConfigurationBuilder()
.setConfigurationSource(source)
.setRootProperties(properties)
.setLoggerContext(loggerContext)
.build();
}
}
Or extend PropertiesConfigurationFactory
No need of PropertyConfigurator in log4j2.
It will automatically read your log4j2.property file.
Just make sure you have below jar included in your pom.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
----------------------------------------------------------------------
log4j2.properties
appenders=xyz
appender.xyz.type = Console
appender.xyz.name = myOutput
appender.xyz.layout.type = PatternLayout
appender.xyz.layout.pattern = [%d{yy-MMM-dd HH:mm:ss:S}] [%p] [%c{1}:%L] - %m%n
rootLogger.level = info
rootLogger.appenderRefs = abc
rootLogger.appenderRef.abc.ref = myOutputstrong text
----------------------------------------------------------------------
MyClass.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyClass {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(MyClass.class);
logger.info("----start-------");
}
}
----------------------------------------------------------------------
Output:
[22-Jul-22 17:11:09:128] [INFO] [MyClass:24] - ----start-------
If you want to reconfigure log4j2 in code with a specific configuration file use below code.
// 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());
© 2022 - 2024 — McMap. All rights reserved.