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.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 uses a SocketAppender that log messages to a 027 * server on a host and port specified by the user. It waits for the 028 * user to type a message which will be sent to the server. 029 * */ 030public class SocketClient2 { 031 static void usage(String msg) { 032 System.err.println(msg); 033 System.err.println("Usage: java " + SocketClient2.class.getName() + " configFile\n" + " configFile a logback configuration file" 034 + " in XML format."); 035 System.exit(1); 036 } 037 038 static public void main(String[] args) throws Exception { 039 if (args.length != 1) { 040 usage("Wrong number of arguments."); 041 } 042 043 String configFile = args[0]; 044 045 if (configFile.endsWith(".xml")) { 046 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 047 JoranConfigurator configurator = new JoranConfigurator(); 048 lc.stop(); 049 configurator.setContext(lc); 050 configurator.doConfigure(configFile); 051 } 052 053 Logger logger = LoggerFactory.getLogger(SocketClient2.class); 054 055 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 056 057 while (true) { 058 System.out.println("Type a message to send to log server. Type 'q' to quit."); 059 060 String s = reader.readLine(); 061 062 if (s.equals("q")) { 063 break; 064 } else { 065 logger.debug(s); 066 } 067 } 068 } 069}