View Javadoc

1   /*
2    *   @(#) $Id: IoLoggingFilter.java 327113 2005-10-21 06:59:15Z 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 org.apache.mina.common.ByteBuffer;
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.io.IoFilter;
24  import org.apache.mina.io.IoSession;
25  import org.apache.mina.util.SessionLog;
26  import org.slf4j.Logger;
27  
28  /***
29   * Logs all MINA I/O events to {@link Logger}.
30   * 
31   * @author The Apache Directory Project (dev@directory.apache.org)
32   * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $
33   * 
34   * @see SessionLog
35   */
36  public class IoLoggingFilter implements IoFilter
37  {
38      /***
39       * Session attribute key: prefix string
40       */
41      public static final String PREFIX = SessionLog.PREFIX;
42  
43      /***
44       * Session attribute key: {@link Logger}
45       */
46      public static final String LOGGER = SessionLog.LOGGER;
47      
48      /***
49       * Creates a new instance.
50       */
51      public IoLoggingFilter()
52      {
53      }
54      
55      public void sessionOpened( NextFilter nextFilter, IoSession session )
56      {
57          SessionLog.info( session, "OPENED" );
58          nextFilter.sessionOpened( session );
59      }
60  
61      public void sessionClosed( NextFilter nextFilter, IoSession session )
62      {
63          SessionLog.info( session, "CLOSED" );
64          nextFilter.sessionClosed( session );
65      }
66  
67      public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status )
68      {
69          SessionLog.info( session, "IDLE: " + status );
70          nextFilter.sessionIdle( session, status );
71      }
72  
73      public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause )
74      {
75          SessionLog.error( session, "EXCEPTION: ", cause );
76          nextFilter.exceptionCaught( session, cause );
77      }
78  
79      public void dataRead( NextFilter nextFilter, IoSession session, ByteBuffer buf)
80      {
81          SessionLog.info( session, "READ: " + buf.getHexDump() );
82          nextFilter.dataRead( session, buf );
83      }
84  
85      public void dataWritten( NextFilter nextFilter, IoSession session, Object marker)
86      {
87          SessionLog.info( session, "WRITTEN: " + marker );
88          nextFilter.dataWritten( session, marker );
89      }
90  
91      public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker)
92      {
93          SessionLog.info( session, "WRITE: " + marker + ", " + buf.getHexDump() );
94          nextFilter.filterWrite( session, buf, marker );
95      }
96  }