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 chapters.configuration;
015
016/**
017 * Demonstrates programmatic invocation of Joran.
018 * 
019 */
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023import ch.qos.logback.classic.LoggerContext;
024import ch.qos.logback.classic.joran.JoranConfigurator;
025import ch.qos.logback.core.joran.spi.JoranException;
026import ch.qos.logback.core.util.StatusPrinter;
027
028public class MyApp3 {
029    final static Logger logger = LoggerFactory.getLogger(MyApp3.class);
030
031    public static void main(String[] args) {
032        // assume SLF4J is bound to logback in the current environment
033        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
034
035        try {
036            JoranConfigurator configurator = new JoranConfigurator();
037            configurator.setContext(context);
038            // Call context.reset() to clear any previous configuration, e.g. default
039            // configuration. For multi-step configuration, omit calling context.reset().
040            context.reset();
041            configurator.doConfigure(args[0]);
042        } catch (JoranException je) {
043            // StatusPrinter will handle this
044        }
045        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
046
047        logger.info("Entering application.");
048
049        Foo foo = new Foo();
050        foo.doIt();
051        logger.info("Exiting application.");
052    }
053}