001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.log4j.net; 019 020import java.io.BufferedInputStream; 021import java.io.IOException; 022import java.io.InterruptedIOException; 023import java.io.ObjectInputStream; 024import java.net.Socket; 025 026import org.apache.log4j.Logger; 027import org.apache.log4j.spi.LoggerRepository; 028import org.apache.log4j.spi.LoggingEvent; 029 030// Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu> 031 032/** 033 Read {@link LoggingEvent} objects sent from a remote client using 034 Sockets (TCP). These logging events are logged according to local 035 policy, as if they were generated locally. 036 037 <p>For example, the socket node might decide to log events to a 038 local file and also resent them to a second socket node. 039 040 @author Ceki Gülcü 041 042 @since 0.8.4 043*/ 044public class SocketNode implements Runnable { 045 046 Socket socket; 047 LoggerRepository hierarchy; 048 ObjectInputStream ois; 049 050 static Logger logger = Logger.getLogger(SocketNode.class); 051 052 public SocketNode(Socket socket, LoggerRepository hierarchy) { 053 this.socket = socket; 054 this.hierarchy = hierarchy; 055 try { 056 ois = new ObjectInputStream( 057 new BufferedInputStream(socket.getInputStream())); 058 } catch(InterruptedIOException e) { 059 Thread.currentThread().interrupt(); 060 logger.error("Could not open ObjectInputStream to "+socket, e); 061 } catch(IOException e) { 062 logger.error("Could not open ObjectInputStream to "+socket, e); 063 } catch(RuntimeException e) { 064 logger.error("Could not open ObjectInputStream to "+socket, e); 065 } 066 } 067 068 //public 069 //void finalize() { 070 //System.err.println("-------------------------Finalize called"); 071 // System.err.flush(); 072 //} 073 074 public void run() { 075 LoggingEvent event; 076 Logger remoteLogger; 077 078 try { 079 if (ois != null) { 080 while(true) { 081 // read an event from the wire 082 event = (LoggingEvent) ois.readObject(); 083 // get a logger from the hierarchy. The name of the logger is taken to be the name contained in the event. 084 remoteLogger = hierarchy.getLogger(event.getLoggerName()); 085 //event.logger = remoteLogger; 086 // apply the logger-level filter 087 if(event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) { 088 // finally log the event as if was generated locally 089 remoteLogger.callAppenders(event); 090 } 091 } 092 } 093 } catch(java.io.EOFException e) { 094 logger.info("Caught java.io.EOFException closing conneciton."); 095 } catch(java.net.SocketException e) { 096 logger.info("Caught java.net.SocketException closing conneciton."); 097 } catch(InterruptedIOException e) { 098 Thread.currentThread().interrupt(); 099 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); 106 } finally { 107 if (ois != null) { 108 try { 109 ois.close(); 110 } catch(Exception e) { 111 logger.info("Could not close connection.", e); 112 } 113 } 114 if (socket != null) { 115 try { 116 socket.close(); 117 } catch(InterruptedIOException e) { 118 Thread.currentThread().interrupt(); 119 } catch(IOException ex) { 120 } 121 } 122 } 123 } 124}