PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/net/SocketAppender.java

#
Java | 474 lines | 231 code | 49 blank | 194 comment | 30 complexity | c83e2fbc9e5d4851e2084bb6816c1a58 MD5 | raw file
Possible License(s): Apache-2.0
  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. // Contributors: Dan MacDonald <dan@redknee.com>
  18. package org.apache.log4j.net;
  19. import java.io.IOException;
  20. import java.io.ObjectOutputStream;
  21. import java.io.InterruptedIOException;
  22. import java.net.InetAddress;
  23. import java.net.Socket;
  24. import org.apache.log4j.AppenderSkeleton;
  25. import org.apache.log4j.helpers.LogLog;
  26. import org.apache.log4j.spi.ErrorCode;
  27. import org.apache.log4j.spi.LoggingEvent;
  28. /**
  29. Sends {@link LoggingEvent} objects to a remote a log server,
  30. usually a {@link SocketNode}.
  31. <p>The SocketAppender has the following properties:
  32. <ul>
  33. <p><li>If sent to a {@link SocketNode}, remote logging is
  34. non-intrusive as far as the log event is concerned. In other
  35. words, the event will be logged with the same time stamp, {@link
  36. org.apache.log4j.NDC}, location info as if it were logged locally by
  37. the client.
  38. <p><li>SocketAppenders do not use a layout. They ship a
  39. serialized {@link LoggingEvent} object to the server side.
  40. <p><li>Remote logging uses the TCP protocol. Consequently, if
  41. the server is reachable, then log events will eventually arrive
  42. at the server.
  43. <p><li>If the remote server is down, the logging requests are
  44. simply dropped. However, if and when the server comes back up,
  45. then event transmission is resumed transparently. This
  46. transparent reconneciton is performed by a <em>connector</em>
  47. thread which periodically attempts to connect to the server.
  48. <p><li>Logging events are automatically <em>buffered</em> by the
  49. native TCP implementation. This means that if the link to server
  50. is slow but still faster than the rate of (log) event production
  51. by the client, the client will not be affected by the slow
  52. network connection. However, if the network connection is slower
  53. then the rate of event production, then the client can only
  54. progress at the network rate. In particular, if the network link
  55. to the the server is down, the client will be blocked.
  56. <p>On the other hand, if the network link is up, but the server
  57. is down, the client will not be blocked when making log requests
  58. but the log events will be lost due to server unavailability.
  59. <p><li>Even if a <code>SocketAppender</code> is no longer
  60. attached to any category, it will not be garbage collected in
  61. the presence of a connector thread. A connector thread exists
  62. only if the connection to the server is down. To avoid this
  63. garbage collection problem, you should {@link #close} the the
  64. <code>SocketAppender</code> explicitly. See also next item.
  65. <p>Long lived applications which create/destroy many
  66. <code>SocketAppender</code> instances should be aware of this
  67. garbage collection problem. Most other applications can safely
  68. ignore it.
  69. <p><li>If the JVM hosting the <code>SocketAppender</code> exits
  70. before the <code>SocketAppender</code> is closed either
  71. explicitly or subsequent to garbage collection, then there might
  72. be untransmitted data in the pipe which might be lost. This is a
  73. common problem on Windows based systems.
  74. <p>To avoid lost data, it is usually sufficient to {@link
  75. #close} the <code>SocketAppender</code> either explicitly or by
  76. calling the {@link org.apache.log4j.LogManager#shutdown} method
  77. before exiting the application.
  78. </ul>
  79. @author Ceki G&uuml;lc&uuml;
  80. @since 0.8.4 */
  81. public class SocketAppender extends AppenderSkeleton {
  82. /**
  83. The default port number of remote logging server (4560).
  84. @since 1.2.15
  85. */
  86. static public final int DEFAULT_PORT = 4560;
  87. /**
  88. The default reconnection delay (30000 milliseconds or 30 seconds).
  89. */
  90. static final int DEFAULT_RECONNECTION_DELAY = 30000;
  91. /**
  92. We remember host name as String in addition to the resolved
  93. InetAddress so that it can be returned via getOption().
  94. */
  95. String remoteHost;
  96. /**
  97. * The MulticastDNS zone advertised by a SocketAppender
  98. */
  99. public static final String ZONE = "_log4j_obj_tcpconnect_appender.local.";
  100. InetAddress address;
  101. int port = DEFAULT_PORT;
  102. ObjectOutputStream oos;
  103. int reconnectionDelay = DEFAULT_RECONNECTION_DELAY;
  104. boolean locationInfo = false;
  105. private String application;
  106. private Connector connector;
  107. int counter = 0;
  108. // reset the ObjectOutputStream every 70 calls
  109. //private static final int RESET_FREQUENCY = 70;
  110. private static final int RESET_FREQUENCY = 1;
  111. private boolean advertiseViaMulticastDNS;
  112. private ZeroConfSupport zeroConf;
  113. public SocketAppender() {
  114. }
  115. /**
  116. Connects to remote server at <code>address</code> and <code>port</code>.
  117. */
  118. public SocketAppender(InetAddress address, int port) {
  119. this.address = address;
  120. this.remoteHost = address.getHostName();
  121. this.port = port;
  122. connect(address, port);
  123. }
  124. /**
  125. Connects to remote server at <code>host</code> and <code>port</code>.
  126. */
  127. public SocketAppender(String host, int port) {
  128. this.port = port;
  129. this.address = getAddressByName(host);
  130. this.remoteHost = host;
  131. connect(address, port);
  132. }
  133. /**
  134. Connect to the specified <b>RemoteHost</b> and <b>Port</b>.
  135. */
  136. public void activateOptions() {
  137. if (advertiseViaMulticastDNS) {
  138. zeroConf = new ZeroConfSupport(ZONE, port, getName());
  139. zeroConf.advertise();
  140. }
  141. connect(address, port);
  142. }
  143. /**
  144. * Close this appender.
  145. *
  146. * <p>This will mark the appender as closed and call then {@link
  147. * #cleanUp} method.
  148. * */
  149. synchronized public void close() {
  150. if(closed)
  151. return;
  152. this.closed = true;
  153. if (advertiseViaMulticastDNS) {
  154. zeroConf.unadvertise();
  155. }
  156. cleanUp();
  157. }
  158. /**
  159. * Drop the connection to the remote host and release the underlying
  160. * connector thread if it has been created
  161. * */
  162. public void cleanUp() {
  163. if(oos != null) {
  164. try {
  165. oos.close();
  166. } catch(IOException e) {
  167. if (e instanceof InterruptedIOException) {
  168. Thread.currentThread().interrupt();
  169. }
  170. LogLog.error("Could not close oos.", e);
  171. }
  172. oos = null;
  173. }
  174. if(connector != null) {
  175. //LogLog.debug("Interrupting the connector.");
  176. connector.interrupted = true;
  177. connector = null; // allow gc
  178. }
  179. }
  180. void connect(InetAddress address, int port) {
  181. if(this.address == null)
  182. return;
  183. try {
  184. // First, close the previous connection if any.
  185. cleanUp();
  186. oos = new ObjectOutputStream(new Socket(address, port).getOutputStream());
  187. } catch(IOException e) {
  188. if (e instanceof InterruptedIOException) {
  189. Thread.currentThread().interrupt();
  190. }
  191. String msg = "Could not connect to remote log4j server at ["
  192. +address.getHostName()+"].";
  193. if(reconnectionDelay > 0) {
  194. msg += " We will try again later.";
  195. fireConnector(); // fire the connector thread
  196. } else {
  197. msg += " We are not retrying.";
  198. errorHandler.error(msg, e, ErrorCode.GENERIC_FAILURE);
  199. }
  200. LogLog.error(msg);
  201. }
  202. }
  203. public void append(LoggingEvent event) {
  204. if(event == null)
  205. return;
  206. if(address==null) {
  207. errorHandler.error("No remote host is set for SocketAppender named \""+
  208. this.name+"\".");
  209. return;
  210. }
  211. if(oos != null) {
  212. try {
  213. if(locationInfo) {
  214. event.getLocationInformation();
  215. }
  216. if (application != null) {
  217. event.setProperty("application", application);
  218. }
  219. event.getNDC();
  220. event.getThreadName();
  221. event.getMDCCopy();
  222. event.getRenderedMessage();
  223. event.getThrowableStrRep();
  224. oos.writeObject(event);
  225. //LogLog.debug("=========Flushing.");
  226. oos.flush();
  227. if(++counter >= RESET_FREQUENCY) {
  228. counter = 0;
  229. // Failing to reset the object output stream every now and
  230. // then creates a serious memory leak.
  231. //System.err.println("Doing oos.reset()");
  232. oos.reset();
  233. }
  234. } catch(IOException e) {
  235. if (e instanceof InterruptedIOException) {
  236. Thread.currentThread().interrupt();
  237. }
  238. oos = null;
  239. LogLog.warn("Detected problem with connection: "+e);
  240. if(reconnectionDelay > 0) {
  241. fireConnector();
  242. } else {
  243. errorHandler.error("Detected problem with connection, not reconnecting.", e,
  244. ErrorCode.GENERIC_FAILURE);
  245. }
  246. }
  247. }
  248. }
  249. public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
  250. this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
  251. }
  252. public boolean isAdvertiseViaMulticastDNS() {
  253. return advertiseViaMulticastDNS;
  254. }
  255. void fireConnector() {
  256. if(connector == null) {
  257. LogLog.debug("Starting a new connector thread.");
  258. connector = new Connector();
  259. connector.setDaemon(true);
  260. connector.setPriority(Thread.MIN_PRIORITY);
  261. connector.start();
  262. }
  263. }
  264. static
  265. InetAddress getAddressByName(String host) {
  266. try {
  267. return InetAddress.getByName(host);
  268. } catch(Exception e) {
  269. if (e instanceof InterruptedIOException || e instanceof InterruptedException) {
  270. Thread.currentThread().interrupt();
  271. }
  272. LogLog.error("Could not find address of ["+host+"].", e);
  273. return null;
  274. }
  275. }
  276. /**
  277. * The SocketAppender does not use a layout. Hence, this method
  278. * returns <code>false</code>.
  279. * */
  280. public boolean requiresLayout() {
  281. return false;
  282. }
  283. /**
  284. * The <b>RemoteHost</b> option takes a string value which should be
  285. * the host name of the server where a {@link SocketNode} is
  286. * running.
  287. * */
  288. public void setRemoteHost(String host) {
  289. address = getAddressByName(host);
  290. remoteHost = host;
  291. }
  292. /**
  293. Returns value of the <b>RemoteHost</b> option.
  294. */
  295. public String getRemoteHost() {
  296. return remoteHost;
  297. }
  298. /**
  299. The <b>Port</b> option takes a positive integer representing
  300. the port where the server is waiting for connections.
  301. */
  302. public void setPort(int port) {
  303. this.port = port;
  304. }
  305. /**
  306. Returns value of the <b>Port</b> option.
  307. */
  308. public int getPort() {
  309. return port;
  310. }
  311. /**
  312. The <b>LocationInfo</b> option takes a boolean value. If true,
  313. the information sent to the remote host will include location
  314. information. By default no location information is sent to the server.
  315. */
  316. public void setLocationInfo(boolean locationInfo) {
  317. this.locationInfo = locationInfo;
  318. }
  319. /**
  320. Returns value of the <b>LocationInfo</b> option.
  321. */
  322. public boolean getLocationInfo() {
  323. return locationInfo;
  324. }
  325. /**
  326. * The <b>App</b> option takes a string value which should be the name of the
  327. * application getting logged.
  328. * If property was already set (via system property), don't set here.
  329. * @since 1.2.15
  330. */
  331. public void setApplication(String lapp) {
  332. this.application = lapp;
  333. }
  334. /**
  335. * Returns value of the <b>Application</b> option.
  336. * @since 1.2.15
  337. */
  338. public String getApplication() {
  339. return application;
  340. }
  341. /**
  342. The <b>ReconnectionDelay</b> option takes a positive integer
  343. representing the number of milliseconds to wait between each
  344. failed connection attempt to the server. The default value of
  345. this option is 30000 which corresponds to 30 seconds.
  346. <p>Setting this option to zero turns off reconnection
  347. capability.
  348. */
  349. public void setReconnectionDelay(int delay) {
  350. this.reconnectionDelay = delay;
  351. }
  352. /**
  353. Returns value of the <b>ReconnectionDelay</b> option.
  354. */
  355. public int getReconnectionDelay() {
  356. return reconnectionDelay;
  357. }
  358. /**
  359. The Connector will reconnect when the server becomes available
  360. again. It does this by attempting to open a new connection every
  361. <code>reconnectionDelay</code> milliseconds.
  362. <p>It stops trying whenever a connection is established. It will
  363. restart to try reconnect to the server when previously open
  364. connection is droppped.
  365. @author Ceki G&uuml;lc&uuml;
  366. @since 0.8.4
  367. */
  368. class Connector extends Thread {
  369. boolean interrupted = false;
  370. public
  371. void run() {
  372. Socket socket;
  373. while(!interrupted) {
  374. try {
  375. sleep(reconnectionDelay);
  376. LogLog.debug("Attempting connection to "+address.getHostName());
  377. socket = new Socket(address, port);
  378. synchronized(this) {
  379. oos = new ObjectOutputStream(socket.getOutputStream());
  380. connector = null;
  381. LogLog.debug("Connection established. Exiting connector thread.");
  382. break;
  383. }
  384. } catch(InterruptedException e) {
  385. LogLog.debug("Connector interrupted. Leaving loop.");
  386. return;
  387. } catch(java.net.ConnectException e) {
  388. LogLog.debug("Remote host "+address.getHostName()
  389. +" refused connection.");
  390. } catch(IOException e) {
  391. if (e instanceof InterruptedIOException) {
  392. Thread.currentThread().interrupt();
  393. }
  394. LogLog.debug("Could not connect to " + address.getHostName()+
  395. ". Exception is " + e);
  396. }
  397. }
  398. //LogLog.debug("Exiting Connector.run() method.");
  399. }
  400. /**
  401. public
  402. void finalize() {
  403. LogLog.debug("Connector finalize() has been called.");
  404. }
  405. */
  406. }
  407. }