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