如何将javax.mail.Session setDebugOut重定向到log4j logger?

web*_*bgt 10 java email debugging log4j jakarta-mail

如何将javax.mail.Session setDebugOut重定向到log4j logger?

是否可以仅将mailSession调试重定向到记录器?

我的意思是,有类似的解决方案

链接文字

它将所有标准输出重新分配到log4j

--System.setOut(new Log4jStream())

最好的祝福

Luk*_*mpa 11

Apache Commons Exec库包含有用的类LogOutputStream,您可以将其用于此目的:

LogOutputStream losStdOut = new LogOutputStream() {             
    @Override
    protected void processLine(String line, int level) {
        cat.debug(line);
    }
};

Session session = Session.getDefaultInstance(new Properties(), null);
session.setDebugOut(new PrintStream(losStdOut));
Run Code Online (Sandbox Code Playgroud)

猫显然是log4j类别/ Appender.


web*_*bgt 2

编写自己的 OutputStream 类

mailSession.setDebugOut(new PrintStream(您的自定义输出流对象));