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 java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
23 import java.io.ObjectInputStream;
24 import java.net.Socket;
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.spi.LoggerRepository;
28 import org.apache.log4j.spi.LoggingEvent;
30 // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
33 Read {@link LoggingEvent} objects sent from a remote client using
34 Sockets (TCP). These logging events are logged according to local
35 policy, as if they were generated locally.
37 <p>For example, the socket node might decide to log events to a
38 local file and also resent them to a second socket node.
40 @author Ceki Gülcü
44 public class SocketNode implements Runnable {
47 LoggerRepository hierarchy;
48 ObjectInputStream ois;
50 static Logger logger = Logger.getLogger(SocketNode.class);
52 public SocketNode(Socket socket, LoggerRepository hierarchy) {
54 this.hierarchy = hierarchy;
56 ois = new ObjectInputStream(
57 new BufferedInputStream(socket.getInputStream()));
58 } catch(InterruptedIOException e) {
59 Thread.currentThread().interrupt();
60 logger.error("Could not open ObjectInputStream to "+socket, e);
61 } catch(IOException e) {
62 logger.error("Could not open ObjectInputStream to "+socket, e);
63 } catch(RuntimeException e) {
64 logger.error("Could not open ObjectInputStream to "+socket, e);
70 //System.err.println("-------------------------Finalize called");
71 // System.err.flush();
81 // read an event from the wire
82 event = (LoggingEvent) ois.readObject();
83 // get a logger from the hierarchy. The name of the logger is taken to be the name contained in the event.
84 remoteLogger = hierarchy.getLogger(event.getLoggerName());
85 //event.logger = remoteLogger;
86 // apply the logger-level filter
87 if(event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
88 // finally log the event as if was generated locally
89 remoteLogger.callAppenders(event);
93 } catch(java.io.EOFException e) {
94 logger.info("Caught java.io.EOFException closing conneciton.");
95 } catch(java.net.SocketException e) {
96 logger.info("Caught java.net.SocketException closing conneciton.");
97 } catch(InterruptedIOException e) {
98 Thread.currentThread().interrupt();
99 logger.info("Caught java.io.InterruptedIOException: "+e);
100 logger.info("Closing connection.");
101 } catch(IOException e) {
102 logger.info("Caught java.io.IOException: "+e);
103 logger.info("Closing connection.");
104 } catch(Exception e) {
105 logger.error("Unexpected exception. Closing conneciton.", e);
110 } catch(Exception e) {
111 logger.info("Could not close connection.", e);
114 if (socket != null) {
117 } catch(InterruptedIOException e) {
118 Thread.currentThread().interrupt();
119 } catch(IOException ex) {