directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r165582 - /directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java
Date Mon, 02 May 2005 05:13:12 GMT
Author: trustin
Date: Sun May  1 22:13:11 2005
New Revision: 165582

URL: http://svn.apache.org/viewcvs?rev=165582&view=rev
Log:
Added: LoggingFilter

Added:
    directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java   (with
props)

Added: directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java?rev=165582&view=auto
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java (added)
+++ directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java Sun May
 1 22:13:11 2005
@@ -0,0 +1,148 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.io.filter;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.SessionInitializer;
+import org.apache.mina.io.IoFilter;
+import org.apache.mina.io.IoSession;
+
+/**
+ * Logs all MINA I/O events to {@link Logger}.
+ * <p>
+ * Call {@link #getLogger(IoSession)}, {@link #log(IoSession, String)}, and
+ * {@link #log(IoSession, String, Throwable)} to log protocol-specific messages.
+ * <p>
+ * Set {@link #PREFIX}, {@link #LOGGER}, {@link #LEVEL} session attributes
+ * to override prefix string, logger, and log level. (See {@link SessionInitializer})
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class LoggingFilter implements IoFilter
+{
+    /**
+     * Session attribute key: prefix string
+     */
+    public static final String PREFIX = LoggingFilter.class.getName() + ".prefix";
+
+    /**
+     * Session attribute key: {@link Logger}
+     */
+    public static final String LOGGER = LoggingFilter.class.getName() + ".logger";
+    
+    /**
+     * Session attribute key: {@link Level}
+     */
+    public static final String LEVEL = LoggingFilter.class.getName() + ".level";
+    
+    public static Logger getLogger( IoSession session )
+    {
+        
+        Logger log = (Logger) session.getAttribute( LOGGER );
+        if( log == null )
+        {
+            log = Logger.getLogger( session.getHandler().getClass().getName() );
+            String prefix = ( String ) session.getAttribute( PREFIX );
+            if( prefix != null )
+            {
+                prefix = "[" + session.getRemoteAddress() + "] ";
+                session.setAttribute( PREFIX, prefix );
+            }
+            
+            Level level = ( Level ) session.getAttribute( LEVEL );
+            if( level != null )
+            {
+                level = Level.INFO;
+                session.setAttribute( LEVEL, level );
+            }
+                
+            session.setAttribute( LOGGER, log );
+        }
+        
+        return log;
+    }
+
+    public static void log( IoSession session, String message )
+    {
+        Logger log = getLogger( session );
+        Level level = ( Level ) session.getAttribute( LEVEL );
+        if( log.isLoggable( level ) )
+        {
+            log.log( level, message );
+        }
+    }
+
+    public static void log( IoSession session, String message, Throwable cause )
+    {
+        Logger log = getLogger( session );
+        Level level = ( Level ) session.getAttribute( LEVEL );
+        if( log.isLoggable( level ) )
+        {
+            log.log( level, message, cause );
+        }
+    }
+
+    public void sessionOpened( NextFilter nextFilter, IoSession session )
+    {
+        log( session, "OPENED" );
+        nextFilter.sessionOpened( session );
+    }
+
+    public void sessionClosed( NextFilter nextFilter, IoSession session )
+    {
+        log( session, "CLOSED" );
+        nextFilter.sessionClosed( session );
+    }
+
+    public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status
)
+    {
+        log( session, "IDLE (" + status + ")" );
+        nextFilter.sessionIdle( session, status );
+    }
+
+    public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause
)
+    {
+        log( session, "EXCEPTION", cause );
+        nextFilter.exceptionCaught( session, cause );
+    }
+
+    public void dataRead( NextFilter nextFilter, IoSession session, ByteBuffer buf)
+    {
+        log( session, "READ (" + buf.getHexDump() + ")" );
+        nextFilter.dataRead( session, buf );
+    }
+
+    public void dataWritten( NextFilter nextFilter, IoSession session, Object marker)
+    {
+        log( session, "WRITTEN (" + marker + ")" );
+        nextFilter.dataWritten( session, marker );
+    }
+
+    public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object
marker)
+    {
+        log( session, "WRITE (" + buf.getHexDump() + ", " + marker + ")" );
+        nextFilter.filterWrite( session, buf, marker );
+    }
+}

Propchange: directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision



Mime
View raw message