View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 org.slf4j.LoggerFactory;
17  
18  import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
19  import ch.qos.logback.classic.spi.ILoggingEvent;
20  import ch.qos.logback.core.ConsoleAppender;
21  import ch.qos.logback.core.status.InfoStatus;
22  import ch.qos.logback.core.status.StatusManager;
23  
24  /**
25   * BasicConfigurator configures logback-classic by attaching a 
26   * {@link ConsoleAppender} to the root logger. The console appender's layout 
27   * is set to a {@link  PatternLayout} with the pattern 
28   * "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n".
29   * 
30   * @author Ceki Gulcu
31   */
32  public class BasicConfigurator {
33  
34    final static BasicConfigurator hiddenSingleton = new BasicConfigurator();
35      
36    private BasicConfigurator() {
37    }
38    
39    public static void configure(LoggerContext lc) {
40      StatusManager sm = lc.getStatusManager();
41      if(sm != null)  {
42       sm.add(new InfoStatus("Setting up default configuration.", lc));
43      }
44      ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
45      ca.setContext(lc);
46      ca.setName("console");
47      PatternLayoutEncoder pl = new PatternLayoutEncoder();
48      pl.setContext(lc);
49      pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
50      pl.start();
51  
52      ca.setEncoder(pl);
53      ca.start();
54      Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
55      rootLogger.addAppender(ca);
56    }
57  
58    public static void configureDefaultContext() {
59      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
60      configure(lc);
61    }
62  }