|
30 | 30 | // Contributors: Moses Hohman <mmhohman@rainbow.uchicago.edu> |
31 | 31 |
|
32 | 32 | /** |
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. |
36 | | -
|
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. |
39 | | -
|
40 | | - @author Ceki Gülcü |
41 | | -
|
42 | | - @since 0.8.4 |
43 | | -*/ |
| 33 | + * Read {@link LoggingEvent} objects sent from a remote client using Sockets |
| 34 | + * (TCP). These logging events are logged according to local policy, as if they |
| 35 | + * were generated locally. |
| 36 | + * |
| 37 | + * <p> |
| 38 | + * For example, the socket node might decide to log events to a local file and |
| 39 | + * also resent them to a second socket node. |
| 40 | + * |
| 41 | + * @author Ceki Gülcü |
| 42 | + * |
| 43 | + * @since 0.8.4 |
| 44 | + */ |
44 | 45 | public class SocketNode implements Runnable { |
45 | 46 |
|
46 | | - Socket socket; |
47 | | - LoggerRepository hierarchy; |
48 | | - ObjectInputStream ois; |
| 47 | + Socket socket; |
| 48 | + LoggerRepository hierarchy; |
| 49 | + ObjectInputStream ois; |
49 | 50 |
|
50 | | - static Logger logger = Logger.getLogger(SocketNode.class); |
| 51 | + static Logger logger = Logger.getLogger(SocketNode.class); |
51 | 52 |
|
52 | | - public SocketNode(Socket socket, LoggerRepository hierarchy) { |
53 | | - this.socket = socket; |
54 | | - this.hierarchy = hierarchy; |
55 | | - try { |
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); |
65 | | - } |
66 | | - } |
| 53 | + public SocketNode(Socket socket, LoggerRepository hierarchy) { |
| 54 | + this.socket = socket; |
| 55 | + this.hierarchy = hierarchy; |
| 56 | + try { |
| 57 | + ois = new ObjectInputStream(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); |
| 65 | + } |
| 66 | + } |
67 | 67 |
|
68 | | - //public |
69 | | - //void finalize() { |
70 | | - //System.err.println("-------------------------Finalize called"); |
71 | | - // System.err.flush(); |
72 | | - //} |
| 68 | + // public |
| 69 | + // void finalize() { |
| 70 | + // System.err.println("-------------------------Finalize called"); |
| 71 | + // System.err.flush(); |
| 72 | + // } |
73 | 73 |
|
74 | | - public void run() { |
75 | | - LoggingEvent event; |
76 | | - Logger remoteLogger; |
| 74 | + public void run() { |
| 75 | + LoggingEvent event; |
| 76 | + Logger remoteLogger; |
77 | 77 |
|
78 | | - try { |
79 | | - if (ois != null) { |
80 | | - while(true) { |
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); |
90 | | - } |
91 | | - } |
92 | | - } |
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); |
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 | | - } |
| 78 | + try { |
| 79 | + if (ois != null) { |
| 80 | + while (true) { |
| 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 |
| 84 | + // name contained in the event. |
| 85 | + remoteLogger = hierarchy.getLogger(event.getLoggerName()); |
| 86 | + // event.logger = remoteLogger; |
| 87 | + // apply the logger-level filter |
| 88 | + if (event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) { |
| 89 | + // finally log the event as if was generated locally |
| 90 | + remoteLogger.callAppenders(event); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } catch (java.io.EOFException e) { |
| 95 | + logger.info("Caught java.io.EOFException closing conneciton."); |
| 96 | + } catch (java.net.SocketException e) { |
| 97 | + logger.info("Caught java.net.SocketException closing conneciton."); |
| 98 | + } catch (InterruptedIOException e) { |
| 99 | + Thread.currentThread().interrupt(); |
| 100 | + logger.info("Caught java.io.InterruptedIOException: " + e); |
| 101 | + logger.info("Closing connection."); |
| 102 | + } catch (IOException e) { |
| 103 | + logger.info("Caught java.io.IOException: " + e); |
| 104 | + logger.info("Closing connection."); |
| 105 | + } catch (Exception e) { |
| 106 | + logger.error("Unexpected exception. Closing conneciton.", e); |
| 107 | + } finally { |
| 108 | + if (ois != null) { |
| 109 | + try { |
| 110 | + ois.close(); |
| 111 | + } catch (Exception e) { |
| 112 | + logger.info("Could not close connection.", e); |
| 113 | + } |
| 114 | + } |
| 115 | + if (socket != null) { |
| 116 | + try { |
| 117 | + socket.close(); |
| 118 | + } catch (InterruptedIOException e) { |
| 119 | + Thread.currentThread().interrupt(); |
| 120 | + } catch (IOException ex) { |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
124 | 125 | } |
0 commit comments