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.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   * This application loads a configuration containing some form of 
27   * socket appender and then allows the user to enter messages
28   * which will be relayed to remote clients via the appender.
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  }