Last days I’ve been playing with Play! Framework, which really is a breeze to work with when it comes to rapid web development for Java. If you haven’t checked it out yet, you really should over at http://www.playframework.org/ .
Anyway, lets bump into how to fix log4j to send error logs by email to my “maintainer” email address by a SMTPS (SMTP SSL) server. In this example I’ll show you how to configure it using the log4j.properties file.
My instructions are based on http://www.playframework.org/code with a few adjustments to get the correct version of log4j.
First of all, we’ll check out the latest version of Play! framework by using git:
git clone git://github.com/playframework/play.git
or git clone git://github.com/playframework/play.git –depth 10 (as the project’s history is pretty big >115MB at least)
Iv’e checked out my Play!’s installation over at /opt/play .
Delete log4j-1.2.15.jar from /opt/play/framework/lib/
Download log4j 1.2.16 or newer from http://logging.apache.org/log4j/1.2/download.html
This is required since log4j version 1.2.16 supports setting SMTPTransport to SMTPS by using a properties file/config file. Earlier versions requires some nasty extending of the SMTPAppender class and do stuff you shouldn’t be required to do! Finally this is fixed in 1.2.16
Place log4j-1.2.16.jar into /opt/play/framework/lib/
cd /opt/play/framework
ant
Now it will build Play! Framework while using the latest log4j library.
For projects already setup, make sure you do:
play dependencies
(if your using play maven module, you’ll also make sure you reimport the libs defined in your pom.xml by:)
( play mvn:re )
That should have updated all your project dependencies. The only last important part is to configure up log4j to send email. I’ve used Google SMTPS server as an example here:
You place the log4j.properties file in $PlayApp/conf/
log4j.rootLogger=INFO, Console, gmail
log4j.logger.play=INFO
log4j.logger.org.quartz=WARN
log4j.logger.org.hibernate=WARN
log4j.logger.DataNucleus=WARN
log4j.logger.org.apache.commons.httpclient=ERROR
log4j.logger.net.sf.oval.internal=ERROR
log4j.logger.org.springframework=WARN# Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%nlog4j.appender.gmail=org.apache.log4j.net.SMTPAppender
log4j.appender.gmail.SMTPProtocol=smtps
log4j.appender.gmail.SMTPHost=smtp.gmail.com
log4j.appender.gmail.SMTPPort=465
log4j.appender.gmail.SMTPUsername=your@gmail.com
log4j.appender.gmail.SMTPPassword=XXX
log4j.appender.gmail.From=your@gmail.com
log4j.appender.gmail.To=whatever@example.com
log4j.appender.gmail.Subject=Application Error – ${application.path}
log4j.appender.gmail.BufferSize=1
log4j.appender.gmail.layout=org.apache.log4j.PatternLayout
log4j.appender.gmail.layout.ConversionPattern=%m
log4j.appender.gmail.threshold=ERROR# Rolling files
# log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
# log4j.appender.Rolling.File=${application.path}/logs/application.log
# log4j.appender.Rolling.MaxFileSize=1MB
# log4j.appender.Rolling.MaxBackupIndex=100
# log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
# log4j.appender.Rolling.layout.ConversionPattern=%d{DATE} %-5p ~ %m%n
Thats it! Hopefully it shouldn’t be required to get the right log4j library yourself in the future, I’ll submit a ticket to the developers at Play! to ask them to bump to the latest version of log4j, so people don’t need to do this hassle
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.