Author: norman
Date: Thu Dec  9 16:35:07 2010
New Revision: 1044025

URL: http://svn.apache.org/viewvc?rev=1044025&view=rev
Log:
Improve logging for SMTPServer. Now it really easy to see what handlers 
response to the client and look at it in one transaction. See JAMES-1144

Added:
    james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/CommandHandlerResultLogger.java
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/ConnectHandlerResultLogger.java
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/HookResultLogger.java
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/LineHandlerResultLogger.java
Modified:
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CoreCmdHandlerLoader.java
    
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/netty/SMTPNettySession.java

Modified: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CoreCmdHandlerLoader.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CoreCmdHandlerLoader.java?rev=1044025&r1=1044024&r2=1044025&view=diff
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CoreCmdHandlerLoader.java
 (original)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/CoreCmdHandlerLoader.java
 Thu Dec  9 16:35:07 2010
@@ -24,6 +24,7 @@ package org.apache.james.smtpserver;
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.james.protocols.api.CommandHandlerResultHandler;
 import org.apache.james.protocols.api.HandlersPackage;
 import org.apache.james.protocols.smtp.core.ExpnCmdHandler;
 import org.apache.james.protocols.smtp.core.HeloCmdHandler;
@@ -39,6 +40,10 @@ import org.apache.james.protocols.smtp.c
 import org.apache.james.protocols.smtp.core.esmtp.EhloCmdHandler;
 import org.apache.james.protocols.smtp.core.esmtp.MailSizeEsmtpExtension;
 import org.apache.james.protocols.smtp.core.esmtp.StartTlsCmdHandler;
+import org.apache.james.smtpserver.log.CommandHandlerResultLogger;
+import org.apache.james.smtpserver.log.ConnectHandlerResultLogger;
+import org.apache.james.smtpserver.log.HookResultLogger;
+import org.apache.james.smtpserver.log.LineHandlerResultLogger;
 
 /**
  * This class represent the base command handlers which are shipped with james.
@@ -71,6 +76,12 @@ public class CoreCmdHandlerLoader implem
     // MessageHooks
     private final String ADDDEFAULTATTRIBUTESHANDLER = 
AddDefaultAttributesMessageHook.class.getName();
     private final String SENDMAILHANDLER = SendMailHandler.class.getName();
+    
+    // logging stuff
+    private final String COMMANDHANDLERRESULTLOGGER = 
CommandHandlerResultLogger.class.getName();
+    private final String CONNECTHANDLERRESULTLOGGER = 
ConnectHandlerResultLogger.class.getName();
+    private final String LINEHANDLERRESULTLOGGER = 
LineHandlerResultLogger.class.getName();
+    private final String HOOKRESULTLOGGER = HookResultLogger.class.getName();
 
     private final List<String> commands = new LinkedList<String>();
 
@@ -101,6 +112,12 @@ public class CoreCmdHandlerLoader implem
         // Add the default messageHooks
         commands.add(ADDDEFAULTATTRIBUTESHANDLER);
         commands.add(SENDMAILHANDLER);
+        
+        // Add logging stuff
+        commands.add(COMMANDHANDLERRESULTLOGGER);
+        commands.add(CONNECTHANDLERRESULTLOGGER);
+        commands.add(LINEHANDLERRESULTLOGGER);
+        commands.add(HOOKRESULTLOGGER);
     }
     /*
      * (non-Javadoc)

Added: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/CommandHandlerResultLogger.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/CommandHandlerResultLogger.java?rev=1044025&view=auto
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/CommandHandlerResultLogger.java
 (added)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/CommandHandlerResultLogger.java
 Thu Dec  9 16:35:07 2010
@@ -0,0 +1,54 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.smtpserver.log;
+
+import org.apache.james.protocols.api.CommandHandler;
+import org.apache.james.protocols.api.CommandHandlerResultHandler;
+import org.apache.james.protocols.api.ProtocolSession;
+import org.apache.james.protocols.api.Response;
+import org.apache.james.protocols.smtp.SMTPResponse;
+import org.apache.james.protocols.smtp.SMTPSession;
+
+/**
+ * Log every 5xx and 4xx response in INFO level. The rest is logged via DEBUG 
level
+ * 
+ * TODO: This should go to protocols
+ * 
+ *
+ */
+public class CommandHandlerResultLogger implements 
CommandHandlerResultHandler<SMTPResponse, SMTPSession> {
+
+    /*
+     * (non-Javadoc)
+     * @see 
org.apache.james.protocols.api.CommandHandlerResultHandler#onResponse(org.apache.james.protocols.api.ProtocolSession,
 org.apache.james.protocols.api.Response, long, 
org.apache.james.protocols.api.CommandHandler)
+     */
+    public Response onResponse(ProtocolSession session, SMTPResponse response, 
long executionTime, CommandHandler<SMTPSession> handler) {
+        String code = response.getRetCode();
+        String msg = handler.getClass().getName() + ": " + response.toString();
+        
+        // check if the response was a perm error or a temp error
+        if (code.startsWith("5") || code.startsWith("4")) {
+            session.getLogger().info(msg);
+        } else {
+            session.getLogger().debug(msg);
+        }
+        return response;
+    }
+
+}

