title: Log4J Custom Fields
body: <h1>Introduction:</h1>
  
  <p>I'm trying to get additional fields to log with log4j, and its working but only when I create an appender in code and not in the log4j.properties</p>
  
  <h1>Progress:</h1>
  
  <ol>
  <li>Used this article <a href="http://www.jajakarta.org/log4j/jakarta-log4j-1.1.3/docs/deepExtension.html" rel="noreferrer">Adding Conversion Characters to PatternLayout</a> for log4j 1.1.3</li>
  <li>Made a <a href="http://github.com/scottcowan/spikes/tree/master/log4j/CustomFields/" rel="noreferrer">sample app for log4j 1.2</a></li>
  </ol>
  
  <h1>Problem:</h1>
  
  <p>using the properties file it will run but won't use AppServerPatternLayout so the custom fields aren't displayed.</p>
  
  <h1><a href="http://github.com/scottcowan/spikes/tree/master/log4j/CustomFields/" rel="noreferrer">Download Code</a></h1>
  
  <h3>customlog.properties</h3>
  
  <pre><code>log4j.rootLogger=FATAL
  log4j.logger.some.log=INFO,stdout
  
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=logging.AppServerPatternLayout
  log4j.appender.stdout.layout.ConversionPattern=-----------------using log file------------------------%nTime:      %d%nHost:      %h%nServer:    %s%nComponent: %b%nVersion:   %v%nPriority:  %p%nThread Id: %t%nContext:   %x%nMessage:   %m%n
  </code></pre>
  
  <h3>Main.java logging without a log4j properties file</h3>
  
  <pre><code>AppServerLoggerFactory factory;
  factory = new AppServerLoggerFactory("MyServer", "MyComponent", "1.0");
  AppServerLogger.setFactory(factory);
  Logger logger = AppServerLogger.getLogger("some.log");
  PatternLayout layout = new AppServerPatternLayout( formatString );
  logger.addAppender( new ConsoleAppender(layout) );
  logger.info("Hello");
  </code></pre>
  
  <h3>Main.java logging with a log4j properties file</h3>
  
  <pre><code>PropertyConfigurator.configure("customlog.properties");
  AppServerLoggerFactory factory;
  factory = new AppServerLoggerFactory("MyServer", "MyComponent", "1.0");
  AppServerLogger.setFactory(factory);
  Logger logger = AppServerLogger.getLogger("some.log");
  logger.info("Hello");
  </code></pre>
  
  <h3>Expected output</h3>
  
  <pre><code>----------------using in code appender----------------------
  Time:      2009-11-06 12:55:05,785
  Host:      M1330
  Server:    MyServer
  Component: MyComponent
  Version:   1.0
  Priority:  INFO
  Thread Id: main
  Context:   
  Message:   logging config from code
  </code></pre>
  
  <h3>Actual output</h3>
  
  <pre><code>-----------------using log file------------------------
  Time:      2009-11-06 12:56:17,983
  Host:      
  Server:    
  Component: 
  Version:   
  Priority:  INFO
  Thread Id: main
  Context:   
  Message:   logging config from customlog.properties
  </code></pre>
  
  <h1>Solution</h1>
  
  <p>Using MDC you can add custom fields like</p>
  
  <pre><code>MDC.put("Version", versionName);
  Logger log = LogManager.getLogger("some.log");        
  log.info("Hello");
  </code></pre>
  
  <p>and pull it out in the log4j.properties with a UPPER case X</p>
  
  <pre><code>log4j.appender.stdout.layout.ConversionPattern=%X{Version}
  </code></pre>