View Javadoc

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