Last active
August 29, 2015 14:04
-
-
Save jclosure/c76a5ce531ef323ee384 to your computer and use it in GitHub Desktop.
extend log4j with MDC properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup your log4j ConversionPattern to include a new ApplicationId field | |
log4j.appender.out.layout.ConversionPattern=%d{ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{ApplicationId} | %m%n | |
# You can now set this value in your java code like this | |
private void setupLogging() { | |
org.apache.log4j.MDC.put("ApplicationId", "XZY321"); | |
} | |
# Or if you are not writing java code you can still do this with Spring Xml Config | |
# Here's how to run a static method using Spring's MethodInvokingFactoryBean to add a MDC Property to log4j's tracked fields | |
# Setup a Spring Context with the | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Configure Spring Context --> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<bean | |
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> | |
<property name="targetClass"> | |
<value>org.apache.log4j.MDC</value> | |
</property> | |
<property name="targetMethod"> | |
<value>put</value> | |
</property> | |
<property name="arguments"> | |
<list> | |
<value>ApplicationId</value> | |
<value>ABC123</value> | |
</list> | |
</property> | |
</bean> | |
</beans> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment