查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.filter;
21  
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.common.IoFilterAdapter;
24  import org.apache.mina.common.IoSession;
25  import org.apache.mina.util.SessionLog;
26  import org.slf4j.Logger;
27  
28  /**
29   * Logs all MINA protocol events to {@link Logger}.
30   * 
31   * @author The Apache Directory Project (mina-dev@directory.apache.org)
32   * @version $Rev: 587373 $, $Date: 2007-10-23 11:54:05 +0900 (Tue, 23 Oct 2007) $
33   *
34   * @see SessionLog
35   */
36  public class LoggingFilter extends IoFilterAdapter {
37      /**
38       * Session attribute key: prefix string
39       */
40      public static final String PREFIX = SessionLog.PREFIX;
41  
42      /**
43       * Session attribute key: {@link Logger}
44       */
45      public static final String LOGGER = SessionLog.LOGGER;
46  
47      /**
48       * Creates a new instance.
49       */
50      public LoggingFilter() {
51      }
52  
53      public void sessionCreated(NextFilter nextFilter, IoSession session) {
54          SessionLog.info(session, "CREATED");
55          nextFilter.sessionCreated(session);
56      }
57  
58      public void sessionOpened(NextFilter nextFilter, IoSession session) {
59          SessionLog.info(session, "OPENED");
60          nextFilter.sessionOpened(session);
61      }
62  
63      public void sessionClosed(NextFilter nextFilter, IoSession session) {
64          SessionLog.info(session, "CLOSED");
65          nextFilter.sessionClosed(session);
66      }
67  
68      public void sessionIdle(NextFilter nextFilter, IoSession session,
69              IdleStatus status) {
70          if (SessionLog.isInfoEnabled(session)) {
71              SessionLog.info(session, "IDLE: " + status);
72          }
73          nextFilter.sessionIdle(session, status);
74      }
75  
76      public void exceptionCaught(NextFilter nextFilter, IoSession session,
77              Throwable cause) {
78          if (SessionLog.isWarnEnabled(session)) {
79              SessionLog.warn(session, "EXCEPTION:", cause);
80          }
81          nextFilter.exceptionCaught(session, cause);
82      }
83  
84      public void messageReceived(NextFilter nextFilter, IoSession session,
85              Object message) {
86          if (SessionLog.isInfoEnabled(session)) {
87              SessionLog.info(session, "RECEIVED: " + message);
88          }
89          nextFilter.messageReceived(session, message);
90      }
91  
92      public void messageSent(NextFilter nextFilter, IoSession session,
93              Object message) {
94          if (SessionLog.isInfoEnabled(session)) {
95              SessionLog.info(session, "SENT: " + message);
96          }
97          nextFilter.messageSent(session, message);
98      }
99  
100     public void filterWrite(NextFilter nextFilter, IoSession session,
101             WriteRequest writeRequest) {
102         if (SessionLog.isInfoEnabled(session)) {
103             SessionLog.info(session, "WRITE: " + writeRequest);
104         }
105         nextFilter.filterWrite(session, writeRequest);
106     }
107 
108     public void filterClose(NextFilter nextFilter, IoSession session)
109             throws Exception {
110         SessionLog.info(session, "CLOSE");
111         nextFilter.filterClose(session);
112     }
113 }