View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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.pattern;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.PatternLayout;
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.classic.spi.LoggingEvent;
22  import ch.qos.logback.core.status.testUtil.StatusChecker;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  //import ch.qos.logback.core.util.StatusPrinter;
28  
29  public class CompositeConverterTest {
30  
31      private PatternLayout pl = new PatternLayout();
32      private LoggerContext lc = new LoggerContext();
33      Logger logger = lc.getLogger(this.getClass());
34      StatusChecker sc = new StatusChecker(lc);
35      
36      
37      @BeforeEach
38      public void setUp() {
39          pl.setContext(lc);
40      }
41  
42      ILoggingEvent makeLoggingEvent(String msg, Exception ex) {
43          return new LoggingEvent(CompositeConverterTest.class.getName(), logger, Level.INFO, msg, ex, null);
44      }
45  
46      
47  
48      @Test
49      public void testLogback1582() {
50          // EVAL_REF is searched within the context, if context is not set (== null), then
51          // a NullPointerException will be thrown
52          pl.setPattern("%m  %replace(%rootException{5, EVAL_REF}){'\\n', 'XYZ'}\"");
53          pl.start();
54          ILoggingEvent le = makeLoggingEvent("assert", new Exception("test"));
55          
56          String result = pl.doLayout(le);
57          sc.assertIsErrorFree();
58          assertTrue(result.contains("XYZ"));
59      }
60  
61  }