1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.net.server;
15
16 import java.io.EOFException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.Socket;
20
21 import ch.qos.logback.classic.Logger;
22 import ch.qos.logback.classic.LoggerContext;
23 import ch.qos.logback.classic.spi.ILoggingEvent;
24 import ch.qos.logback.core.net.HardenedObjectInputStream;
25 import ch.qos.logback.core.util.CloseUtil;
26
27
28
29
30
31
32
33 class RemoteAppenderStreamClient implements RemoteAppenderClient {
34
35 private final String id;
36 private final Socket socket;
37 private final InputStream inputStream;
38
39 private LoggerContext lc;
40 private Logger logger;
41
42
43
44
45
46
47
48 public RemoteAppenderStreamClient(String id, Socket socket) {
49 this.id = id;
50 this.socket = socket;
51 this.inputStream = null;
52 }
53
54
55
56
57
58
59
60
61
62
63 public RemoteAppenderStreamClient(String id, InputStream inputStream) {
64 this.id = id;
65 this.socket = null;
66 this.inputStream = inputStream;
67 }
68
69
70
71
72 public void setLoggerContext(LoggerContext lc) {
73 this.lc = lc;
74 this.logger = lc.getLogger(getClass().getPackage().getName());
75 }
76
77
78
79
80 public void close() {
81 if (socket == null)
82 return;
83 CloseUtil.closeQuietly(socket);
84 }
85
86
87
88
89 public void run() {
90 logger.info(this + ": connected");
91 HardenedObjectInputStream ois = null;
92 try {
93 ois = createObjectInputStream();
94 while (true) {
95
96 ILoggingEvent event = (ILoggingEvent) ois.readObject();
97
98
99 Logger remoteLogger = lc.getLogger(event.getLoggerName());
100
101 if (remoteLogger.isEnabledFor(event.getLevel())) {
102
103 remoteLogger.callAppenders(event);
104 }
105 }
106 } catch (EOFException ex) {
107
108 assert true;
109 } catch (IOException ex) {
110 logger.info(this + ": " + ex);
111 } catch (ClassNotFoundException ex) {
112 logger.error(this + ": unknown event class");
113 } catch (RuntimeException ex) {
114 logger.error(this + ": " + ex);
115 } finally {
116 if (ois != null) {
117 CloseUtil.closeQuietly(ois);
118 }
119 close();
120 logger.info(this + ": connection closed");
121 }
122 }
123
124 private HardenedObjectInputStream createObjectInputStream() throws IOException {
125 if (inputStream != null) {
126 return new HardenedLoggingEventInputStream(inputStream);
127 }
128 return new HardenedLoggingEventInputStream(socket.getInputStream());
129 }
130
131
132
133
134 @Override
135 public String toString() {
136 return "client " + id;
137 }
138
139 }