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