View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package chapters.appenders.socket;
15  
16  import java.io.BufferedReader;
17  import java.io.InputStreamReader;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.classic.joran.JoranConfigurator;
24  
25  /**
26   * This application uses a SocketAppender that log messages to a
27   * server on a host and port specified by the user. It waits for the
28   * user to type a message which will be sent to the server.
29   * */
30  public class SocketClient2 {
31      static void usage(String msg) {
32          System.err.println(msg);
33          System.err.println("Usage: java " + SocketClient2.class.getName() + " configFile\n" + "   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("Type a message to send to log server. Type 'q' to quit.");
59  
60              String s = reader.readLine();
61  
62              if (s.equals("q")) {
63                  break;
64              } else {
65                  logger.debug(s);
66              }
67          }
68      }
69  }