Added: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/ConnectHandlerResultLogger.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/ConnectHandlerResultLogger.java?rev=1044025&view=auto
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/ConnectHandlerResultLogger.java
 (added)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/ConnectHandlerResultLogger.java
 Thu Dec  9 16:35:07 2010
@@ -0,0 +1,48 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.smtpserver.log;
+
+import org.apache.james.protocols.api.ConnectHandler;
+import org.apache.james.protocols.api.ConnectHandlerResultHandler;
+import org.apache.james.protocols.api.ProtocolSession;
+import org.apache.james.protocols.smtp.SMTPSession;
+
+/**
+ * Log disconnects caused by {...@link ConnectHandler} vi INFO. The rest is 
logged via DEBUG
+ *
+ * TODO: This should go to protocols
+
+ */
+public class ConnectHandlerResultLogger implements 
ConnectHandlerResultHandler<SMTPSession>{
+
+    /*
+     * (non-Javadoc)
+     * @see 
org.apache.james.protocols.api.ConnectHandlerResultHandler#onResponse(org.apache.james.protocols.api.ProtocolSession,
 boolean, long, org.apache.james.protocols.api.ConnectHandler)
+     */
+    public boolean onResponse(ProtocolSession session, boolean response, long 
executionTime, ConnectHandler<SMTPSession> handler) {
+
+        if (response) {
+            session.getLogger().info(handler.getClass().getName() + " 
disconnect=true");
+        } else {
+            session.getLogger().debug(handler.getClass().getName() + " 
disconnect=false");
+        }
+        return response;
+    }
+
+}

Added: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/HookResultLogger.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/HookResultLogger.java?rev=1044025&view=auto
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/HookResultLogger.java
 (added)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/HookResultLogger.java
 Thu Dec  9 16:35:07 2010
@@ -0,0 +1,80 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.smtpserver.log;
+
+import org.apache.james.protocols.smtp.SMTPSession;
+import org.apache.james.protocols.smtp.hook.Hook;
+import org.apache.james.protocols.smtp.hook.HookResult;
+import org.apache.james.protocols.smtp.hook.HookResultHook;
+import org.apache.james.protocols.smtp.hook.HookReturnCode;
+
+/**
+ * 
+ * Log the {...@link HookResult}. If {...@link HookReturnCode#DENY}, {...@link 
HookReturnCode#DENYSOFT} or {...@link HookReturnCode#DISCONNECT} was used it 
will get 
+ * logged to INFO. If not to DEBUG
+ *
+ * TODO: This should go to protocols  
+ */
+public class HookResultLogger implements HookResultHook{
+
+    public HookResult onHookResult(SMTPSession session, HookResult hResult, 
long executionTime, Hook hook) {
+        boolean match = false;
+        boolean info = false;
+        int result = hResult.getResult();
+        StringBuilder sb = new StringBuilder();
+        sb.append(hook.getClass().getName());
+        sb.append(": result=");
+        sb.append(result);
+        sb.append(" (");
+        if ((result & HookReturnCode.DECLINED) == HookReturnCode.DECLINED) {
+            sb.append("DECLINED");
+            match = true;
+        }
+        if ((result & HookReturnCode.OK) == HookReturnCode.OK) {
+            sb.append("OK");
+            match = true;
+        }
+        if ((result & HookReturnCode.DENY) == HookReturnCode.DENY) {
+            sb.append("DENY");
+            match = true;
+            info = true;
+        }
+        if ((result & HookReturnCode.DENYSOFT) == HookReturnCode.DENYSOFT) {
+            sb.append("DENYSOFT");
+            match = true;
+            info = true;
+        }
+        if ((result & HookReturnCode.DISCONNECT) == HookReturnCode.DISCONNECT) 
{
+            if(match) {
+                sb.append("|");
+            }
+            sb.append("DISCONNECT");
+            info = true;
+        }
+        sb.append(")");
+
+        if (info) {
+            session.getLogger().info(sb.toString());
+        } else {
+            session.getLogger().debug(sb.toString());
+        }
+        return hResult;
+    }
+
+}

Added: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/LineHandlerResultLogger.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/LineHandlerResultLogger.java?rev=1044025&view=auto
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/LineHandlerResultLogger.java
 (added)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/log/LineHandlerResultLogger.java
 Thu Dec  9 16:35:07 2010
@@ -0,0 +1,48 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.james.smtpserver.log;
+
+import org.apache.james.protocols.api.LineHandler;
+import org.apache.james.protocols.api.LineHandlerResultHandler;
+import org.apache.james.protocols.api.ProtocolSession;
+import org.apache.james.protocols.smtp.SMTPSession;
+
+/**
+ * Log disconnects caused by {...@link LineHandler} via INFO. The rest via 
DEBUG
+ *
+  * TODO: This should go to protocols
+
+ */
+public class LineHandlerResultLogger implements 
LineHandlerResultHandler<SMTPSession>{
+
+    /*
+     * (non-Javadoc)
+     * @see 
org.apache.james.protocols.api.LineHandlerResultHandler#onResponse(org.apache.james.protocols.api.ProtocolSession,
 boolean, long, org.apache.james.protocols.api.LineHandler)
+     */
+    public boolean onResponse(ProtocolSession session, boolean response, long 
executionTime, LineHandler<SMTPSession> handler) {
+
+        if (response) {
+            session.getLogger().info(handler.getClass().getName() + " 
disconnect=true");
+        } else {
+            session.getLogger().debug(handler.getClass().getName() + " 
disconnect=false");
+        }
+        return response;
+    }
+
+}

Modified: 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/netty/SMTPNettySession.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/netty/SMTPNettySession.java?rev=1044025&r1=1044024&r2=1044025&view=diff
==============================================================================
--- 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/netty/SMTPNettySession.java
 (original)
+++ 
james/server/trunk/smtpserver/src/main/java/org/apache/james/smtpserver/netty/SMTPNettySession.java
 Thu Dec  9 16:35:07 2010
@@ -52,7 +52,8 @@ public class SMTPNettySession extends Ab
     private SMTPConfiguration theConfigData;
 
     private int lineHandlerCount = 0;
-
+    private Log log = null;
+    
     public SMTPNettySession(SMTPConfiguration theConfigData, Log logger, 
ChannelHandlerContext handlerContext, SSLEngine engine) {
         super(logger, handlerContext, engine);
         this.theConfigData = theConfigData;
@@ -62,6 +63,14 @@ public class SMTPNettySession extends Ab
         relayingAllowed = 
theConfigData.isRelayingAllowed(getRemoteIPAddress());    
     }
    
+    @Override
+    public Log getLogger() {
+        if (log == null) {
+            log = new SMTPSessionLog(super.getLogger());
+        }
+        return log;
+    }
+
     public SMTPNettySession(SMTPConfiguration theConfigData, Log logger, 
ChannelHandlerContext handlerContext) {
         this(theConfigData, logger, handlerContext, null);  
     }
@@ -233,4 +242,103 @@ public class SMTPNettySession extends Ab
         }
     }
 
+    
+    /**
+     * A {...@link Log} implementation which suffix every log message with the 
session Id
+     * 
+     *
+     */
+    private final class SMTPSessionLog implements Log {
+        private Log logger;
+
+        public SMTPSessionLog(Log logger) {
+            this.logger = logger;
+        }
+
+        private String getText(Object obj) {
+            return getSessionID()+ " " + obj.toString();
+        }
+        public void debug(Object arg0) {
+            logger.debug(getText(arg0));
+        }
+
+        public void debug(Object arg0, Throwable arg1) {
+            logger.debug(getText(arg0), arg1);
+            
+        }
+
+        public void error(Object arg0) {
+            logger.error(getText(arg0));
+            
+        }
+
+        public void error(Object arg0, Throwable arg1) {
+            logger.error(getText(arg0), arg1);
+            
+        }
+
+        public void fatal(Object arg0) {
+            logger.fatal(getText(arg0));
+            
+        }
+
+        public void fatal(Object arg0, Throwable arg1) {
+            logger.fatal(getText(arg0), arg1);
+            
+        }
+
+        public void info(Object arg0) {
+            logger.info(getText(arg0));
+            
+        }
+
+        public void info(Object arg0, Throwable arg1) {
+            logger.info(getText(arg0), arg1);
+            
+        }
+
+        public boolean isDebugEnabled() {
+            return logger.isDebugEnabled();
+        }
+
+        public boolean isErrorEnabled() {
+            return logger.isErrorEnabled();
+        }
+
+        public boolean isFatalEnabled() {
+            return logger.isFatalEnabled();
+        }
+
+        public boolean isInfoEnabled() {
+            return logger.isInfoEnabled();
+        }
+
+        public boolean isTraceEnabled() {
+            return logger.isTraceEnabled();
+        }
+
+        public boolean isWarnEnabled() {
+            return logger.isWarnEnabled();
+        }
+
+        public void trace(Object arg0) {
+            logger.trace(getText(arg0));
+        }
+
+        public void trace(Object arg0, Throwable arg1) {
+            logger.trace(getText(arg0), arg1);
+            
+        }
+
+        public void warn(Object arg0) {
+            logger.warn(getText(arg0));
+            
+        }
+
+        public void warn(Object arg0, Throwable arg1) {
+            logger.warn(getText(arg0), arg1);
+            
+        }
+        
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to