View Javadoc

1   /*
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
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
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.
16   */
17  
18  package org.apache.log4j.net;
19  
20  import java.net.Socket;
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.BufferedInputStream;
24  
25  
26  import org.apache.log4j.*;
27  import org.apache.log4j.spi.*;
28  
29  // Contributors:  Moses Hohman <mmhohman@rainbow.uchicago.edu>
30  
31  /***
32     Read {@link LoggingEvent} objects sent from a remote client using
33     Sockets (TCP). These logging events are logged according to local
34     policy, as if they were generated locally.
35  
36     <p>For example, the socket node might decide to log events to a
37     local file and also resent them to a second socket node.
38  
39      @author  Ceki G&uuml;lc&uuml;
40  
41      @since 0.8.4
42  */
43  public class SocketNode implements Runnable {
44  
45    Socket socket;
46    LoggerRepository hierarchy;
47    ObjectInputStream ois;
48  
49    static Logger logger = Logger.getLogger(SocketNode.class);
50  
51    public SocketNode(Socket socket, LoggerRepository hierarchy) {
52      this.socket = socket;
53      this.hierarchy = hierarchy;
54      try {
55        ois = new ObjectInputStream(
56                           new BufferedInputStream(socket.getInputStream()));
57      }
58      catch(Exception e) {
59        logger.error("Could not open ObjectInputStream to "+socket, e);
60      }
61    }
62  
63    //public
64    //void finalize() {
65    //System.err.println("-------------------------Finalize called");
66    // System.err.flush();
67    //}
68  
69    public void run() {
70      LoggingEvent event;
71      Logger remoteLogger;
72  
73      try {
74        if (ois != null) {
75            while(true) {
76  	        // read an event from the wire
77  	        event = (LoggingEvent) ois.readObject();
78  	        // get a logger from the hierarchy. The name of the logger is taken to be the name contained in the event.
79  	        remoteLogger = hierarchy.getLogger(event.getLoggerName());
80  	        //event.logger = remoteLogger;
81  	        // apply the logger-level filter
82  	        if(event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
83  	        // finally log the event as if was generated locally
84  	        remoteLogger.callAppenders(event);
85  	      }
86          }
87        }
88      } catch(java.io.EOFException e) {
89        logger.info("Caught java.io.EOFException closing conneciton.");
90      } catch(java.net.SocketException e) {
91        logger.info("Caught java.net.SocketException closing conneciton.");
92      } catch(IOException e) {
93        logger.info("Caught java.io.IOException: "+e);
94        logger.info("Closing connection.");
95      } catch(Exception e) {
96        logger.error("Unexpected exception. Closing conneciton.", e);
97      } finally {
98        if (ois != null) {
99           try {
100             ois.close();
101          } catch(Exception e) {
102             logger.info("Could not close connection.", e);
103          }
104       }
105       if (socket != null) {
106         try {
107           socket.close();
108         } catch(IOException ex) {
109         }
110       }
111     }
112   }
113 }