1
2
3
4
5
6
7
8
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
25
26
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 }