View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.classic;
12  
13  import org.slf4j.LoggerFactory;
14  
15  import ch.qos.logback.classic.spi.LoggingEvent;
16  import ch.qos.logback.core.ConsoleAppender;
17  import ch.qos.logback.core.status.InfoStatus;
18  import ch.qos.logback.core.status.StatusManager;
19  
20  /**
21   * BasicConfigurator configures logback-classic by attaching a 
22   * {@link ConsoleAppender} to the root logger. The console appender's layout 
23   * is set to a {@link  PatternLayout} with the pattern 
24   * "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n".
25   * 
26   * @author Ceki Gulcu
27   */
28  public class BasicConfigurator {
29  
30    final static BasicConfigurator hiddenSingleton = new BasicConfigurator();
31      
32    private BasicConfigurator() {
33    }
34    
35    public static void configure(LoggerContext lc) {
36      StatusManager sm = lc.getStatusManager();
37      if(sm != null)  {
38       sm.add(new InfoStatus("Setting up default configuration.", lc));
39      }
40      ConsoleAppender<LoggingEvent> ca = new ConsoleAppender<LoggingEvent>();
41      ca.setContext(lc);
42      ca.setName("console");
43      PatternLayout pl = new PatternLayout();
44      pl.setContext(lc);
45      pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
46      pl.start();
47  
48      ca.setLayout(pl);
49      ca.start();
50      Logger rootLogger = lc.getLogger(LoggerContext.ROOT_NAME);
51      rootLogger.addAppender(ca);
52    }
53  
54    public static void configureDefaultContext() {
55      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
56      configure(lc);
57    }
58  }