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.receivers.socket; 015 016import java.io.BufferedReader; 017import java.io.InputStreamReader; 018 019import org.slf4j.Logger; 020import org.slf4j.LoggerFactory; 021 022import ch.qos.logback.classic.LoggerContext; 023import ch.qos.logback.classic.joran.JoranConfigurator; 024 025/** 026 * This application loads a configuration containing some form of 027 * socket appender and then allows the user to enter messages 028 * which will be relayed to remote clients via the appender. 029 */ 030public class AppenderExample { 031 032 static void usage(String msg) { 033 System.err.println(msg); 034 System.err.println("Usage: java " + AppenderExample.class.getName() + " configFile\n" + " configFile a logback configuration file" 035 + " in XML format."); 036 System.exit(1); 037 } 038 039 static public void main(String[] args) throws Exception { 040 if (args.length != 1) { 041 usage("Wrong number of arguments."); 042 } 043 044 String configFile = args[0]; 045 046 if (configFile.endsWith(".xml")) { 047 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 048 lc.reset(); 049 JoranConfigurator configurator = new JoranConfigurator(); 050 configurator.setContext(lc); 051 configurator.doConfigure(configFile); 052 } 053 054 Logger logger = LoggerFactory.getLogger(AppenderExample.class); 055 056 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 057 058 while (true) { 059 System.out.println("Type a message to send to remote clients. Type 'q' to quit."); 060 061 String s = reader.readLine(); 062 063 if (s.equals("q")) { 064 break; 065 } else { 066 logger.debug(s); 067 } 068 } 069 070 ((LoggerContext) LoggerFactory.getILoggerFactory()).stop(); 071 } 072 073}