Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default log4j MDC value

Tags:

java

log4j

mdc

Does anyone know how to specify a default value for a missing entry in the MDC using log4j's config xml? I have an appender defined in my XML file like so:

<appender name="DBAppender" class="org.apache.log4j.jdbc.JDBCAppender"> 
    <param name="URL" value="jdbc:sqlserver://phenom\\MSSQLSERVER_2012\;databaseName=pickmax_express" /> 
    <param name="Driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 
    <param name="User" value="user" /> 
    <param name="Password" value="password" /> 
    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" 
          value="INSERT INTO LOG (source, message, order_id, log_level) VALUES ( 'TESTSOURCE','%m', %X{orderID}, 0)" 
        /> 
    </layout> 
</appender> 

The part in question is the order ID from the MDC (%X{orderID}). I searched around and only found duplicates of the same thread saying something along the lines of $${orderID:-DefaultValue}, but this doesnt work in this context. I need to be able to default the value to 0 or -1 or some other sentinal value when log messages are received in contexts which wont have an order ID

like image 497
Mark W Avatar asked Apr 25 '14 17:04

Mark W


People also ask

How to configure configuration of Log4j 2?

Configuration of Log4j 2 can be accomplished in 1 of 4 ways: Through a configuration file written in XML, JSON, YAML, or properties format. Programmatically, by creating a ConfigurationFactory and Configuration implementation.

What is MDC in Log4j?

MDC in Log4j Let's introduce MDC. MDC in Log4j allows us to fill a map-like structure with pieces of information that are accessible to the appender when the log message is actually written. The MDC structure is internally attached to the executing thread in the same way a ThreadLocal variable would be.

How does Log4j handle variables declared as $ {name}?

To accomplish this, Log4j uses variations of Apache Commons Lang 's StrSubstitutor and StrLookup classes. In a manner similar to Ant or Maven, this allows variables declared as $ {name} to be resolved using properties declared in the configuration itself.

How do I debug Log4j logs?

This can be accomplished by adding the status attribute to the configuration element or a default value can be provided by setting the "Log4jDefaultStatusLevel" system property. Valid values of the status attribute are "trace", "debug", "info", "warn", "error" and "fatal". The following configuration has the status attribute set to debug.


Video Answer


1 Answers

If you access the MDC object in your java code, you can initialize the value for orderId by adding the following in some startup area (for example, a servlet init() method):

import org.apache.log4j.MDC;

public void blammyStartupMethod()
{
    MDC.put("orderId", "sentinal value");
}

Edit: It seems likely that you will need to set this default every time you write a log message that does not have an orderId value and after each MDC.remove(). AOP seems like an option here.

like image 76
DwB Avatar answered Oct 17 '22 05:10

DwB