1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.logging;
17
18 import static org.jboss.netty.buffer.ChannelBuffers.*;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.channel.ChannelDownstreamHandler;
22 import org.jboss.netty.channel.ChannelEvent;
23 import org.jboss.netty.channel.ChannelHandler;
24 import org.jboss.netty.channel.ChannelHandlerContext;
25 import org.jboss.netty.channel.ChannelUpstreamHandler;
26 import org.jboss.netty.channel.ExceptionEvent;
27 import org.jboss.netty.channel.MessageEvent;
28 import org.jboss.netty.channel.ChannelHandler.Sharable;
29 import org.jboss.netty.logging.InternalLogLevel;
30 import org.jboss.netty.logging.InternalLogger;
31 import org.jboss.netty.logging.InternalLoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Sharable
46 public class LoggingHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler {
47
48 private static final InternalLogLevel DEFAULT_LEVEL = InternalLogLevel.DEBUG;
49
50 private final InternalLogger logger;
51 private final InternalLogLevel level;
52 private final boolean hexDump;
53
54
55
56
57
58 public LoggingHandler() {
59 this(true);
60 }
61
62
63
64
65
66
67
68 public LoggingHandler(InternalLogLevel level) {
69 this(level, true);
70 }
71
72
73
74
75
76
77
78
79 public LoggingHandler(boolean hexDump) {
80 this(DEFAULT_LEVEL, hexDump);
81 }
82
83
84
85
86
87
88
89
90
91 public LoggingHandler(InternalLogLevel level, boolean hexDump) {
92 if (level == null) {
93 throw new NullPointerException("level");
94 }
95
96 logger = InternalLoggerFactory.getInstance(getClass());
97 this.level = level;
98 this.hexDump = hexDump;
99 }
100
101
102
103
104
105 public LoggingHandler(Class<?> clazz) {
106 this(clazz, true);
107 }
108
109
110
111
112
113
114
115 public LoggingHandler(Class<?> clazz, boolean hexDump) {
116 this(clazz, DEFAULT_LEVEL, hexDump);
117 }
118
119
120
121
122
123
124 public LoggingHandler(Class<?> clazz, InternalLogLevel level) {
125 this(clazz, level, true);
126 }
127
128
129
130
131
132
133
134
135 public LoggingHandler(Class<?> clazz, InternalLogLevel level, boolean hexDump) {
136 if (clazz == null) {
137 throw new NullPointerException("clazz");
138 }
139 if (level == null) {
140 throw new NullPointerException("level");
141 }
142 logger = InternalLoggerFactory.getInstance(clazz);
143 this.level = level;
144 this.hexDump = hexDump;
145 }
146
147
148
149
150
151 public LoggingHandler(String name) {
152 this(name, true);
153 }
154
155
156
157
158
159
160
161 public LoggingHandler(String name, boolean hexDump) {
162 this(name, DEFAULT_LEVEL, hexDump);
163 }
164
165
166
167
168
169
170
171
172 public LoggingHandler(String name, InternalLogLevel level, boolean hexDump) {
173 if (name == null) {
174 throw new NullPointerException("name");
175 }
176 if (level == null) {
177 throw new NullPointerException("level");
178 }
179 logger = InternalLoggerFactory.getInstance(name);
180 this.level = level;
181 this.hexDump = hexDump;
182 }
183
184
185
186
187
188 public InternalLogger getLogger() {
189 return logger;
190 }
191
192
193
194
195
196 public InternalLogLevel getLevel() {
197 return level;
198 }
199
200
201
202
203
204
205
206 public void log(ChannelEvent e) {
207 if (getLogger().isEnabled(level)) {
208 String msg = e.toString();
209
210
211 if (hexDump && e instanceof MessageEvent) {
212 MessageEvent me = (MessageEvent) e;
213 if (me.getMessage() instanceof ChannelBuffer) {
214 ChannelBuffer buf = (ChannelBuffer) me.getMessage();
215 msg = msg + " - (HEXDUMP: " + hexDump(buf) + ')';
216 }
217 }
218
219
220 if (e instanceof ExceptionEvent) {
221 getLogger().log(level, msg, ((ExceptionEvent) e).getCause());
222 } else {
223 getLogger().log(level, msg);
224 }
225 }
226 }
227
228 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
229 throws Exception {
230 log(e);
231 ctx.sendUpstream(e);
232 }
233
234 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
235 throws Exception {
236 log(e);
237 ctx.sendDownstream(e);
238 }
239 }