0

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.

Community
  • 1
  • 1
Yaroslav Boichuk
  • 1,763
  • 20
  • 31

2 Answers2

2

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.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
1

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
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • I've tried like this, and this doens't work, as I understood, because of SMTPTransport class not using logger, but using PrintStream to log their events – Yaroslav Boichuk Aug 26 '15 at 12:15