001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic;
015
016import ch.qos.logback.classic.layout.TTLLLayout;
017import ch.qos.logback.classic.spi.Configurator;
018import ch.qos.logback.classic.spi.ILoggingEvent;
019import ch.qos.logback.core.ConsoleAppender;
020import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
021import ch.qos.logback.core.spi.ContextAwareBase;
022
023/**
024 * BasicConfigurator configures logback-classic by attaching a 
025 * {@link ConsoleAppender} to the root logger. The console appender's layout 
026 * is set to a {@link ch.qos.logback.classic.layout.TTLLLayout TTLLLayout}.
027 * 
028 * @author Ceki Gülcü
029 */
030public class BasicConfigurator extends ContextAwareBase implements Configurator {
031
032    public BasicConfigurator() {
033    }
034
035    public void configure(LoggerContext lc) {
036        addInfo("Setting up default configuration.");
037        
038        ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
039        ca.setContext(lc);
040        ca.setName("console");
041        LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
042        encoder.setContext(lc);
043        
044 
045        // same as 
046        // PatternLayout layout = new PatternLayout();
047        // layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
048        TTLLLayout layout = new TTLLLayout();
049 
050        layout.setContext(lc);
051        layout.start();
052        encoder.setLayout(layout);
053        
054        ca.setEncoder(encoder);
055        ca.start();
056        
057        Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
058        rootLogger.addAppender(ca);
059    }
060}