1
2
3
4
5
6
7
8
9
10
11
12
13
14 package chapters.receivers.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
27
28
29
30 public class AppenderExample {
31
32 static void usage(String msg) {
33 System.err.println(msg);
34 System.err.println("Usage: java " + AppenderExample.class.getName() + " configFile\n" + " configFile a logback configuration file"
35 + " in XML format.");
36 System.exit(1);
37 }
38
39 static public void main(String[] args) throws Exception {
40 if (args.length != 1) {
41 usage("Wrong number of arguments.");
42 }
43
44 String configFile = args[0];
45
46 if (configFile.endsWith(".xml")) {
47 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
48 lc.reset();
49 JoranConfigurator configurator = new JoranConfigurator();
50 configurator.setContext(lc);
51 configurator.doConfigure(configFile);
52 }
53
54 Logger logger = LoggerFactory.getLogger(AppenderExample.class);
55
56 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
57
58 while (true) {
59 System.out.println("Type a message to send to remote clients. 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 ((LoggerContext) LoggerFactory.getILoggerFactory()).stop();
71 }
72
73 }