Reputation: 10882
How can I setDebug(true)
on a JavaMail session but capture the stream and use it in my logging framework? (Short of downloading the source, changing the method to accept a stream as a parameter, recompiling it, ...)
More generally, is there a standard way in Java to "hijack-and-redirect" generic stream output in this fashion? Note that System.out may be already polluted with other output and I have no idea how to "filter" that...
Upvotes: 9
Views: 10446
Reputation: 35387
You can link a PrintStream to a ByteArrayOutputStream, tell to JavaMail to use your PrintStream, do the JavaMail stuff and finally, dump the the content of the ByteArrayOutputStream to your favorite logger.
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
Session mailSession = Session.getDefaultInstance(props, null);
try {
if (MAIL_DEBUG) {
logger.info("JAVAMAIL debug mode is ON");
mailSession.setDebugOut(ps);
mailSession.setDebug(true);
}
...
transport.close();
if (MAIL_DEBUG) {
logger.info(os);
}
}
finally {
ps.close();
os.close();
}
Upvotes: 11