KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > net > SocketNode


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.net;
18
19 import java.net.Socket JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.BufferedInputStream JavaDoc;
23
24
25 import org.apache.log4j.*;
26 import org.apache.log4j.spi.*;
27
28 // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu>
29

30 /**
31    Read {@link LoggingEvent} objects sent from a remote client using
32    Sockets (TCP). These logging events are logged according to local
33    policy, as if they were generated locally.
34
35    <p>For example, the socket node might decide to log events to a
36    local file and also resent them to a second socket node.
37
38     @author Ceki G&uuml;lc&uuml;
39
40     @since 0.8.4
41 */

42 public class SocketNode implements Runnable JavaDoc {
43
44   Socket JavaDoc socket;
45   LoggerRepository hierarchy;
46   ObjectInputStream JavaDoc ois;
47
48   static Logger logger = Logger.getLogger(SocketNode.class);
49
50   public SocketNode(Socket JavaDoc socket, LoggerRepository hierarchy) {
51     this.socket = socket;
52     this.hierarchy = hierarchy;
53     try {
54       ois = new ObjectInputStream JavaDoc(
55                          new BufferedInputStream JavaDoc(socket.getInputStream()));
56     }
57     catch(Exception JavaDoc e) {
58       logger.error("Could not open ObjectInputStream to "+socket, e);
59     }
60   }
61
62   //public
63
//void finalize() {
64
//System.err.println("-------------------------Finalize called");
65
// System.err.flush();
66
//}
67

68   public void run() {
69     LoggingEvent event;
70     Logger remoteLogger;
71
72     try {
73       while(true) {
74     // read an event from the wire
75
event = (LoggingEvent) ois.readObject();
76     // get a logger from the hierarchy. The name of the logger is taken to be the name contained in the event.
77
remoteLogger = hierarchy.getLogger(event.getLoggerName());
78     //event.logger = remoteLogger;
79
// apply the logger-level filter
80
if(event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
81       // finally log the event as if was generated locally
82
remoteLogger.callAppenders(event);
83     }
84       }
85     } catch(java.io.EOFException JavaDoc e) {
86       logger.info("Caught java.io.EOFException closing conneciton.");
87     } catch(java.net.SocketException JavaDoc e) {
88       logger.info("Caught java.net.SocketException closing conneciton.");
89     } catch(IOException JavaDoc e) {
90       logger.info("Caught java.io.IOException: "+e);
91       logger.info("Closing connection.");
92     } catch(Exception JavaDoc e) {
93       logger.error("Unexpected exception. Closing conneciton.", e);
94     }
95
96     try {
97       ois.close();
98     } catch(Exception JavaDoc e) {
99       logger.info("Could not close connection.", e);
100     }
101   }
102 }
103
Popular Tags