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 ch.qos.logback.classic.net; 015 016import java.io.InputStreamReader; 017 018import org.slf4j.LoggerFactory; 019 020import ch.qos.logback.classic.BasicConfigurator; 021import ch.qos.logback.classic.Logger; 022 023public class SocketMin { 024 025 static Logger logger = (Logger) LoggerFactory.getLogger(SocketMin.class.getName()); 026 static SocketAppender s; 027 028 public static void main(String argv[]) { 029 if (argv.length == 3) { 030 init(argv[0], argv[1]); 031 } else { 032 usage("Wrong number of arguments."); 033 } 034 035 // NDC.push("some context"); 036 if (argv[2].equals("true")) { 037 loop(); 038 } else { 039 test(); 040 } 041 042 s.stop(); 043 } 044 045 static void usage(String msg) { 046 System.err.println(msg); 047 System.err.println("Usage: java " + SocketMin.class + " host port true|false"); 048 System.exit(1); 049 } 050 051 static void init(String host, String portStr) { 052 Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); 053 BasicConfigurator bc = new BasicConfigurator(); 054 bc.setContext(root.getLoggerContext()); 055 bc.configure(root.getLoggerContext()); 056 try { 057 int port = Integer.parseInt(portStr); 058 logger.info("Creating socket appender (" + host + "," + port + ")."); 059 s = new SocketAppender(); 060 s.setRemoteHost(host); 061 s.setPort(port); 062 s.setName("S"); 063 root.addAppender(s); 064 } catch (java.lang.NumberFormatException e) { 065 e.printStackTrace(); 066 usage("Could not interpret port number [" + portStr + "]."); 067 } catch (Exception e) { 068 System.err.println("Could not start!"); 069 e.printStackTrace(); 070 System.exit(1); 071 } 072 } 073 074 static void loop() { 075 Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); 076 InputStreamReader in = new InputStreamReader(System.in); 077 System.out.println("Type 'q' to quit"); 078 int i; 079 int k = 0; 080 while (true) { 081 logger.debug("Message " + k++); 082 logger.info("Message " + k++); 083 logger.warn("Message " + k++); 084 logger.error("Message " + k++, new Exception("Just testing")); 085 try { 086 i = in.read(); 087 } catch (Exception e) { 088 return; 089 } 090 if (i == -1) 091 break; 092 if (i == 'q') 093 break; 094 if (i == 'r') { 095 System.out.println("Removing appender S"); 096 root.detachAppender("S"); 097 } 098 } 099 } 100 101 static void test() { 102 int i = 0; 103 logger.debug("Message " + i++); 104 logger.info("Message " + i++); 105 logger.warn("Message " + i++); 106 logger.error("Message " + i++); 107 logger.debug("Message " + i++, new Exception("Just testing.")); 108 } 109}