Log4j 2 Programmatic Configuration BAEL 1338 (#4057)
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0"?>
|
||||
<project
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.baeldung.log4j2</groupId>
|
||||
<artifactId>log4j2-programmatic-configuration</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<groupId>com.baeldung.log4j2</groupId>
|
||||
<artifactId>modify-xml-configuration</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>modify-xml-configuration</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
This class demonstrates on modifying the loaded xml configuration by
|
||||
extending XMLConfigurationFactory as defined in section 4.4 of
|
||||
"Programmatic Configuration with Log4j 2"
|
||||
**/
|
||||
package com.baeldung.log4j2.config;
|
||||
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.config.Configuration;
|
||||
import org.apache.logging.log4j.core.config.ConfigurationFactory;
|
||||
import org.apache.logging.log4j.core.config.ConfigurationSource;
|
||||
import org.apache.logging.log4j.core.config.Order;
|
||||
import org.apache.logging.log4j.core.config.plugins.Plugin;
|
||||
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
|
||||
|
||||
@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
|
||||
@Order(50)
|
||||
public class CustomXMLConfigurationFactory extends XmlConfigurationFactory {
|
||||
|
||||
@Override
|
||||
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
|
||||
return new MyXMLConfiguration(loggerContext, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSupportedTypes() {
|
||||
return new String[] { ".xml", "*" };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
This class demonstrates on overriding the configuration loaded through xml
|
||||
as defined in section 4.4 of "Programmatic Configuration with Log4j 2"
|
||||
**/
|
||||
package com.baeldung.log4j2.config;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.core.Appender;
|
||||
import org.apache.logging.log4j.core.Layout;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.appender.FileAppender;
|
||||
import org.apache.logging.log4j.core.config.Configuration;
|
||||
import org.apache.logging.log4j.core.config.ConfigurationSource;
|
||||
import org.apache.logging.log4j.core.config.LoggerConfig;
|
||||
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
|
||||
import org.apache.logging.log4j.core.layout.PatternLayout;
|
||||
|
||||
public class MyXMLConfiguration extends XmlConfiguration {
|
||||
public MyXMLConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
|
||||
super(loggerContext, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doConfigure() {
|
||||
super.doConfigure();
|
||||
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
||||
Configuration config = ctx.getConfiguration();
|
||||
LoggerConfig loggerConfig = config.getLoggerConfig("com");
|
||||
final Layout layout = PatternLayout.createLayout("[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n", null, config, null, null, false, false, null, null);
|
||||
Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config);
|
||||
loggerConfig.addAppender(appender, Level.DEBUG, null);
|
||||
addAppender(appender);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration monitorInterval="60">
|
||||
<Appenders>
|
||||
<Console name="console" target="SYSTEM_OUT">
|
||||
<PatternLayout
|
||||
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="com" additivity="false" level="info">
|
||||
<AppenderRef ref="console" />
|
||||
</Logger>
|
||||
<Root>
|
||||
<AppenderRef ref="console" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.log4j2.logtest;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.core.LoggerContext;
|
||||
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class LogTest {
|
||||
static{
|
||||
PluginManager.addPackage("com.baeldung.log4j2.config");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleProgrammaticConfiguration() {
|
||||
Logger logger = LogManager.getLogger();
|
||||
LoggerContext ctx = (LoggerContext) LogManager.getContext();
|
||||
logger.debug("Debug log message");
|
||||
logger.info("Info log message");
|
||||
logger.error("Error log message");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user