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.ConfiguratorRank;
019import ch.qos.logback.classic.spi.ILoggingEvent;
020import ch.qos.logback.core.ConsoleAppender;
021import ch.qos.logback.core.Context;
022import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
023import ch.qos.logback.core.spi.ContextAwareBase;
024
025/**
026 * BasicConfigurator configures logback-classic by attaching a
027 * {@link ConsoleAppender} to the root logger. The console appender's layout is
028 * set to a {@link ch.qos.logback.classic.layout.TTLLLayout TTLLLayout}.
029 * 
030 * @author Ceki Gülcü
031 */
032@ConfiguratorRank(value = ConfiguratorRank.FALLBACK)
033public class BasicConfigurator extends ContextAwareBase implements Configurator {
034
035    public BasicConfigurator() {
036    }
037
038    public ExecutionStatus configure(LoggerContext loggerContext) {
039        addInfo("Setting up default configuration.");
040
041        ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
042        ca.setContext(context);
043        ca.setName("console");
044        LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
045        encoder.setContext(context);
046
047        // same as
048        // PatternLayout layout = new PatternLayout();
049        // layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
050        // %msg%n");
051        TTLLLayout layout = new TTLLLayout();
052
053        layout.setContext(context);
054        layout.start();
055        encoder.setLayout(layout);
056
057        ca.setEncoder(encoder);
058        ca.start();
059
060        Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
061        rootLogger.addAppender(ca);
062
063        // let the caller decide
064        return ExecutionStatus.NEUTRAL;
065    }
066}