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.spi.ILoggingEvent;
19  import ch.qos.logback.core.ConsoleAppender;
20  import ch.qos.logback.core.status.InfoStatus;
21  import ch.qos.logback.core.status.StatusManager;
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  PatternLayout} with the pattern 
27   * "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n".
28   * 
29   * @author Ceki Gulcu
30   */
31  public class BasicConfigurator {
32  
33    final static BasicConfigurator hiddenSingleton = new BasicConfigurator();
34      
35    private BasicConfigurator() {
36    }
37    
38    public static void configure(LoggerContext lc) {
39      StatusManager sm = lc.getStatusManager();
40      if(sm != null)  {
41       sm.add(new InfoStatus("Setting up default configuration.", lc));
42      }
43      ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
44      ca.setContext(lc);
45      ca.setName("console");
46      PatternLayout pl = new PatternLayout();
47      pl.setContext(lc);
48      pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
49      pl.start();
50  
51      ca.setLayout(pl);
52      ca.start();
53      Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
54      rootLogger.addAppender(ca);
55    }
56  
57    public static void configureDefaultContext() {
58      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
59      configure(lc);
60    }
61  }