You can specify whether JavaMail emits a protocol-level trace either when setting the Properties
for your Session
(by setting the "mail.debug"
property to "true"
) or by calling Session.setDebug
before you do the store connect.
您可以指定JavaMail是否在为会话设置属性时(通过将“mail.debug”属性设置为“true”)或在执行存储连接之前调用Session.setDebug来发出协议级跟踪。
However, when the Protocol
object gets instantiated, it creates a "protocol" TraceLogger
that persists for the lifetime of the protocol object. Which appears to mean that you can't temporarily disable protocol-level debug logging on a connection once you start using it.
但是,当Protocol对象被实例化时,它会创建一个“协议”TraceLogger,它在协议对象的生命周期内持续存在。这似乎意味着一旦开始使用它,就不能暂时禁用连接上的协议级调试日志记录。
There is a Protocol.suspendTracing
method. And it does allow you to temporarily turn off protocol trace output. A bunch of the IMAP auth methods use it to keep your credentials out of the logfile. But suspendTracing
is protected
, so it's not callable from regular user code.
有一个Protocol.suspendTracing方法。它确实允许您暂时关闭协议跟踪输出。一堆IMAP auth方法使用它来保存您的凭据不受日志文件的影响。但是suspendTracing受到保护,因此无法通过常规用户代码进行调用。
Is there another way to temporarily turn off IMAP protocol tracing? (I'd prefer temporarily turning off just the traceInput
logging, but I'm fine with disabling all logging.) Do I need to write and register a whole Protocol subclass so I can get access to Protocol.suspendTracing
?
还有另一种暂时关闭IMAP协议跟踪的方法吗? (我更喜欢暂时关闭traceInput日志记录,但我可以禁用所有日志记录。)我是否需要编写并注册一个完整的Protocol子类,以便我可以访问Protocol.suspendTracing?
2 个解决方案
#1
You could use Session.setDebugOut to set your own stream and control it from there.
您可以使用Session.setDebugOut设置自己的流并从那里控制它。
If you're using java.util.logging, you can change the logging level at any time.
如果您使用的是java.util.logging,则可以随时更改日志记录级别。
#2
Not the best solution but, you can install a custom log filter on the com.sun.mail.imap.protocol logger to check that some specific thread is allowed to produce output. Assuming your connection is local to one thread
不是最佳解决方案,但是,您可以在com.sun.mail.imap.protocol记录器上安装自定义日志过滤器,以检查是否允许某些特定线程生成输出。假设您的连接是一个线程的本地连接
public class ServiceFilter implements Filter {
private static final ThreadLocal<javax.mail.Service> id = new ThreadLocal<>();
public static void suspendTracing(javax.mail.Service s) {
id.set(Objects.requireNonNull(s));
}
public static void enableTracing(javax.mail.Service s) {
if (s.equals(id.get())) {
id.remove();
}
}
@Override
public boolean isLoggable(LogRecord record) {
return id.get() == null;
}
}
The downside is that this code becomes part of your project and it is another resource that you have to manage.
缺点是这个代码成为你项目的一部分,它是你必须管理的另一个资源。
#1
You could use Session.setDebugOut to set your own stream and control it from there.
您可以使用Session.setDebugOut设置自己的流并从那里控制它。
If you're using java.util.logging, you can change the logging level at any time.
如果您使用的是java.util.logging,则可以随时更改日志记录级别。
#2
Not the best solution but, you can install a custom log filter on the com.sun.mail.imap.protocol logger to check that some specific thread is allowed to produce output. Assuming your connection is local to one thread
不是最佳解决方案,但是,您可以在com.sun.mail.imap.protocol记录器上安装自定义日志过滤器,以检查是否允许某些特定线程生成输出。假设您的连接是一个线程的本地连接
public class ServiceFilter implements Filter {
private static final ThreadLocal<javax.mail.Service> id = new ThreadLocal<>();
public static void suspendTracing(javax.mail.Service s) {
id.set(Objects.requireNonNull(s));
}
public static void enableTracing(javax.mail.Service s) {
if (s.equals(id.get())) {
id.remove();
}
}
@Override
public boolean isLoggable(LogRecord record) {
return id.get() == null;
}
}
The downside is that this code becomes part of your project and it is another resource that you have to manage.
缺点是这个代码成为你项目的一部分,它是你必须管理的另一个资源。