View Javadoc

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