Yaroslav Boichuk
Yaroslav Boichuk

Reputation: 1776

Log events from com.sun.mail which use PrintStream with log4j into separate file

com.sun.* package use PrintStream for logging. For example class com.sun.mail.smtp.SMTPTransport

private PrintStream out;
...
if (debug)
        out.println("DEBUG SMTP: useEhlo " + useEhlo + ", useAuth " + useAuth);

is this possible to log these events with log4j? One solution, is to create proxy for System output, like here is suggested: log4j redirect stdout to DailyRollingFileAppender, but problem is, there would be logged all events which use PrintStream, not only com.sun.mail.* package, and I can't only log from com.sun.* package.

For example I want to log email events into separate file, but can't, because of probability of some other class from some 3rd party library using PrintStream.

Upvotes: 0

Views: 787

Answers (2)

jmehrens
jmehrens

Reputation: 11045

JavaMail 1.4.7 or newer uses the JDK logging along with the legacy support for the printstream. The Log4j JUL Adapter can be used to capture output from JavaMail. The JavaMail API documents the names of the JDK loggers at the bottom of each JavaMail package.

Upvotes: 2

vels4j
vels4j

Reputation: 11298

Try this

log4j.appender.MAIL=org.apache.log4j.RollingFileAppender
log4j.appender.MAIL.File=/path/to/MAIL.log
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout

log4j.logger.com.sun.mail=LEVEL, MAIL

Append new MAIL appeender to root logger

log4j.rootLogger= ... , MAIL

Upvotes: 1

Related Questions