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.filters; 015 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018import org.slf4j.MDC; 019import org.slf4j.Marker; 020import org.slf4j.MarkerFactory; 021 022import ch.qos.logback.classic.LoggerContext; 023import ch.qos.logback.classic.joran.JoranConfigurator; 024import ch.qos.logback.core.joran.spi.JoranException; 025 026public class FilterEvents { 027 028 public static void main(String[] args) throws InterruptedException { 029 if (args.length == 0) { 030 System.out.println("A configuration file must be passed as a parameter."); 031 return; 032 } 033 034 Logger logger = (Logger) LoggerFactory.getLogger(FilterEvents.class); 035 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 036 037 try { 038 JoranConfigurator configurator = new JoranConfigurator(); 039 configurator.setContext(lc); 040 lc.reset(); 041 configurator.doConfigure(args[0]); 042 } catch (JoranException je) { 043 je.printStackTrace(); 044 } 045 046 for (int i = 0; i < 10; i++) { 047 if (i == 3) { 048 MDC.put("username", "sebastien"); 049 logger.debug("logging statement {}", i); 050 MDC.remove("username"); 051 } else if (i == 6) { 052 Marker billing = MarkerFactory.getMarker("billing"); 053 logger.error(billing, "billing statement {}", i); 054 } else { 055 logger.info("logging statement {}", i); 056 } 057 } 058 } 059}