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

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.