Skip to content

Instantly share code, notes, and snippets.

@jclosure
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/c76a5ce531ef323ee384 to your computer and use it in GitHub Desktop.
Save jclosure/c76a5ce531ef323ee384 to your computer and use it in GitHub Desktop.
extend log4j with MDC properties
# 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