You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2024/03/29 03:05:18 UTC

(james-project) 04/04: JAMES-3977 IMAP log should embed username where possible

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 8155100e36e81668d803693daff8693177376299
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Thu Mar 28 09:41:57 2024 +0100

    JAMES-3977 IMAP log should embed username where possible
    
    Easier filtering during log analysis
---
 .../james/imap/processor/fetch/FetchProcessor.java      |  8 ++++++--
 .../imapserver/netty/ImapChannelUpstreamHandler.java    | 17 ++++++++++++++---
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java
index 6619064152..90291cae84 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/fetch/FetchProcessor.java
@@ -99,14 +99,18 @@ public class FetchProcessor extends AbstractMailboxProcessor<FetchRequest> {
             AtomicBoolean mustRequestOne = new AtomicBoolean(true);
             responder.respond(fetchResponse);
             Runnable requestOne = () -> {
+                LOGGER.info("Resuming IMAP FETCH for user {}", imapSession.getUserName().asString());
                 if (mustRequestOne.getAndSet(false)) {
                     requestOne();
                 }
             };
             if (imapSession.backpressureNeeded(requestOne)) {
-                LOGGER.debug("Applying backpressure as we encounter a slow reader");
+                LOGGER.info("Applying backpressure as user {} is a slow reader",
+                    imapSession.getUserName().asString());
             } else {
-                requestOne.run();
+                if (mustRequestOne.getAndSet(false)) {
+                    requestOne();
+                }
             }
         }
 
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
index 094d0284e3..d6110a4f1f 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapChannelUpstreamHandler.java
@@ -34,6 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.net.ssl.SSLHandshakeException;
 
+import org.apache.james.core.Username;
 import org.apache.james.imap.api.ConnectionCheck;
 import org.apache.james.imap.api.ImapConstants;
 import org.apache.james.imap.api.ImapMessage;
@@ -269,7 +270,7 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
         ImapSession imapSession = ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).getAndSet(null);
         try (Closeable closeable = mdc(imapSession).build()) {
             InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
-            LOGGER.info("Connection closed for {}", address.getAddress().getHostAddress());
+            LOGGER.info("Connection closed for {} and user {}", address.getAddress().getHostAddress(), retrieveUsername(imapSession));
 
             Optional.ofNullable(imapSession).ifPresent(ImapSession::cancelOngoingProcessing);
             Optional.ofNullable(imapSession)
@@ -285,11 +286,20 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
         }
     }
 
+    private static String retrieveUsername(ImapSession imapSession) {
+        return Optional.ofNullable(imapSession)
+            .flatMap(session -> Optional.ofNullable(session.getUserName()))
+            .map(Username::asString)
+            .orElse("");
+    }
+
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+        ImapSession imapSession = ctx.channel().attr(IMAP_SESSION_ATTRIBUTE_KEY).getAndSet(null);
+        String username = retrieveUsername(imapSession);
         try (Closeable closeable = mdc(ctx).build()) {
             if (cause instanceof SocketException) {
-                LOGGER.info("Socket exception encountered: {}", cause.getMessage());
+                LOGGER.info("Socket exception encountered for user {}: {}", username, cause.getMessage());
             } else if (isSslHandshkeException(cause)) {
                 LOGGER.info("SSH handshake rejected {}", cause.getMessage());
             } else if (isNotSslRecordException(cause)) {
@@ -370,7 +380,8 @@ public class ImapChannelUpstreamHandler extends ChannelInboundHandlerAdapter imp
             .subscribe(any -> {
 
             }, e -> {
-                LOGGER.error("Exception while handling errors for channel {}", ctx.channel(), e);
+                LOGGER.error("Exception while handling errors for channel {} and user {}", ctx.channel(),
+                    Optional.ofNullable(imapSession).map(u -> u.getUserName().asString()).orElse(""), e);
                 Channel channel = ctx.channel();
                 if (channel.isActive()) {
                     channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org