Log4j 2 Programmatic Configuration BAEL 1338 (#4057)

This commit is contained in:
ShyamVeda
2018-05-02 18:41:59 +05:30
committed by Josh Cummings
parent fad99fe44b
commit 7d41c7edb7
19 changed files with 559 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
<?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>
<artifactId>simple-configuration-xml</artifactId>
<name>simple-configuration-xml</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" ?>
<Configuration>
<Appenders>
<Console name="Stdout">
<PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" />
<MarkerFilter onMatch="ACCEPT" onMismatch="DENY" marker="FLOW" />
</Console>
<RollingFile name="rolling" fileName="target/rolling.log"
filePattern="target/archive/rolling-%d{MM-dd-yy}.log.gz">
<PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" />
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?" />
<SizeBasedTriggeringPolicy size="100M" />
</Policies>
</RollingFile>
<File name="FileSystem" fileName="target/logging.log">
<PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" />
</File>
</Appenders>
<Loggers>
<Logger name="com" level="DEBUG" additivity="false">
<AppenderRef ref="Stdout" />
<AppenderRef ref="rolling" />
<AppenderRef ref="FileSystem" />
</Logger>
<Root level="ERROR" additivity="false">
<AppenderRef ref="Stdout" />
<AppenderRef ref="rolling" />
<AppenderRef ref="FileSystem" />
</Root>
</Loggers>
</Configuration>

View File

@@ -0,0 +1,30 @@
/**
This class loads the logging configuration from the xml defined in
src/main/resources and uses the same configuration generated through
programmatic configuration as defined in simple-configuration example.
**/
package com.baeldung.log4j2.logtest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class LogTest {
@Test
public void simpleProgrammaticConfiguration(){
Logger logger = LogManager.getLogger();
Marker markerContent = MarkerManager.getMarker("FLOW");
logger.debug(markerContent, "Debug log message");
logger.info(markerContent, "Info log message");
logger.error(markerContent, "Error log message");
}
}