[logback-user] No context given for compiler/Parser Error with SimpleMDC example

Ceki Gulcu listid at qos.ch
Fri Apr 11 23:58:43 CEST 2008


Hello Daniel,

I could easily reproduce the "%PARSER_ERROR_X" output. Fortunately, it's a bug 
in the SimpleMDC example, instead of in main code.

You have two options.

1) remove the configuration code in SimpleMDC and replace it with a config file.
The code then becomes:

package chapter7;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class SimpleMDC {
   static public void main(String[] args) throws Exception {
     // You can put values in the MDC at any time. We first put the
     // first name
     MDC.put("first", "Dorothy");

     // get another logger
     Logger logger = LoggerFactory.getLogger(SimpleMDC.class);

     // We now put the last name
     MDC.put("last", "Parker");

     // The most beautiful two words in the English language according
     // to Dorothy Parker:
     logger.info("Check enclosed.");
     logger.debug("The most beautiful two words in English.");

     MDC.put("first", "Richard");
     MDC.put("last", "Nixon");
     logger.info("I am not a crook.");
     logger.info("Attributed to the former US president. 17 Nov 1973.");
   }
}

Here is the config file.


<configuration>
   <appender name="STDOUT"
     class="ch.qos.logback.core.ConsoleAppender">
     <layout class="ch.qos.logback.classic.PatternLayout">
       <Pattern>%X{first} %X{last} - %m%n</Pattern>
     </layout>
   </appender>
   <root>
     <level value="debug" />
     <appender-ref ref="STDOUT" />
   </root>
</configuration>



2) Alternatively, modify the existing SimpleMDC example by adding the following 
lines:

  + LoggerContext loggerContext =
             (LoggerContext) LoggerFactory.getILoggerFactory();
  + loggerContext.shutdownAndReset();

  PatternLayout layout = new PatternLayout();
  + layout.setContext(loggerContext);

Anyway, I will fix this problem (in the examples) for the next version of logback.

The missing import in "Ch# 7 SimpleMDC" example has been fixed in SVN trunk.

HTH,

Daniel King wrote:
> Any help would be greatly appreciated since I want my company to change 
> over from log4j to slf4j and logback.  However with this error, I won’t 
> be able to change over to logback only slf4j.  I searched the archives 
> and found no bug or solution for this error:
> 
>  
> 
> LOGBACK: No context given for 
> ch.qos.logback.core.pattern.parser.Compiler at 4a65e0
> 
> 15:10:23.205 [main] INFO  chapter7.SimpleMDC - Check enclosed.
> 
> %PARSER_ERROR_X %PARSER_ERROR_X - 
> %PARSER_ERROR_m%PARSER_ERROR_n15:10:23.220 [main] DEBUG 
> chapter7.SimpleMDC - The most beautiful two words in English.
> 
> %PARSER_ERROR_X %PARSER_ERROR_X - 
> %PARSER_ERROR_m%PARSER_ERROR_n15:10:23.220 [main] INFO  
> chapter7.SimpleMDC - I am not a crook.
> 
> %PARSER_ERROR_X %PARSER_ERROR_X - 
> %PARSER_ERROR_m%PARSER_ERROR_n15:10:23.220 [main] INFO  
> chapter7.SimpleMDC - Attributed to the former US president. 17 Nov 1973.
> 
> %PARSER_ERROR_X %PARSER_ERROR_X - %PARSER_ERROR_m%PARSER_ERROR_n
> 
>  
-- 
Ceki Gülcü
QOS.ch is looking to hire talented developers in Switzerland.  If
interested, please contact c e k i @ q o s . c h




More information about the Logback-user mailing list