1 | /* |
2 | * @(#) $Id: IoLoggingFilter.java 264677 2005-08-30 02:44:35Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.io.filter; |
20 | |
21 | import java.util.logging.Level; |
22 | import java.util.logging.Logger; |
23 | |
24 | import org.apache.mina.common.ByteBuffer; |
25 | import org.apache.mina.common.IdleStatus; |
26 | import org.apache.mina.io.IoFilter; |
27 | import org.apache.mina.io.IoSession; |
28 | import org.apache.mina.util.SessionLog; |
29 | |
30 | /** |
31 | * Logs all MINA I/O events to {@link Logger}. |
32 | * |
33 | * @author The Apache Directory Project (dev@directory.apache.org) |
34 | * @author Trustin Lee (trustin@apache.org) |
35 | * @version $Rev: 264677 $, $Date: 2005-08-30 11:44:35 +0900 $ |
36 | * |
37 | * @see SessionLog |
38 | */ |
39 | public class IoLoggingFilter implements IoFilter |
40 | { |
41 | /** |
42 | * Session attribute key: prefix string |
43 | */ |
44 | public static final String PREFIX = SessionLog.PREFIX; |
45 | |
46 | /** |
47 | * Session attribute key: {@link Logger} |
48 | */ |
49 | public static final String LOGGER = SessionLog.LOGGER; |
50 | |
51 | private Level defaultLevel = Level.INFO; |
52 | |
53 | /** |
54 | * Creates a new instance. |
55 | */ |
56 | public IoLoggingFilter() |
57 | { |
58 | } |
59 | |
60 | /** |
61 | * Returns the default level of log entry this filter logs. |
62 | */ |
63 | public Level getDefaultLevel() { |
64 | return defaultLevel; |
65 | } |
66 | |
67 | /** |
68 | * Sets the default level of log entry this filter logs. |
69 | */ |
70 | public void setDefaultLevel(Level defaultLevel) { |
71 | if( defaultLevel == null ) |
72 | { |
73 | defaultLevel = Level.INFO; |
74 | } |
75 | this.defaultLevel = defaultLevel; |
76 | } |
77 | |
78 | public void sessionOpened( NextFilter nextFilter, IoSession session ) |
79 | { |
80 | SessionLog.log( defaultLevel, session, "OPENED" ); |
81 | nextFilter.sessionOpened( session ); |
82 | } |
83 | |
84 | public void sessionClosed( NextFilter nextFilter, IoSession session ) |
85 | { |
86 | SessionLog.log( defaultLevel, session, "CLOSED" ); |
87 | nextFilter.sessionClosed( session ); |
88 | } |
89 | |
90 | public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status ) |
91 | { |
92 | SessionLog.log( defaultLevel, session, "IDLE: " + status ); |
93 | nextFilter.sessionIdle( session, status ); |
94 | } |
95 | |
96 | public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause ) |
97 | { |
98 | SessionLog.log( defaultLevel, session, "EXCEPTION:", cause ); |
99 | nextFilter.exceptionCaught( session, cause ); |
100 | } |
101 | |
102 | public void dataRead( NextFilter nextFilter, IoSession session, ByteBuffer buf) |
103 | { |
104 | SessionLog.log( defaultLevel, session, "READ: " + buf.getHexDump() ); |
105 | nextFilter.dataRead( session, buf ); |
106 | } |
107 | |
108 | public void dataWritten( NextFilter nextFilter, IoSession session, Object marker) |
109 | { |
110 | SessionLog.log( defaultLevel, session, "WRITTEN: " + marker ); |
111 | nextFilter.dataWritten( session, marker ); |
112 | } |
113 | |
114 | public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker) |
115 | { |
116 | SessionLog.log( defaultLevel, session, "WRITE: " + marker + ", " + buf.getHexDump() ); |
117 | nextFilter.filterWrite( session, buf, marker ); |
118 | } |
119 | } |