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 ch.qos.logback.classic.joran.JoranConfigurator;
17  import ch.qos.logback.classic.pattern.ConverterTest;
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  import ch.qos.logback.classic.spi.LoggingEvent;
20  import ch.qos.logback.classic.testUtil.SampleConverter;
21  import ch.qos.logback.core.Context;
22  import ch.qos.logback.core.joran.spi.JoranException;
23  import ch.qos.logback.core.pattern.PatternLayoutBase;
24  import ch.qos.logback.core.pattern.parser.test.AbstractPatternLayoutBaseTest;
25  import ch.qos.logback.core.testUtil.StringListAppender;
26  import ch.qos.logback.core.util.OptionHelper;
27  import ch.qos.logback.core.util.StatusPrinter;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.slf4j.MDC;
31  
32  import static ch.qos.logback.classic.ClassicTestConstants.ISO_REGEX;
33  import static ch.qos.logback.classic.ClassicTestConstants.MAIN_REGEX;
34  import static org.junit.Assert.*;
35  
36  public class PatternLayoutTest extends AbstractPatternLayoutBaseTest<ILoggingEvent> {
37  
38      private PatternLayout pl = new PatternLayout();
39      private LoggerContext lc = new LoggerContext();
40      Logger logger = lc.getLogger(ConverterTest.class);
41      Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
42  
43      String aMessage = "Some message";
44      
45      ILoggingEvent le;
46  
47      public PatternLayoutTest() {
48          super();
49          Exception ex = new Exception("Bogus exception");
50          le = makeLoggingEvent(aMessage, ex);
51      }
52  
53      @Before
54      public void setUp() {
55          pl.setContext(lc);
56      }
57  
58      ILoggingEvent makeLoggingEvent(String msg, Exception ex) {
59          return new LoggingEvent(ch.qos.logback.core.pattern.FormattingConverter.class.getName(), logger, Level.INFO, msg, ex, null);
60      }
61  
62  
63      public ILoggingEvent getEventObject() {
64          return makeLoggingEvent("Some message", null);
65      }
66  
67        public PatternLayoutBase<ILoggingEvent> getPatternLayoutBase() {
68          return new PatternLayout();
69      }
70  
71      @Test
72      public void testOK() {
73          pl.setPattern("%d %le [%t] %lo{30} - %m%n");
74          pl.start();
75          String val = pl.doLayout(getEventObject());
76          // 2006-02-01 22:38:06,212 INFO [main] c.q.l.pattern.ConverterTest - Some
77          // message
78          // 2010-12-29 19:04:26,137 INFO [pool-1-thread-47] c.q.l.c.pattern.ConverterTest - Some message
79          String regex = ISO_REGEX + " INFO " + MAIN_REGEX + " c.q.l.c.pattern.ConverterTest - Some message\\s*";
80  
81          assertTrue("val=" + val, val.matches(regex));
82      }
83  
84      @Test
85      public void testNoExeptionHandler() {
86          pl.setPattern("%m%n");
87          pl.start();
88          String val = pl.doLayout(le);
89          assertTrue(val.contains("java.lang.Exception: Bogus exception"));
90      }
91  
92      @Test
93      public void testCompositePattern() {
94          pl.setPattern("%-56(%d %lo{20}) - %m%n");
95          pl.start();
96          String val = pl.doLayout(getEventObject());
97          // 2008-03-18 21:55:54,250 c.q.l.c.pattern.ConverterTest - Some message
98          String regex = ISO_REGEX + " c.q.l.c.p.ConverterTest          - Some message\\s*";
99          assertTrue(val.matches(regex));
100     }
101 
102     @Test
103     public void contextProperty() {
104         pl.setPattern("%property{a}");
105         pl.start();
106         lc.putProperty("a", "b");
107 
108         String val = pl.doLayout(getEventObject());
109         assertEquals("b", val);
110     }
111 
112     @Test
113     public void testNopExeptionHandler() {
114         pl.setPattern("%nopex %m%n");
115         pl.start();
116         String val = pl.doLayout(le);
117         assertTrue(!val.contains("java.lang.Exception: Bogus exception"));
118     }
119 
120     @Test
121     public void testWithParenthesis() {
122         pl.setPattern("\\(%msg:%msg\\) %msg");
123         pl.start();
124         le = makeLoggingEvent(aMessage, null);
125         String val = pl.doLayout(le);
126         assertEquals("(Some message:Some message) Some message", val);
127     }
128 
129     @Test
130     public void testWithLettersComingFromLog4j() {
131         // Letters: p = level and c = logger
132         pl.setPattern("%d %p [%t] %c{30} - %m%n");
133         pl.start();
134         String val = pl.doLayout(getEventObject());
135         // 2006-02-01 22:38:06,212 INFO [main] c.q.l.pattern.ConverterTest - Some
136         // message
137         String regex = ClassicTestConstants.ISO_REGEX + " INFO " + MAIN_REGEX + " c.q.l.c.pattern.ConverterTest - Some message\\s*";
138         assertTrue(val.matches(regex));
139     }
140 
141     @Test
142     public void mdcWithDefaultValue() {
143         String pattern = "%msg %mdc{foo} %mdc{bar:-[null]}";
144         pl.setPattern(OptionHelper.substVars(pattern, lc));
145         pl.start();
146         MDC.put("foo", "foo");
147         try {
148             String val = pl.doLayout(getEventObject());
149             assertEquals("Some message foo [null]", val);
150         } finally {
151             MDC.remove("foo");
152         }
153     }
154 
155     @Test
156     public void contextNameTest() {
157         pl.setPattern("%contextName");
158         lc.setName("aValue");
159         pl.start();
160         String val = pl.doLayout(getEventObject());
161         assertEquals("aValue", val);
162     }
163 
164     @Test
165     public void cnTest() {
166         pl.setPattern("%cn");
167         lc.setName("aValue");
168         pl.start();
169         String val = pl.doLayout(getEventObject());
170         assertEquals("aValue", val);
171     }
172 
173     @Override
174     public Context getContext() {
175         return lc;
176     }
177 
178     void configure(String file) throws JoranException {
179         JoranConfigurator jc = new JoranConfigurator();
180         jc.setContext(lc);
181         jc.doConfigure(file);
182     }
183 
184     @Test
185     public void testConversionRuleSupportInPatternLayout() throws JoranException {
186         configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "conversionRule/patternLayout0.xml");
187         root.getAppender("LIST");
188         String msg = "Simon says";
189         logger.debug(msg);
190         StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root.getAppender("LIST");
191         assertNotNull(sla);
192         assertEquals(1, sla.strList.size());
193         assertEquals(SampleConverter.SAMPLE_STR + " - " + msg, sla.strList.get(0));
194     }
195 
196     @Test
197     public void smokeReplace() {
198         pl.setPattern("%replace(a1234b){'\\d{4}', 'XXXX'}");
199         pl.start();
200         StatusPrinter.print(lc);
201         String val = pl.doLayout(getEventObject());
202         assertEquals("aXXXXb", val);
203     }
204 
205     @Test
206     public void replaceNewline() {
207         String pattern = "%replace(A\nB){'\n', '\n\t'}";
208         String substPattern = OptionHelper.substVars(pattern, null, lc);
209         assertEquals(pattern, substPattern);
210         pl.setPattern(substPattern);
211         pl.start();
212         StatusPrinter.print(lc);
213         String val = pl.doLayout(makeLoggingEvent("", null));
214         assertEquals("A\n\tB", val);
215     }
216     
217     @Test
218     public void replaceWithJoran() throws JoranException {
219         configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "pattern/replace0.xml");
220         StatusPrinter.print(lc);
221         root.getAppender("LIST");
222         String msg = "And the number is 4111111111110000, expiring on 12/2010";
223         logger.debug(msg);
224         StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root.getAppender("LIST");
225         assertNotNull(sla);
226         assertEquals(1, sla.strList.size());
227         assertEquals("And the number is XXXX, expiring on 12/2010", sla.strList.get(0));
228     }
229 
230     @Test
231     public void replaceWithJoran_NEWLINE() throws JoranException {
232         lc.putProperty("TAB", "\t");
233         configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "pattern/replaceNewline.xml");
234         StatusPrinter.print(lc);
235         root.getAppender("LIST");
236         String msg = "A\nC";
237         logger.debug(msg);
238         StringListAppender<ILoggingEvent> sla = (StringListAppender<ILoggingEvent>) root.getAppender("LIST");
239         assertNotNull(sla);
240         assertEquals(1, sla.strList.size());
241         assertEquals("A\n\tC", sla.strList.get(0));
242     }
243 }