1
2
3
4
5
6
7
8
9 package net.sf.jour.log4j;
10
11 import java.net.Socket;
12 import java.io.IOException;
13 import java.io.ObjectInputStream;
14 import java.io.BufferedInputStream;
15
16
17 import org.apache.log4j.*;
18 import org.apache.log4j.spi.*;
19
20
21
22 /***
23 Read {@link LoggingEvent} objects sent from a remote client using
24 Sockets (TCP). These logging events are logged according to local
25 policy, as if they were generated locally.
26
27 <p>For example, the socket node might decide to log events to a
28 local file and also resent them to a second socket node.
29
30 @author Ceki Gülcü
31
32 @since 0.8.4
33 */
34 public class ProfilerSocketNode implements Runnable {
35
36 Socket socket;
37 LoggerRepository hierarchy;
38 ObjectInputStream ois;
39
40 static Logger logger = Logger.getLogger(ProfilerSocketNode.class);
41
42 static int instanceCount = 0;
43 int instanceNumber;
44
45 public ProfilerSocketNode(Socket socket, LoggerRepository hierarchy) {
46 this.socket = socket;
47 this.hierarchy = hierarchy;
48 this.instanceNumber = ++instanceCount;
49 try {
50 ois = new ObjectInputStream(
51 new BufferedInputStream(socket.getInputStream()));
52 }
53 catch(Exception e) {
54 logger.error("Could not open ObjectInputStream to "+socket, e);
55 }
56 }
57
58
59
60
61
62
63
64 static long eventCnt = 0;
65 long thisEventCnt = 0;
66
67 public void run() {
68 LoggingEvent event;
69 ProfilerEvent pEvent;
70 ProfilerEventExt pEventExt;
71 Logger remoteLogger;
72 Logger pLogger;
73 long prnCnt = 0;
74 try {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 pLogger = hierarchy.getLogger("Profiler");
91 while (true) {
92 String eventName;
93 long numEvents = 0;
94
95 Object o = ois.readObject();
96 if (o instanceof ProfilerEvent) {
97 pEvent = (ProfilerEvent) o;
98 pLogger.debug(pEvent);
99 eventName = "ProfilerEvent";
100 numEvents = 1;
101 } else if (o instanceof ProfilerEventExt) {
102 pEventExt = (ProfilerEventExt) o;
103 pLogger.debug(pEventExt);
104 eventName = "ProfilerEventExt";
105 numEvents = 1;
106 } else if (o instanceof ProfilerEvent[]) {
107 ProfilerEvent[] events = (ProfilerEvent[]) o;
108 for(int i = 0; i < events.length; i++) {
109 pEvent = events[i];
110 pLogger.debug(pEvent);
111 }
112 eventName = "ProfilerEvent[]" + events.length;
113 numEvents = events.length;
114 } else if (o instanceof ProfilerEventExt[]) {
115 ProfilerEventExt[] events = (ProfilerEventExt[]) o;
116 for(int i = 0; i < events.length; i++) {
117 pEventExt = events[i];
118 pLogger.debug(pEventExt);
119 }
120 eventName = "ProfilerEventExt[]" + events.length;
121 numEvents = events.length;
122 } else if (o instanceof LoggingEvent[]) {
123 LoggingEvent[] events = (LoggingEvent[]) o;
124 for(int i = 0; i < events.length; i++) {
125 event = events[i];
126 remoteLogger = hierarchy.getLogger(event.getLoggerName());
127 if (event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
128
129 remoteLogger.callAppenders(event);
130 }
131 }
132 eventName = "LoggingEvent[]" + events.length;
133 numEvents = events.length;
134 } else {
135 event = (LoggingEvent) o;
136
137
138 remoteLogger = hierarchy.getLogger(event.getLoggerName());
139
140
141 if (event.getLevel().isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
142
143 remoteLogger.callAppenders(event);
144 }
145 eventName = "LoggingEvent";
146 numEvents = 1;
147 }
148
149 eventCnt += numEvents;
150 prnCnt += numEvents;
151 thisEventCnt += numEvents;
152
153 if (prnCnt > 2000) {
154 System.out.println("Cnt=" + eventCnt + " " + this.instanceNumber + " " + eventName);
155 prnCnt = 0;
156 }
157 }
158
159 } catch (java.io.EOFException e) {
160 logger.info("Caught java.io.EOFException closing conneciton.");
161 } catch (java.net.SocketException e) {
162 logger.info("Caught java.net.SocketException closing conneciton.");
163 } catch (IOException e) {
164 logger.info("Caught java.io.IOException: " + e);
165 logger.info("Closing connection.");
166 } catch (Exception e) {
167 logger.error("Unexpected exception. Closing conneciton.", e);
168 }
169
170 logger.info("Event count:" + thisEventCnt + " of " + eventCnt);
171
172 try {
173 ois.close();
174 } catch(Exception e) {
175 logger.info("Could not close connection.", e);
176 }
177 }
178 }