[logback-user] No context given for compiler/Parser Error withSimpleMDC example - FIXED!

Daniel King dking at vurv.com
Sat Apr 12 00:12:46 CEST 2008


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 at qos.ch [mailto:logback-user-bounces at 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 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

_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://qos.ch/mailman/listinfo/logback-user



More information about the Logback-user mailing list