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.appenders.mail; 015 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018import org.slf4j.Marker; 019import org.slf4j.MarkerFactory; 020 021import ch.qos.logback.classic.LoggerContext; 022import ch.qos.logback.classic.joran.JoranConfigurator; 023import ch.qos.logback.core.util.StatusPrinter; 024 025/** 026 * This application generates a number of message many of which are of LEVEL. 027 * However, only one message bears the "NOTIFY_ADMIN" marker. 028 * */ 029public class Marked_EMail { 030 static public void main(String[] args) throws Exception { 031 if (args.length != 1) { 032 usage("Wrong number of arguments."); 033 } 034 035 String configFile = args[0]; 036 037 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 038 JoranConfigurator configurator = new JoranConfigurator(); 039 lc.reset(); 040 configurator.setContext(lc); 041 configurator.doConfigure(configFile); 042 StatusPrinter.printInCaseOfErrorsOrWarnings(lc); 043 044 Logger logger = LoggerFactory.getLogger(Marked_EMail.class); 045 046 int runLength = 100; 047 for (int i = 1; i <= runLength; i++) { 048 if ((i % 10) < 9) { 049 logger.debug("This is a debug message. Message number: " + i); 050 } else { 051 logger.error("This is an error message. Message number: " + i); 052 } 053 } 054 055 Marker notifyAdmin = MarkerFactory.getMarker("NOTIFY_ADMIN"); 056 logger.error(notifyAdmin, "This is a serious an error requiring the admin's attention", new Exception("Just testing")); 057 058 StatusPrinter.printInCaseOfErrorsOrWarnings(lc); 059 } 060 061 static void usage(String msg) { 062 System.err.println(msg); 063 System.err.println("Usage: java " + Marked_EMail.class.getName() + " configFile\n" + " configFile a logback configuration file in XML format."); 064 System.exit(1); 065 } 066}