26break;
27}
28} catch (InterruptedException e) {
29LogLog.debug("Connector interrupted. Leaving loop.");
30return;
31} catch (java.net.ConnectException e) {
32LogLog.debug("Remote host " + address.getHostName()
33+ " refused connection.");
34} catch (IOException e) {
35if (e instanceof InterruptedIOException) {
36Thread.currentThread().interrupt();
37}
38LogLog.debug("Could not connect to "
39+ address.getHostName() + ". Exception is " + e);
40}
41}
42}
43 }
而后,在每一次日志记录请求时只需将LoggingEvent实例序列化到之前创建的ObjectOutputStream中即可,若该操作失败,则会重新建立Connector线程以隔时检测远程日志服务器可以重新链接。
1 public void append(LoggingEvent event) {
2if (event == null)
3return;
4if (address == null) {
5errorHandler
6.error("No remote host is set for SocketAppender named \""
7+ this.name + "\".");
8return;
9}
10if (oos != null) {
11try {
12if (locationInfo) {
13event.getLocationInformation();
14}
15if (application != null) {
16event.setProperty("application", application);
17}
18event.getNDC();
19event.getThreadName();
20event.getMDCCopy();
21event.getRenderedMessage();
22event.getThrowableStrRep();
23oos.writeObject(event);
24oos.flush();
25if (++counter >= RESET_FREQUENCY) {
26counter = 0;
27// Failing to reset the object output stream every now and
28// then creates a serious memory leak.
29// System.err.println("Doing oos.reset()");
30oos.reset();
31}
32} catch (IOException e) {
33if (e instanceof InterruptedIOException) {
34Thread.currentThread().interrupt();
35}
36oos = null;
37LogLog.warn("Detected problem with connection: " + e);
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-29497-38.html
加油↖