View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package chapter4.socket;
12  
13  import java.io.BufferedReader;
14  import java.io.InputStreamReader;
15  
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.joran.JoranConfigurator;
21  
22  
23  /**
24   * This application uses a SocketAppender that log messages to a
25   * server on a host and port specified by the user. It waits for the
26   * user to type a message which will be sent to the server.
27   * */
28  public class SocketClient2 {
29    static void usage(String msg) {
30      System.err.println(msg);
31      System.err.println("Usage: java " + SocketClient2.class.getName() +
32        " configFile\n" +
33        "   configFile a logback configuration file" +
34        "   in XML format.");
35      System.exit(1);
36    }
37  
38    static public void main(String[] args) throws Exception {
39      if (args.length != 1) {
40        usage("Wrong number of arguments.");
41      }
42  
43      String configFile = args[0];
44  
45      if (configFile.endsWith(".xml")) {
46        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
47        JoranConfigurator configurator = new JoranConfigurator();
48        lc.stop();
49        configurator.setContext(lc);
50        configurator.doConfigure(configFile);
51      }
52  
53      Logger logger = LoggerFactory.getLogger(SocketClient2.class);
54  
55      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
56  
57      while (true) {
58        System.out.println(
59          "Type a message to send to log server. Type 'q' to quit.");
60  
61        String s = reader.readLine();
62  
63        if (s.equals("q")) {
64          break;
65        } else {
66          logger.debug(s);
67        }
68      }
69    }
70  }