mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r477612 - in /mina/trunk/core/src/main/java/org/apache/mina: filter/LoggingFilter.java util/SessionLog.java
Date Tue, 21 Nov 2006 10:37:56 GMT
Author: niklas
Date: Tue Nov 21 02:37:55 2006
New Revision: 477612

URL: http://svn.apache.org/viewvc?view=rev&rev=477612
Log:
* DIRMINA-314: LoggingFilter will now log each event to different loggers. The loggers share
a common prefix which is configurable. By default this prefix will be the name of the logger
used by SessionLog.
* Made SessionLog.getLogger() public and added SessionLog.getPrefix().
* The logger and prefix used by SessionLog can now be configured using the static SessionLog.setPrefix()
and SessionLog.setLogger() methods. This is easier than having to add an attribute to the
session.

Modified:
    mina/trunk/core/src/main/java/org/apache/mina/filter/LoggingFilter.java
    mina/trunk/core/src/main/java/org/apache/mina/util/SessionLog.java

Modified: mina/trunk/core/src/main/java/org/apache/mina/filter/LoggingFilter.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/LoggingFilter.java?view=diff&rev=477612&r1=477611&r2=477612
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/filter/LoggingFilter.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/filter/LoggingFilter.java Tue Nov 21 02:37:55
2006
@@ -21,12 +21,35 @@
 
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.IoFilterAdapter;
+import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.util.SessionLog;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * Logs all MINA protocol events to {@link Logger}.
+ * Logs all MINA protocol events using the {@link SessionLog}. Each event will
+ * be logged to its own {@link Logger}. The names of these loggers will share a 
+ * common prefix. By default the name of the logger used by {@link SessionLog}
+ * will be used as prefix. Use {@link #setLoggerNamePrefix(String)} to override
+ * the default prefix.
+ * <p>
+ * For most of the events the name of the corresponding {@link IoHandler} method
+ * which handles that event will be added to the prefix to form the logger name.
+ * Here's a complete list of the logger names for each event:
+ * <table>
+ *   <tr><td>Event</td><td>Name of logger</td></tr>
+ *   <tr><td>sessionCreated</td><td>&lt;prefix&gt;.sessionCreated</td></tr>
+ *   <tr><td>sessionOpened</td><td>&lt;prefix&gt;.sessionOpened</td></tr>
+ *   <tr><td>sessionClosed</td><td>&lt;prefix&gt;.sessionClosed</td></tr>
+ *   <tr><td>sessionIdle</td><td>&lt;prefix&gt;.sessionIdle</td></tr>
+ *   <tr><td>exceptionCaught</td><td>&lt;prefix&gt;.exceptionCaught</td></tr>
+ *   <tr><td>messageReceived</td><td>&lt;prefix&gt;.messageReceived</td></tr>
+ *   <tr><td>messageSent</td><td>&lt;prefix&gt;.messageSent</td></tr>
+ *   <tr><td>{@link IoSession#write(Object)}</td><td>&lt;prefix&gt;.write</td></tr>
+ *   <tr><td>{@link IoSession#close()}</td><td>&lt;prefix&gt;.close</td></tr>
+ * </table>
+ * </p> 
  * 
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
@@ -45,6 +68,10 @@
      */
     public static final String LOGGER = SessionLog.LOGGER;
     
+    private static final String EVENT_LOGGER = LoggingFilter.class.getName() + ".event";
+    
+    private String loggerNamePrefix = null;
+    
     /**
      * Creates a new instance.
      */
@@ -52,72 +79,116 @@
     {
     }
     
+    /**
+     * Returns the prefix used for the names of the loggers used to log the different 
+     * events. If <code>null</code> the name of the {@link Logger} used by
+     * {@link SessionLog} will be used for the prefix. The default value is 
+     * <code>null</code>.
+     *
+     * @return the prefix or <code>null</code>.
+     */
+    public String getLoggerNamePrefix() {
+        return loggerNamePrefix;
+    }
+
+    /**
+     * Sets the prefix used for the names of the loggers used to log the different 
+     * events. If set to <code>null</code> the name of the {@link Logger} used
by
+     * {@link SessionLog} will be used for the prefix.
+     *
+     * @param loggerNamePrefix the new prefix.
+     */
+    public void setLoggerNamePrefix(String loggerNamePrefix) {
+        this.loggerNamePrefix = loggerNamePrefix;
+    }
+    
     public void sessionCreated( NextFilter nextFilter, IoSession session )
     {
-    	SessionLog.info( session, "CREATED" );
+        SessionLog.info( getLogger( session, "sessionCreated" ), session, "CREATED" );
         nextFilter.sessionCreated( session );
     }
     
     public void sessionOpened( NextFilter nextFilter, IoSession session )
     {
-        SessionLog.info( session, "OPENED" );
+        SessionLog.info( getLogger( session, "sessionOpened" ), session, "OPENED" );
         nextFilter.sessionOpened( session );
     }
 
     public void sessionClosed( NextFilter nextFilter, IoSession session )
     {
-        SessionLog.info( session, "CLOSED" );
+        SessionLog.info( getLogger( session, "sessionClosed" ), session, "CLOSED" );
         nextFilter.sessionClosed( session );
     }
 
     public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status
)
     {
-        if( SessionLog.isInfoEnabled( session ) )
+    	Logger log = getLogger( session, "sessionIdle" );
+        if( log.isInfoEnabled() )
         {
-            SessionLog.info( session, "IDLE: " + status );
+            SessionLog.info( log, session, "IDLE: " + status );
         }
         nextFilter.sessionIdle( session, status );
     }
 
     public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause
)
     {
-        if( SessionLog.isInfoEnabled( session ) )
+    	Logger log = getLogger( session, "exceptionCaught" );
+        if( log.isInfoEnabled() )
         {
-            SessionLog.info( session, "EXCEPTION:", cause );
+            SessionLog.info( log, session, "EXCEPTION:", cause );
         }
         nextFilter.exceptionCaught( session, cause );
     }
 
     public void messageReceived( NextFilter nextFilter, IoSession session, Object message
)
     {
-        if( SessionLog.isInfoEnabled( session ) )
+    	Logger log = getLogger( session, "messageReceived" );
+        if( log.isInfoEnabled() )
         {
-            SessionLog.info( session, "RECEIVED: " + message );
+            SessionLog.info( log, session, "RECEIVED: " + message );
         }
         nextFilter.messageReceived( session, message );
     }
 
     public void messageSent( NextFilter nextFilter, IoSession session, Object message )
     {
-        if( SessionLog.isInfoEnabled( session ) )
+    	Logger log = getLogger( session, "messageSent" );
+        if( log.isInfoEnabled() )
         {
-            SessionLog.info( session, "SENT: " + message );
+            SessionLog.info( log, session, "SENT: " + message );
         }
         nextFilter.messageSent( session, message );
     }
 
     public void filterWrite( NextFilter nextFilter, IoSession session, WriteRequest writeRequest
)
     {
-        if( SessionLog.isInfoEnabled( session ) )
+    	Logger log = getLogger( session, "write" );
+        if( log.isInfoEnabled() )
         {
-            SessionLog.info( session, "WRITE: " + writeRequest );
+            SessionLog.info( log, session, "WRITE: " + writeRequest );
         }
         nextFilter.filterWrite( session, writeRequest );
     }
 
     public void filterClose( NextFilter nextFilter, IoSession session ) throws Exception
     {
-        SessionLog.info( session, "CLOSE" );
+        SessionLog.info( getLogger( session, "close" ), session, "CLOSE" );
         nextFilter.filterClose( session );
+    }
+    
+    private Logger getLogger( IoSession session, String event )
+    {
+    	Logger log = ( Logger ) session.getAttribute( EVENT_LOGGER + "." + event );
+    	if( log == null )
+    	{
+    		String prefix = loggerNamePrefix;
+    		if( prefix == null )
+    		{
+    			prefix = SessionLog.getLogger( session ).getName();
+    		}
+    		log = LoggerFactory.getLogger( prefix + "." + event );
+            session.setAttribute( EVENT_LOGGER + "." + event, log );
+    	}
+    	return log;
     }
 }

