001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package chapters.appenders.socket;
015
016import ch.qos.logback.core.util.Duration;
017import org.slf4j.LoggerFactory;
018
019import ch.qos.logback.classic.Logger;
020
021import ch.qos.logback.classic.LoggerContext;
022import ch.qos.logback.classic.net.SocketAppender;
023import ch.qos.logback.core.status.OnConsoleStatusListener;
024import ch.qos.logback.core.util.StatusPrinter;
025import static org.slf4j.Logger.ROOT_LOGGER_NAME;
026
027/**
028 * Created with IntelliJ IDEA. User: ceki Date: 27.06.12 Time: 19:35 To change
029 * this template use File | Settings | File Templates.
030 */
031public class ConsolePluginClient {
032
033    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.";
034
035    static String LOGGER_NAME = "com.acme.myapp.foo";
036    static String UGLY_BETTY_LOGGER_NAME = "com.acme.myapp.UglyBetty";
037    static long SLEEP = 1;
038    static long RUN_LENGTH = 200 * 1000;
039
040    static public void main(String[] args) throws Exception {
041        // Create a SocketAppender connected to hostname:port with a
042        // reconnection delay of 10000 seconds.
043        String hostname = "localhost";
044        int port = 4321;
045        SocketAppender socketAppender = new SocketAppender();
046        socketAppender.setRemoteHost(hostname);
047        socketAppender.setPort(port);
048        socketAppender.setIncludeCallerData(true);
049        socketAppender.setReconnectionDelay(new Duration(10000));
050
051        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
052
053        socketAppender.setContext(lc);
054
055        lc.reset();
056
057        lc.getStatusManager().add(new OnConsoleStatusListener());
058        // SocketAppender options become active only after the execution
059        // of the next statement.
060        socketAppender.start();
061
062        Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
063        rootLogger.addAppender(socketAppender);
064
065        org.slf4j.Logger logger = LoggerFactory.getLogger(LOGGER_NAME);
066
067        UglyBetty ub = new UglyBetty("ugly-betty-thread-234");
068        ub.start();
069        for (int i = 0; i < RUN_LENGTH; i++) {
070            if (i % 3 == 0) {
071                logger.warn(i + " is divisible by 3");
072            } else {
073                toto(logger, i);
074            }
075            Thread.sleep(SLEEP);
076        }
077        ub.join();
078
079        StatusPrinter.print(lc);
080    }
081
082    /**
083     * @param logger
084     * @param i
085     */
086    /**
087     * @param logger
088     * @param i
089     */
090    static void toto(org.slf4j.Logger logger, int i) {
091        logger.debug("this is message number " + i);
092    }
093
094    static class UglyBetty extends Thread {
095        org.slf4j.Logger logger = LoggerFactory.getLogger(UGLY_BETTY_LOGGER_NAME);
096
097        public UglyBetty(String name) {
098            super(name);
099        }
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}