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.layout.TTLLLayout;
17  import ch.qos.logback.classic.spi.Configurator;
18  import ch.qos.logback.classic.spi.ILoggingEvent;
19  import ch.qos.logback.core.ConsoleAppender;
20  import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
21  import ch.qos.logback.core.spi.ContextAwareBase;
22  
23  /**
24   * BasicConfigurator configures logback-classic by attaching a 
25   * {@link ConsoleAppender} to the root logger. The console appender's layout 
26   * is set to a {@link ch.qos.logback.classic.layout.TTLLLayout TTLLLayout}.
27   * 
28   * @author Ceki Gülcü
29   */
30  public class BasicConfigurator extends ContextAwareBase implements Configurator {
31  
32      public BasicConfigurator() {
33      }
34  
35      public void configure(LoggerContext lc) {
36          addInfo("Setting up default configuration.");
37          
38          ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
39          ca.setContext(lc);
40          ca.setName("console");
41          LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
42          encoder.setContext(lc);
43          
44   
45          // same as 
46          // PatternLayout layout = new PatternLayout();
47          // layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
48          TTLLLayout layout = new TTLLLayout();
49   
50          layout.setContext(lc);
51          layout.start();
52          encoder.setLayout(layout);
53          
54          ca.setEncoder(encoder);
55          ca.start();
56          
57          Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
58          rootLogger.addAppender(ca);
59      }
60  }