Details
-
Bug
-
Resolution: Unresolved
-
None
-
1.6.x
-
None
-
Operating System: Mac OS X 10.3
Platform: Macintosh
-
blocker
-
P1
-
238
Description
In my tomcat environment, logback throws NullPointerException occasionally. The satack trace looks like:
Exception in thread "Thread-5" java.lang.NullPointerException
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:135)
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:471)
at ch.qos.logback.classic.Logger.filterAndLog_2(Logger.java:464)
at ch.qos.logback.classic.Logger.debug(Logger.java:542)
at com.alu.ipd.ode.customer.exponentiale.reporting.ram.RamMonitor$RamServerChecker.run(RamMonitor.java:140)
Look at the code of LoggingEvent.java around line 135:
LogbackMDCAdapter logbackMDCAdapter = (LogbackMDCAdapter) MDC
.getMDCAdapter();
mdcPropertyMap = logbackMDCAdapter.getPropertyMap();
Sometime the MDC.getMDCAdapter() returns null, which caused the NullPointerException.
I did a lot of debugging, found that the exception would not happen if I watched the MDC.mdcAdapter with the debugger also found no changed to it.
I believed that this must be a threading issue, and modified the MDC's static initializer as below:
static final MDCAdapter mdcAdapter;
static {
MDCAdapter mdcAdapter1=null;
try
catch (NoClassDefFoundError ncde) {
mdcAdapter1 = new NOPMDCAdapter();
String msg = ncde.getMessage();
if (msg != null && msg.indexOf("org/slf4j/impl/StaticMDCBinder") != -1)
else
{ throw ncde; }} catch (Exception e)
{ // we should never get here Util.report("MDC binding unsuccessful.", e); }finally
{ mdcAdapter = mdcAdapter1; }}
NOTE: The key here is: make the mdcAdapter final. I used a temporary variable mdcAdapter1 and assigned it to mdcAdapter in the finally clause.
After this change, the NullPointerException never happens.
I used slf4j 1.6.2 and logback 0.9.29.