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.mdc;
015
016import java.net.URL;
017
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020import org.slf4j.MDC;
021
022import ch.qos.logback.classic.LoggerContext;
023import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
024import ch.qos.logback.classic.joran.JoranConfigurator;
025import ch.qos.logback.classic.spi.ILoggingEvent;
026import ch.qos.logback.core.ConsoleAppender;
027import ch.qos.logback.core.joran.spi.JoranException;
028import ch.qos.logback.core.util.Loader;
029import ch.qos.logback.core.util.StatusPrinter;
030
031public class SimpleMDC {
032    static public void main(String[] args) throws Exception {
033        // You can put values in the MDC at any time. Before anything else
034        // we put the first name
035        MDC.put("first", "Dorothy");
036
037        // configure via the configuration file "chapters/mdc/simpleMDC.xml"
038        // which ships with the examples
039        configureViaXML_File();
040
041        // For educational purposes, the same configuration can
042        // be accomplished programmatically.
043        //
044        // programmaticConfiguration();
045
046        Logger logger = LoggerFactory.getLogger(SimpleMDC.class);
047        // We now put the last name
048        MDC.put("last", "Parker");
049
050        // The most beautiful two words in the English language according
051        // to Dorothy Parker:
052        logger.info("Check enclosed.");
053        logger.debug("The most beautiful two words in English.");
054
055        MDC.put("first", "Richard");
056        MDC.put("last", "Nixon");
057        logger.info("I am not a crook.");
058        logger.info("Attributed to the former US president. 17 Nov 1973.");
059    }
060
061    static void programmaticConfiguration() {
062        // Configure logback
063        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
064        loggerContext.reset();
065        PatternLayoutEncoder layout = new PatternLayoutEncoder();
066        layout.setContext(loggerContext);
067        layout.setPattern("%X{first} %X{last} - %m%n");
068        layout.start();
069        ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
070        appender.setContext(loggerContext);
071        appender.setEncoder(layout);
072        appender.start();
073        // cast root logger to c.q.logback.classic.Logger so that we can attach
074        // an appender to it
075        ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("root");
076        root.addAppender(appender);
077    }
078
079    static void configureViaXML_File() {
080        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
081        try {
082            JoranConfigurator configurator = new JoranConfigurator();
083            configurator.setContext(lc);
084            lc.reset();
085            URL url = Loader.getResourceBySelfClassLoader("chapters/mdc/simpleMDC.xml");
086            configurator.doConfigure(url);
087        } catch (JoranException je) {
088            StatusPrinter.print(lc);
089        }
090    }
091
092}