Gmail Logging with log4j 1.2.16

Log4j includes an SMTP appender that can be used to send logging messages via email. As of log4j 1.2.16, this built-in appender has the ability to send messages via Gmail.

Here is an example log4j configuration that uses Gmail to send logged messages:

log4j.rootLogger=DEBUG, gmail log4j.appender.gmail=org.apache.log4j.net.SMTPAppender log4j.appender.gmail.SMTPProtocol=smtps log4j.appender.gmail.SMTPUsername=user@gmail.com log4j.appender.gmail.SMTPPassword=passw0rd log4j.appender.gmail.SMTPHost=smtp.gmail.com log4j.appender.gmail.SMTPPort=465 log4j.appender.gmail.Subject=Logging Message via Gmail log4j.appender.gmail.To=recipient@somewhere.com log4j.appender.gmail.From=user@gmail.com log4j.appender.gmail.layout=org.apache.log4j.PatternLayout log4j.appender.gmail.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss} [%M] %-5p %C - %m%n log4j.appender.gmail.BufferSize=5

The SMTPUsername, SMTPPassword, Subject, To, and From properties should be replaced with values appropriate for your situation. One thing that tripped me up originally is that the SMTPPort should be set to 465, not 587 as I had originally done.

This article is part of the GWB Archives. Original Author: Sean Carpenter

New on Geeks with Blogs