View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.spi.ILoggingEvent;
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.pattern.CompositeConverter;
20  import ch.qos.logback.core.pattern.Converter;
21  import ch.qos.logback.core.pattern.ConverterUtil;
22  import ch.qos.logback.core.pattern.PostCompileProcessor;
23  
24  public class EnsureExceptionHandling implements PostCompileProcessor<ILoggingEvent> {
25  
26      /**
27       * This implementation checks if any of the converters in the chain handles
28       * exceptions. If not, then this method adds a
29       * {@link ExtendedThrowableProxyConverter} instance to the end of the chain.
30       * <p>
31       * This allows appenders using this layout to output exception information event
32       * if the user forgets to add %ex to the pattern. Note that the appenders
33       * defined in the Core package are not aware of exceptions nor LoggingEvents.
34       * <p>
35       * If for some reason the user wishes to NOT print exceptions, then she can add
36       * %nopex to the pattern.
37       * 
38       * 
39       */
40      public void process(Context context, Converter<ILoggingEvent> head) {
41          if (head == null) {
42              // this should never happen
43              throw new IllegalArgumentException("cannot process empty chain");
44          }
45          if (!chainHandlesThrowable(head)) {
46              Converter<ILoggingEvent> tail = ConverterUtil.findTail(head);
47              Converter<ILoggingEvent> exConverter = null;
48              LoggerContext loggerContext = (LoggerContext) context;
49              if (loggerContext.isPackagingDataEnabled()) {
50                  exConverter = new ExtendedThrowableProxyConverter();
51              } else {
52                  exConverter = new ThrowableProxyConverter();
53              }
54              tail.setNext(exConverter);
55          }
56      }
57  
58      /**
59       * This method computes whether a chain of converters handles exceptions or not.
60       * 
61       * @param head The first element of the chain
62       * @return true if it can handle throwables contained in logging events
63       */
64      public boolean chainHandlesThrowable(Converter<ILoggingEvent> head) {
65          Converter<ILoggingEvent> c = head;
66          while (c != null) {
67              if (c instanceof ThrowableHandlingConverter) {
68                  return true;
69              } else if (c instanceof CompositeConverter) {
70                  if (compositeHandlesThrowable((CompositeConverter<ILoggingEvent>) c)) {
71                      return true;
72                  }
73              }
74              c = c.getNext();
75          }
76          return false;
77      }
78  
79      /**
80       * This method computes whether a composite converter handles exceptions or not.
81       *
82       * @param compositeConverter The composite converter
83       * @return true if it can handle throwables contained in logging events
84       */
85      public boolean compositeHandlesThrowable(CompositeConverter<ILoggingEvent> compositeConverter) {
86          Converter<ILoggingEvent> childConverter = compositeConverter.getChildConverter();
87  
88          for (Converter<ILoggingEvent> c = childConverter; c != null; c = c.getNext()) {
89              if (c instanceof ThrowableHandlingConverter) {
90                  return true;
91              } else if (c instanceof CompositeConverter) {
92                  boolean r = compositeHandlesThrowable((CompositeConverter<ILoggingEvent>) c);
93                  if (r)
94                      return true;
95              }
96  
97          }
98          return false;
99      }
100 }