Skip to content

Commit 7d41c7e

Browse files
ShyamVedajzheaux
authored andcommitted
Log4j 2 Programmatic Configuration BAEL 1338 (eugenp#4057)
1 parent fad99fe commit 7d41c7e

19 files changed

Lines changed: 559 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung.log4j2</groupId>
8+
<artifactId>log4j2-programmatic-configuration</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
<groupId>com.baeldung.log4j2</groupId>
12+
<artifactId>modify-xml-configuration</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>modify-xml-configuration</name>
15+
<url>http://maven.apache.org</url>
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
This class demonstrates on modifying the loaded xml configuration by
3+
extending XMLConfigurationFactory as defined in section 4.4 of
4+
"Programmatic Configuration with Log4j 2"
5+
**/
6+
package com.baeldung.log4j2.config;
7+
8+
import org.apache.logging.log4j.core.LoggerContext;
9+
import org.apache.logging.log4j.core.config.Configuration;
10+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
11+
import org.apache.logging.log4j.core.config.ConfigurationSource;
12+
import org.apache.logging.log4j.core.config.Order;
13+
import org.apache.logging.log4j.core.config.plugins.Plugin;
14+
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
15+
16+
@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
17+
@Order(50)
18+
public class CustomXMLConfigurationFactory extends XmlConfigurationFactory {
19+
20+
@Override
21+
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
22+
return new MyXMLConfiguration(loggerContext, source);
23+
}
24+
25+
@Override
26+
public String[] getSupportedTypes() {
27+
return new String[] { ".xml", "*" };
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
This class demonstrates on overriding the configuration loaded through xml
3+
as defined in section 4.4 of "Programmatic Configuration with Log4j 2"
4+
**/
5+
package com.baeldung.log4j2.config;
6+
7+
import org.apache.logging.log4j.Level;
8+
import org.apache.logging.log4j.LogManager;
9+
import org.apache.logging.log4j.core.Appender;
10+
import org.apache.logging.log4j.core.Layout;
11+
import org.apache.logging.log4j.core.LoggerContext;
12+
import org.apache.logging.log4j.core.appender.FileAppender;
13+
import org.apache.logging.log4j.core.config.Configuration;
14+
import org.apache.logging.log4j.core.config.ConfigurationSource;
15+
import org.apache.logging.log4j.core.config.LoggerConfig;
16+
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
17+
import org.apache.logging.log4j.core.layout.PatternLayout;
18+
19+
public class MyXMLConfiguration extends XmlConfiguration {
20+
public MyXMLConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
21+
super(loggerContext, source);
22+
}
23+
24+
@Override
25+
protected void doConfigure() {
26+
super.doConfigure();
27+
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
28+
Configuration config = ctx.getConfiguration();
29+
LoggerConfig loggerConfig = config.getLoggerConfig("com");
30+
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);
31+
Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config);
32+
loggerConfig.addAppender(appender, Level.DEBUG, null);
33+
addAppender(appender);
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration monitorInterval="60">
3+
<Appenders>
4+
<Console name="console" target="SYSTEM_OUT">
5+
<PatternLayout
6+
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
7+
</Console>
8+
</Appenders>
9+
<Loggers>
10+
<Logger name="com" additivity="false" level="info">
11+
<AppenderRef ref="console" />
12+
</Logger>
13+
<Root>
14+
<AppenderRef ref="console" />
15+
</Root>
16+
</Loggers>
17+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.log4j2.logtest;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
import org.apache.logging.log4j.core.LoggerContext;
6+
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
7+
import org.junit.Test;
8+
9+
10+
public class LogTest {
11+
static{
12+
PluginManager.addPackage("com.baeldung.log4j2.config");
13+
}
14+
15+
@Test
16+
public void simpleProgrammaticConfiguration() {
17+
Logger logger = LogManager.getLogger();
18+
LoggerContext ctx = (LoggerContext) LogManager.getContext();
19+
logger.debug("Debug log message");
20+
logger.info("Info log message");
21+
logger.error("Error log message");
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.log4j2</groupId>
6+
<artifactId>log4j2-programmatic-configuration</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
<modules>
10+
<module>simple-configuration</module>
11+
<module>set-configuration-factory</module>
12+
<module>simple-configurator</module>
13+
<module>simple-configuration-xml</module>
14+
<module>modify-xml-configuration</module>
15+
</modules>
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.logging.log4j</groupId>
25+
<artifactId>log4j-core</artifactId>
26+
<version>2.11.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.logging.log4j</groupId>
30+
<artifactId>log4j-slf4j-impl</artifactId>
31+
<version>2.11.0</version>
32+
</dependency>
33+
</dependencies>
34+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.baeldung.log4j2</groupId>
6+
<artifactId>log4j2-programmatic-configuration</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>set-configuration-factory</artifactId>
10+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
This class demonstrates how to build the components of
3+
the configuration factory, as described in Section 3 of
4+
"Programmatic Configuration with Log4j 2"
5+
**/
6+
package com.baeldung.log4j2.config;
7+
8+
import java.io.IOException;
9+
import java.net.URI;
10+
11+
import org.apache.logging.log4j.Level;
12+
import org.apache.logging.log4j.core.Filter;
13+
import org.apache.logging.log4j.core.LoggerContext;
14+
import org.apache.logging.log4j.core.config.Configuration;
15+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
16+
import org.apache.logging.log4j.core.config.ConfigurationSource;
17+
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
18+
import org.apache.logging.log4j.core.config.builder.api.ComponentBuilder;
19+
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
20+
import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder;
21+
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
22+
import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder;
23+
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
24+
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
25+
26+
public class CustomConfigurationFactory extends ConfigurationFactory {
27+
28+
static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
29+
AppenderComponentBuilder console = builder.newAppender("Stdout", "Console");
30+
LayoutComponentBuilder layout = builder.newLayout("PatternLayout")
31+
.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable");
32+
console.add(layout);
33+
FilterComponentBuilder filter = builder.newFilter("MarkerFilter", Filter.Result.ACCEPT, Filter.Result.DENY);
34+
filter.addAttribute("marker", "FLOW");
35+
console.add(filter);
36+
builder.add(console);
37+
ComponentBuilder triggeringPolicies = builder.newComponent("Policies")
38+
.addComponent(builder.newComponent("CronTriggeringPolicy")
39+
.addAttribute("schedule", "0 0 0 * * ?"))
40+
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy")
41+
.addAttribute("size", "100M"));
42+
AppenderComponentBuilder rollingFile = builder.newAppender("rolling", "RollingFile");
43+
rollingFile.addAttribute("fileName", "target/rolling.log");
44+
rollingFile.addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz");
45+
rollingFile.add(layout);
46+
rollingFile.addComponent(triggeringPolicies);
47+
builder.add(rollingFile);
48+
AppenderComponentBuilder file = builder.newAppender("FileSystem", "File");
49+
file.addAttribute("fileName", "target/logging.log");
50+
file.add(layout);
51+
builder.add(file);
52+
LoggerComponentBuilder logger = builder.newLogger("com", Level.DEBUG);
53+
logger.add(builder.newAppenderRef("Stdout"));
54+
logger.add(builder.newAppenderRef("rolling"));
55+
logger.add(builder.newAppenderRef("FileSystem"));
56+
logger.addAttribute("additivity", false);
57+
builder.add(logger);
58+
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.ERROR);
59+
rootLogger.add(builder.newAppenderRef("Stdout"));
60+
rootLogger.add(builder.newAppenderRef("rolling"));
61+
rootLogger.add(builder.newAppenderRef("FileSystem"));
62+
rootLogger.addAttribute("additivity", false);
63+
builder.add(rootLogger);
64+
try {
65+
builder.writeXmlConfiguration(System.out);
66+
} catch (IOException e) {
67+
// TODO Auto-generated catch block
68+
e.printStackTrace();
69+
}
70+
return builder.build();
71+
}
72+
73+
public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
74+
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder();
75+
return createConfiguration(name, builder);
76+
}
77+
78+
@Override
79+
protected String[] getSupportedTypes() {
80+
return new String[] { "*" };
81+
}
82+
83+
@Override
84+
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) {
85+
return getConfiguration(loggerContext, source.toString(), null);
86+
}
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
This class invokes the configuration factory with static initialization,
3+
as defined in section 4.1 of the "Programmatic Configuration with Log4j 2"
4+
**/
5+
package com.baeldung.log4j2.logtest;
6+
7+
import org.apache.logging.log4j.LogManager;
8+
import org.apache.logging.log4j.Logger;
9+
import org.apache.logging.log4j.Marker;
10+
import org.apache.logging.log4j.MarkerManager;
11+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.JUnit4;
15+
16+
import com.baeldung.log4j2.config.CustomConfigurationFactory;
17+
18+
@RunWith(JUnit4.class)
19+
public class LogTest {
20+
static {
21+
CustomConfigurationFactory customConfigurationFactory = new CustomConfigurationFactory();
22+
ConfigurationFactory.setConfigurationFactory(customConfigurationFactory);
23+
}
24+
25+
@Test
26+
public void simpleProgrammaticConfiguration() {
27+
Logger logger = LogManager.getLogger();
28+
Marker markerContent = MarkerManager.getMarker("FLOW");
29+
logger.debug(markerContent, "Debug log message");
30+
logger.info(markerContent, "Info log message");
31+
logger.error(markerContent, "Error log message");
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung.log4j2</groupId>
8+
<artifactId>log4j2-programmatic-configuration</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>simple-configuration-xml</artifactId>
12+
<name>simple-configuration-xml</name>
13+
<url>http://maven.apache.org</url>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
</project>

0 commit comments

Comments
 (0)