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 ch.qos.logback.core.util.Duration;
17  import org.slf4j.LoggerFactory;
18  
19  import ch.qos.logback.classic.Logger;
20  
21  import ch.qos.logback.classic.LoggerContext;
22  import ch.qos.logback.classic.net.SocketAppender;
23  import ch.qos.logback.core.status.OnConsoleStatusListener;
24  import ch.qos.logback.core.util.StatusPrinter;
25  import static org.slf4j.Logger.ROOT_LOGGER_NAME;
26  
27  /**
28   * Created with IntelliJ IDEA. User: ceki Date: 27.06.12 Time: 19:35 To change
29   * this template use File | Settings | File Templates.
30   */
31  public class ConsolePluginClient {
32  
33      static String LONG_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum lectus augue, pulvinar quis cursus nec, imperdiet nec ante. Cras sit amet arcu et enim adipiscing pellentesque. Suspendisse mi felis, dictum a lobortis nec, placerat in diam. Proin lobortis tortor at nunc facilisis aliquet. Praesent eget dignissim orci. Ut iaculis bibendum.";
34  
35      static String LOGGER_NAME = "com.acme.myapp.foo";
36      static String UGLY_BETTY_LOGGER_NAME = "com.acme.myapp.UglyBetty";
37      static long SLEEP = 1;
38      static long RUN_LENGTH = 200 * 1000;
39  
40      static public void main(String[] args) throws Exception {
41          // Create a SocketAppender connected to hostname:port with a
42          // reconnection delay of 10000 seconds.
43          String hostname = "localhost";
44          int port = 4321;
45          SocketAppender socketAppender = new SocketAppender();
46          socketAppender.setRemoteHost(hostname);
47          socketAppender.setPort(port);
48          socketAppender.setIncludeCallerData(true);
49          socketAppender.setReconnectionDelay(new Duration(10000));
50  
51          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
52  
53          socketAppender.setContext(lc);
54  
55          lc.reset();
56  
57          lc.getStatusManager().add(new OnConsoleStatusListener());
58          // SocketAppender options become active only after the execution
59          // of the next statement.
60          socketAppender.start();
61  
62          Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
63          rootLogger.addAppender(socketAppender);
64  
65          org.slf4j.Logger logger = LoggerFactory.getLogger(LOGGER_NAME);
66  
67          UglyBetty ub = new UglyBetty("ugly-betty-thread-234");
68          ub.start();
69          for (int i = 0; i < RUN_LENGTH; i++) {
70              if (i % 3 == 0) {
71                  logger.warn(i + " is divisible by 3");
72              } else {
73                  toto(logger, i);
74              }
75              Thread.sleep(SLEEP);
76          }
77          ub.join();
78  
79          StatusPrinter.print(lc);
80      }
81  
82      /**
83       * @param logger
84       * @param i
85       */
86      /**
87       * @param logger
88       * @param i
89       */
90      static void toto(org.slf4j.Logger logger, int i) {
91          logger.debug("this is message number " + i);
92      }
93  
94      static class UglyBetty extends Thread {
95          org.slf4j.Logger logger = LoggerFactory.getLogger(UGLY_BETTY_LOGGER_NAME);
96  
97          public UglyBetty(String name) {
98              super(name);
99          }
100 
101         public void run() {
102             for (int i = 0; i < RUN_LENGTH; i++) {
103                 if (i % 23 == 0) {
104                     logger.warn(LONG_TEXT);
105                 } else if (i % 47 == 0) {
106                     logger.error("this is an exception", new Exception("test"));
107                 } else {
108                     count(logger, i);
109                 }
110                 try {
111                     Thread.sleep(SLEEP);
112                 } catch (InterruptedException e) {
113                     e.printStackTrace();
114                 }
115             }
116         }
117 
118         void count(org.slf4j.Logger logger, int i) {
119             logger.debug("Betty counts to " + i);
120         }
121     }
122 }