View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import ch.qos.logback.classic.joran.action.ContextNameAction;
20  import ch.qos.logback.classic.pattern.CallerDataConverter;
21  import ch.qos.logback.classic.pattern.ClassOfCallerConverter;
22  import ch.qos.logback.classic.pattern.ContextNameConverter;
23  import ch.qos.logback.classic.pattern.PropertyConverter;
24  import ch.qos.logback.classic.pattern.DateConverter;
25  import ch.qos.logback.classic.pattern.EnsureExceptionHandling;
26  import ch.qos.logback.classic.pattern.ExtendedThrowableProxyConverter;
27  import ch.qos.logback.classic.pattern.FileOfCallerConverter;
28  import ch.qos.logback.classic.pattern.LevelConverter;
29  import ch.qos.logback.classic.pattern.LineOfCallerConverter;
30  import ch.qos.logback.classic.pattern.LineSeparatorConverter;
31  import ch.qos.logback.classic.pattern.LoggerConverter;
32  import ch.qos.logback.classic.pattern.MDCConverter;
33  import ch.qos.logback.classic.pattern.MarkerConverter;
34  import ch.qos.logback.classic.pattern.MessageConverter;
35  import ch.qos.logback.classic.pattern.MethodOfCallerConverter;
36  import ch.qos.logback.classic.pattern.NopThrowableInformationConverter;
37  import ch.qos.logback.classic.pattern.RelativeTimeConverter;
38  import ch.qos.logback.classic.pattern.RootCauseFirstThrowableProxyConverter;
39  import ch.qos.logback.classic.pattern.ThreadConverter;
40  import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
41  import ch.qos.logback.classic.spi.ILoggingEvent;
42  import ch.qos.logback.core.CoreConstants;
43  import ch.qos.logback.core.pattern.PatternLayoutBase;
44  import ch.qos.logback.core.pattern.parser.Parser;
45  
46  /**
47   * <p>
48   * A flexible layout configurable with pattern string. The goal of this class is
49   * to {@link #format format} a {@link ILoggingEvent} and return the results in a
50   * {#link String}. The format of the result depends on the
51   * <em>conversion pattern</em>.
52   * <p>
53   * For more information about this layout, please refer to the online manual at
54   * http://logback.qos.ch/manual/layouts.html#PatternLayout
55   * 
56   */
57  
58  public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {
59  
60    public static final Map<String, String> defaultConverterMap = new HashMap<String, String>();
61  
62    static {
63      defaultConverterMap.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
64  
65      defaultConverterMap.put("d", DateConverter.class.getName());
66      defaultConverterMap.put("date", DateConverter.class.getName());
67  
68      defaultConverterMap.put("r", RelativeTimeConverter.class.getName());
69      defaultConverterMap.put("relative", RelativeTimeConverter.class.getName());
70  
71      defaultConverterMap.put("level", LevelConverter.class.getName());
72      defaultConverterMap.put("le", LevelConverter.class.getName());
73      defaultConverterMap.put("p", LevelConverter.class.getName());
74  
75      defaultConverterMap.put("t", ThreadConverter.class.getName());
76      defaultConverterMap.put("thread", ThreadConverter.class.getName());
77  
78      defaultConverterMap.put("lo", LoggerConverter.class.getName());
79      defaultConverterMap.put("logger", LoggerConverter.class.getName());
80      defaultConverterMap.put("c", LoggerConverter.class.getName());
81  
82      defaultConverterMap.put("m", MessageConverter.class.getName());
83      defaultConverterMap.put("msg", MessageConverter.class.getName());
84      defaultConverterMap.put("message", MessageConverter.class.getName());
85  
86      defaultConverterMap.put("C", ClassOfCallerConverter.class.getName());
87      defaultConverterMap.put("class", ClassOfCallerConverter.class.getName());
88  
89      defaultConverterMap.put("M", MethodOfCallerConverter.class.getName());
90      defaultConverterMap.put("method", MethodOfCallerConverter.class.getName());
91  
92      defaultConverterMap.put("L", LineOfCallerConverter.class.getName());
93      defaultConverterMap.put("line", LineOfCallerConverter.class.getName());
94  
95      defaultConverterMap.put("F", FileOfCallerConverter.class.getName());
96      defaultConverterMap.put("file", FileOfCallerConverter.class.getName());
97  
98      defaultConverterMap.put("X", MDCConverter.class.getName());
99      defaultConverterMap.put("mdc", MDCConverter.class.getName());
100 
101     defaultConverterMap.put("ex", ThrowableProxyConverter.class.getName());
102     defaultConverterMap.put("exception", ThrowableProxyConverter.class
103         .getName());
104     defaultConverterMap.put("rEx", RootCauseFirstThrowableProxyConverter.class.getName());
105     defaultConverterMap.put("rootException", RootCauseFirstThrowableProxyConverter.class
106         .getName());
107     defaultConverterMap.put("throwable", ThrowableProxyConverter.class
108         .getName());
109 
110     defaultConverterMap.put("xEx", ExtendedThrowableProxyConverter.class.getName());
111     defaultConverterMap.put("xException", ExtendedThrowableProxyConverter.class
112         .getName());
113     defaultConverterMap.put("xThrowable", ExtendedThrowableProxyConverter.class
114         .getName());
115 
116     defaultConverterMap.put("nopex", NopThrowableInformationConverter.class
117         .getName());
118     defaultConverterMap.put("nopexception",
119         NopThrowableInformationConverter.class.getName());
120 
121     defaultConverterMap.put("cn", ContextNameAction.class.getName());
122     defaultConverterMap.put("contextName", ContextNameConverter.class.getName());
123     
124     defaultConverterMap.put("caller", CallerDataConverter.class.getName());
125 
126     defaultConverterMap.put("marker", MarkerConverter.class.getName());
127 
128     defaultConverterMap.put("property", PropertyConverter.class.getName());
129 
130     
131     defaultConverterMap.put("n", LineSeparatorConverter.class.getName());
132   }
133 
134   public PatternLayout() {
135     this.postCompileProcessor = new EnsureExceptionHandling();
136   }
137 
138   public Map<String, String> getDefaultConverterMap() {
139     return defaultConverterMap;
140   }
141 
142   public String doLayout(ILoggingEvent event) {
143     if (!isStarted()) {
144       return CoreConstants.EMPTY_STRING;
145     }
146     return writeLoopOnConverters(event);
147   }
148 }