Skip to content

Commit

Permalink
Initial fully-functional version of DispatcherAppender
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Apr 28, 2011
1 parent b644d39 commit aca3cc2
Show file tree
Hide file tree
Showing 14 changed files with 1,075 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
target
bin
.classpath
.project
.settings

61 changes: 59 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,64 @@
Log4j DispatcherAppender Log4j DispatcherAppender
======================== ========================


A Log4j Appender that dispatches to a different Appender instance depending A [Log4j](http://logging.apache.org/log4j/1.2) `Appender` that
on the value of the NDC. dispatches to a different `Appender` instance depending on the value
of the [Nested Diagnostic
Context](http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html).
The delegate appender is copied and a single property is overriden
(e.g. a file location) based on a layout pattern. A typical use case
would be directing logs to a different file based on the business data
that are being processed.

Note that adding an appender to an existing one cannot be done using
the Log4j `PropertiesConfigurator` so you have to use XML or Java to
configure a `DispatcherAppender`.


License: Apache 2.0 License: Apache 2.0

Usage
-----

Sample `log4j.xml`:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="LOGGER" class="org.springframework.util.log4j.DispatcherAppender">
<param name="propertyName" value="file" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="target/logs/%x.log" />
</layout>
<appender-ref ref="FILE" />
</appender>

<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="file" value="target/logs/default.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p: %m%n" />
</layout>
</appender>

<root>
<priority value="info" />
<appender-ref ref="LOGGER" />
</root>

</log4j:configuration>

Application code:

logger.info("foo");
NDC.push("alt");
try {
logger.info("foo");
} finally {
NDC.clear();
}

Result:

$ ls target/logs
default.log alt.log
80 changes: 80 additions & 0 deletions pom.xml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>log4j-utils</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>
<profiles>
<profile>
<id>strict</id>
<properties>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
</properties>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<descriptorRefs>
<descriptorRef>project</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--forkMode>pertest</forkMode -->
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit aca3cc2

Please sign in to comment.