Modified: mina/trunk/core/src/main/java/org/apache/mina/util/SessionLog.java
URL: http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/SessionLog.java?view=diff&rev=477612&r1=477611&r2=477612
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/SessionLog.java (original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/SessionLog.java Tue Nov 21 02:37:55
2006
@@ -27,13 +27,16 @@
  * Provides utility methods to log protocol-specific messages.
  * <p>
  * Set {@link #PREFIX} and {@link #LOGGER} session attributes
- * to override prefix string and logger.
+ * to override prefix string and logger or use {@link #setPrefix(IoSession, String)}
+ * and {@link #setLogger(IoSession, Logger)}.
+ * </p>
  *
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$
  *
  */
-public class SessionLog {
+public class SessionLog
+{
     /**
      * Session attribute key: prefix string
      */
@@ -51,73 +54,105 @@
 
     public static void debug( IoSession session, String message )
     {
-        Logger log = getLogger( session );
+        debug( getLogger( session ), session, message );
+    }
+
+    public static void debug( Logger log, IoSession session, String message )
+    {
         if( log.isDebugEnabled() )
         {
-            log.debug( String.valueOf( session.getAttribute( PREFIX ) ) + message );
+            log.debug( String.valueOf( getPrefix( session )  ) + message );
         }
     }
-
+    
     public static void debug( IoSession session, String message, Throwable cause )
     {
-        Logger log = getLogger( session );
+    	debug( getLogger( session ), session, message, cause );
+    }
+    
+    public static void debug( Logger log, IoSession session, String message, Throwable cause
)
+    {
         if( log.isDebugEnabled() )
         {
-            log.debug( String.valueOf( session.getAttribute( PREFIX ) ) + message, cause
);
+            log.debug( String.valueOf( getPrefix( session ) ) + message, cause );
         }
     }
 
     public static void info( IoSession session, String message )
     {
-        Logger log = getLogger( session );
+    	info( getLogger( session ), session, message );
+    }
+
+    public static void info( Logger log, IoSession session, String message )
+    {
         if( log.isInfoEnabled() )
         {
-            log.info( String.valueOf( session.getAttribute( PREFIX ) ) + message );
+            log.info( String.valueOf( getPrefix( session ) ) + message );
         }
     }
-
+    
     public static void info( IoSession session, String message, Throwable cause )
     {
-        Logger log = getLogger( session );
+    	info( getLogger( session ), session, message, cause );
+    }
+
+    public static void info( Logger log, IoSession session, String message, Throwable cause
)
+    {
         if( log.isInfoEnabled() )
         {
-            log.info( String.valueOf( session.getAttribute( PREFIX ) ) + message, cause );
+            log.info( String.valueOf( getPrefix( session ) ) + message, cause );
         }
     }
-
+    
     public static void warn( IoSession session, String message )
     {
-        Logger log = getLogger( session );
+    	warn( getLogger( session ), session, message );
+    }
+    
+    public static void warn( Logger log, IoSession session, String message )
+    {
         if( log.isWarnEnabled() )
         {
-            log.warn( String.valueOf( session.getAttribute( PREFIX ) ) + message );
+            log.warn( String.valueOf( getPrefix( session ) ) + message );
         }
     }
 
     public static void warn( IoSession session, String message, Throwable cause )
     {
-        Logger log = getLogger( session );
+    	warn( getLogger( session ), session, message, cause );
+    }
+
+    public static void warn( Logger log, IoSession session, String message, Throwable cause
)
+    {
         if( log.isWarnEnabled() )
         {
-            log.warn( String.valueOf( session.getAttribute( PREFIX ) ) + message, cause );
+            log.warn( String.valueOf( getPrefix( session ) ) + message, cause );
         }
     }
-
+    
     public static void error( IoSession session, String message )
     {
-        Logger log = getLogger( session );
+    	error( getLogger( session ), session, message );
+    }
+    
+    public static void error( Logger log, IoSession session, String message )
+    {
         if( log.isErrorEnabled() )
         {
-            log.error( String.valueOf( session.getAttribute( PREFIX ) ) + message );
+            log.error( String.valueOf( getPrefix( session ) ) + message );
         }
     }
 
     public static void error( IoSession session, String message, Throwable cause )
     {
-        Logger log = getLogger( session );
+    	error( getLogger( session ), session, message, cause );
+    }
+    
+    public static void error( Logger log, IoSession session, String message, Throwable cause
)
+    {
         if( log.isErrorEnabled() )
         {
-            log.error( String.valueOf( session.getAttribute( PREFIX ) ) + message, cause
);
+            log.error( String.valueOf( getPrefix( session ) ) + message, cause );
         }
     }
     
@@ -141,22 +176,35 @@
         return getLogger( session ).isErrorEnabled();
     }
 
-    private static Logger getLogger( IoSession session )
+    public static String getPrefix( IoSession session )
+    {
+        String prefix = ( String ) session.getAttribute( PREFIX );
+        if( prefix == null )
+        {
+            prefix = "[" + session.getRemoteAddress() + "] ";
+            setPrefix( session, prefix );
+        }
+        return prefix;
+    }
+    
+    public static void setPrefix( IoSession session, String prefix )
+    {
+    	session.setAttribute( PREFIX, prefix );
+    }
+    
+    public static Logger getLogger( IoSession session )
     {
         Logger log = ( Logger ) session.getAttribute( LOGGER );
         if( log == null )
         {
             log = LoggerFactory.getLogger( getClass( session ) );
-            String prefix = ( String ) session.getAttribute( PREFIX );
-            if( prefix == null )
-            {
-                prefix = "[" + session.getRemoteAddress() + "] ";
-                session.setAttribute( PREFIX, prefix );
-            }
-
-            session.setAttribute( LOGGER, log );
+            setLogger( session, log );
         }
-
         return log;
+    }
+    
+    public static void setLogger( IoSession session, Logger log )
+    {
+        session.setAttribute( LOGGER, log );
     }
 }



Mime
View raw message