2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.net;
20 import org.apache.log4j.AppenderSkeleton;
21 import org.apache.log4j.helpers.LogLog;
22 import org.apache.log4j.spi.LoggingEvent;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.InterruptedIOException;
27 import java.net.ServerSocket;
28 import java.net.Socket;
29 import java.util.Enumeration;
30 import java.util.Iterator;
31 import java.util.Vector;
34 <p>The TelnetAppender is a log4j appender that specializes in
35 writing to a read-only socket. The output is provided in a
36 telnet-friendly way so that a log can be monitored over TCP/IP.
37 Clients using telnet connect to the socket and receive log data.
38 This is handy for remote monitoring, especially when monitoring a
41 <p>Here is a list of the available configuration options:
54 <td>This parameter determines the port to use for announcing log events. The default port is 23 (telnet).</td>
58 @author <a HREF="mailto:jay@v-wave.com">Jay Funnell</a>
61 public class TelnetAppender extends AppenderSkeleton {
63 private static final String EOL = "\r\n";
64 private SocketHandler sh;
65 private int port = 23;
68 This appender requires a layout to format the text to the
69 attached client(s). */
70 public boolean requiresLayout() {
74 /** all of the options have been set, create the socket handler and
75 wait for connections. */
76 public void activateOptions() {
78 sh = new SocketHandler(port);
81 catch(InterruptedIOException e) {
82 Thread.currentThread().interrupt();
84 } catch(IOException e) {
86 } catch(RuntimeException e) {
89 super.activateOptions();
98 void setPort(int port) {
103 /** shuts down the appender. */
104 public void close() {
109 } catch(InterruptedException ex) {
110 Thread.currentThread().interrupt();
115 /** Handles a log event. For this appender, that means writing the
116 message to each connected client. */
117 protected void append(LoggingEvent event) {
119 sh.send(layout.format(event));
120 if(layout.ignoresThrowable()) {
121 String[] s = event.getThrowableStrRep();
123 StringBuffer buf = new StringBuffer();
124 for(int i = 0; i < s.length; i++) {
128 sh.send(buf.toString());
134 //---------------------------------------------------------- SocketHandler:
136 /** The SocketHandler class is used to accept connections from
137 clients. It is threaded so that clients can connect/disconnect
139 protected class SocketHandler extends Thread {
141 private Vector writers = new Vector();
142 private Vector connections = new Vector();
143 private ServerSocket serverSocket;
144 private int MAX_CONNECTIONS = 20;
146 public void finalize() {
151 * make sure we close all network connections when this handler is destroyed.
154 public void close() {
156 for(Enumeration e = connections.elements();e.hasMoreElements();) {
158 ((Socket)e.nextElement()).close();
159 } catch(InterruptedIOException ex) {
160 Thread.currentThread().interrupt();
161 } catch(IOException ex) {
162 } catch(RuntimeException ex) {
168 serverSocket.close();
169 } catch(InterruptedIOException ex) {
170 Thread.currentThread().interrupt();
171 } catch(IOException ex) {
172 } catch(RuntimeException ex) {
176 /** sends a message to each of the clients in telnet-friendly output. */
177 public synchronized void send(final String message) {
178 Iterator ce = connections.iterator();
179 for(Iterator e = writers.iterator();e.hasNext();) {
181 PrintWriter writer = (PrintWriter)e.next();
182 writer.print(message);
183 if(writer.checkError()) {
191 Continually accepts client connections. Client connections
192 are refused when MAX_CONNECTIONS is reached.
195 while(!serverSocket.isClosed()) {
197 Socket newClient = serverSocket.accept();
198 PrintWriter pw = new PrintWriter(newClient.getOutputStream());
199 if(connections.size() < MAX_CONNECTIONS) {
201 connections.addElement(newClient);
202 writers.addElement(pw);
203 pw.print("TelnetAppender v1.0 (" + connections.size()
204 + " active connections)" + EOL + EOL);
208 pw.print("Too many connections." + EOL);
212 } catch(Exception e) {
213 if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
214 Thread.currentThread().interrupt();
216 if (!serverSocket.isClosed()) {
217 LogLog.error("Encountered error while in SocketHandler loop.", e);
224 serverSocket.close();
225 } catch(InterruptedIOException ex) {
226 Thread.currentThread().interrupt();
227 } catch(IOException ex) {
231 public SocketHandler(int port) throws IOException {
232 serverSocket = new ServerSocket(port);
233 setName("TelnetAppender-" + getName() + "-" + port);