Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At the top of each country's processing thread, put the country code into Log4j's mapped diagnostic context (MDC). This uses a ThreadLocal variable so that you don't have to pass the country up and down the call stack explicitly. Then create a custom filter that looks at the MDC, and filters out any events that don't contain the current appender's country code.</p> <p>In your <code>Job</code>:</p> <pre><code>... public static final String MDC_COUNTRY = "com.y.foo.Country"; public void execute(JobExecutionContext context) /* Just guessing that you have the country in your JobContext. */ MDC.put(MDC_COUNTRY, context.get(MDC_COUNTRY)); try { /* Perform your job here. */ ... } finally { MDC.remove(MDC_COUNTRY); } } ... </code></pre> <p>Write a custom <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/Filter.html" rel="nofollow noreferrer">Filter</a>:</p> <pre><code>package com.y.log4j; import org.apache.log4j.spi.LoggingEvent; /** * This is a general purpose filter. If its "value" property is null, * it requires only that the specified key be set in the MDC. If its * value is not null, it further requires that the value in the MDC * is equal. */ public final class ContextFilter extends org.apache.log4j.spi.Filter { public int decide(LoggingEvent event) { Object ctx = event.getMDC(key); if (value == null) return (ctx != null) ? NEUTRAL : DENY; else return value.equals(ctx) ? NEUTRAL : DENY; } private String key; private String value; public void setContextKey(String key) { this.key = key; } public String getContextKey() { return key; } public void setValue(String value) { this.value = value; } public String getValue() { return value; } } </code></pre> <p>In your log4j.xml:</p> <pre><code>&lt;appender name="fr" class="org.apache.log4j.FileAppender"&gt; &lt;param name="file" value="france.log"/&gt; ... &lt;filter class="com.y.log4j.ContextFilter"&gt; &lt;param name="key" value="com.y.foo.Country" /&gt; &lt;param name="value" value="fr" /&gt; &lt;/filter&gt; &lt;/appender&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload