No context given for compiler/Parser Error with SimpleMDC example

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@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 I'm using the following: Windows XP JDK 1.6 I created a directory named logback. I put the following jars in it: logback-core-0.9.9.jar logback-classic-0.9.9.jar logback-examples-0.9.9.jar slf4j-api-1.5.0.jar (I also tried slf4j-api-1.4.3.jar with same results) I created a bat file named SimpleMDC.bat. It contains the following: java -classpath "slf4j-api-1.5.0.jar;logback-core-0.9.9.jar;logback-classic-0.9.9.jar;lo gback-examples-0.9.9.jar;" chapter7.SimpleMDC I opened a command prompt and ran the bat file with the following output: C:\logback>SimpleMDC C:\logback>java -classpath "slf4j-api-1.5.0.jar;logback-core-0.9.9.jar;logback-classic-0.9.9.jar;lo gback-examples-0.9.9.jar;" chapter7.SimpleMDC LOGBACK: No context given for ch.qos.logback.core.pattern.parser.Compiler@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 It might be of interest that whenever I used 0.8.1, 0.9.1, 0.9.2, 0.9.3, 0.9.4, 0.9.5, 0.9.6 and 0.9.7 core and classic jars I got the following error when running the SimpleMDC code in a Maven 2 project using Eclipse 3.3 I created: SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder". SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details. Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticMDCBinder at org.slf4j.MDC.<clinit>(MDC.java:69) at com.vurv.logging.MDCLogging.main(MDCLogging.java:15) However, whenever I used 0.9.8 and 0.9.9 core and classic jars I got the same parser_error as listed above. Also the Ch# 7 example in the docs is missing this line of code: import ch.qos.logback.classic.spi.LoggingEvent; It is in the SimpleMDC code in the examples jar though. I also took this same project and copied it. Then I changed it over to use slf and log4j. I changed the logback code to log4j like this: Replace this code: // Configure logback PatternLayout layout = new PatternLayout(); layout.setPattern("%X{first} %X{last} - %m%n"); layout.start(); ConsoleAppender<LoggingEvent> appender = new ConsoleAppender<LoggingEvent>(); appender.setLayout(layout); appender.start(); // cast root logger to c.q.logback.classic.Logger so that we can attach an // appender to it ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("root"); root.addAppender(appender); With this code: PatternLayout layout = new PatternLayout(); layout.setConversionPattern("%X{first} %X{last} - %m%n"); ConsoleAppender appender = new ConsoleAppender(layout); BasicConfigurator.configure(appender); The rest of the code is identical. This produced the correct output with slf4j and log4j. Dorothy Parker - Check enclosed. Dorothy Parker - The most beautiful two words in English. Richard Nixon - I am not a crook. Richard Nixon - Attributed to the former US president. 17 Nov 1973. Thanks, Daniel

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@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

Hello Ceki, Thanks for the quick reply and fix! It worked no problem. Here is the code I used as you stated. public static 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"); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); loggerContext.shutdownAndReset(); // Configure logback PatternLayout layout = new PatternLayout(); layout.setContext(loggerContext); layout.setPattern("%X{first} %X{last} - %m%n"); layout.start(); ConsoleAppender<LoggingEvent> appender = new ConsoleAppender<LoggingEvent>(); appender.setLayout(layout); appender.start(); // cast root logger to c.q.logback.classic.Logger so that we can attach an // appender to it ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("root"); root.addAppender(appender); // get another logger Logger logger = (Logger) LoggerFactory.getLogger(MDCLogging.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."); } And the result: Dorothy Parker - Check enclosed. Dorothy Parker - The most beautiful two words in English. Richard Nixon - I am not a crook. Richard Nixon - Attributed to the former US president. 17 Nov 1973. Thanks, Daniel King Vurv The information contained in this message may be privileged and confidential and protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to the message and deleting it from your computer. Thank you. -----Original Message----- From: logback-user-bounces@qos.ch [mailto:logback-user-bounces@qos.ch] On Behalf Of Ceki Gulcu Sent: Friday, April 11, 2008 5:59 PM To: logback users list Subject: Re: [logback-user] No context given for compiler/Parser Error withSimpleMDC example 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@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 _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://qos.ch/mailman/listinfo/logback-user
participants (2)
-
Ceki Gulcu
-
Daniel King