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 ch.qos.logback.core.util.Duration;
20  import org.slf4j.LoggerFactory;
21  
22  import ch.qos.logback.classic.Logger;
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.net.SocketAppender;
25  
26  /**
27   * This application uses a SocketAppender that log messages to a
28   * server on a host and port specified by the user. It waits for the
29   * user to type a message which will be sent to the server.
30   * */
31  public class SocketClient1 {
32      static void usage(String msg) {
33          System.err.println(msg);
34          System.err.println("Usage: java " + SocketClient1.class.getName() + " hostname port\n" + "   hostname the name of the remote log server\n"
35                          + "   port (integer) the port number of the server\n");
36          System.exit(1);
37      }
38  
39      static public void main(String[] args) throws Exception {
40          if (args.length != 2) {
41              usage("Wrong number of arguments.");
42          }
43  
44          String hostName = args[0];
45          int port = Integer.parseInt(args[1]);
46  
47          // Create a SocketAppender connected to hostname:port with a
48          // reconnection delay of 10000 seconds.
49          SocketAppender socketAppender = new SocketAppender();
50          socketAppender.setRemoteHost(hostName);
51          socketAppender.setPort(port);
52          socketAppender.setReconnectionDelay(new Duration(10000));
53          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
54          socketAppender.setContext(lc);
55  
56          // SocketAppender options become active only after the execution
57          // of the next statement.
58          socketAppender.start();
59  
60          Logger logger = (Logger) LoggerFactory.getLogger(SocketClient1.class);
61          logger.addAppender(socketAppender);
62  
63          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
64  
65          while (true) {
66              System.out.println("Type a message to send to log server at " + hostName + ":" + port + ". Type 'q' to quit.");
67  
68              String s = reader.readLine();
69  
70              if (s.equals("q")) {
71                  break;
72              } else {
73                  logger.debug(s);
74              }
75          }
76      }
77  }