By default mails are sent only for error level logs, but if you insist you can configure it to lower level logs (it is not a good idea doing this. You don’t want your application to start sending tons of emails…). The Appender used for sending mail is called: org.apache.log4j.net.SMTPAppender
Note that older versions of log4j may not have this Appender.
This is a typical properties file adding SMTPAppender:
log4j.rootLogger=INFO, a, emaillog4j.appender.a=org.apache.log4j.ConsoleAppenderlog4j.appender.a.layout=org.apache.log4j.PatternLayoutlog4j.appender.a.layout.ConversionPattern=%d{HH:mm:ss} %-5p [%c{1}]: %m%nlog4j.appender.email=org.apache.log4j.net.SMTPAppenderlog4j.appender.email.BufferSize=10log4j.appender.email.SMTPHost=mysmtp.mailserver.netlog4j.appender.email.From=admin@mycompany.comlog4j.appender.email.To=me@mycompany.comlog4j.appender.email.Subject=My Module Errorlog4j.appender.email.layout=org.apache.log4j.PatternLayoutlog4j.appender.email.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
Don’t forget that in order to send mail using java you will have to add: mail.jar and activation.jar to your class path.
The nice thing in this Appender is that you can send email alerts containing more than just your Exception. You can also add lines that were logged before the exception. This will make it much easier to understand the cause of your exception. The number of log lines that will be sent can be determine by “BufferSize” property. For example: if BufferSize=10, then your email will also contain the 9 lines logged before the exception.
If your SMTP mail server is not on the same network of your server, you would probably won’t be able to send emails without authentication. You can easily overcome this problem by simply adding username and password properties:
log4j.appender.email.SMTPUsername=myusername@mycompany.comlog4j.appender.email.SMTPPassword=mypassword
Note that also here, older versions of log4j may not have support for “username” and “password” properties.
That’s it. you can now check you mail box. You should be able to get email alerts every time an exception is logged on your application. You can also make a fake test to your configuration. This will make it easier to see how your mail will look:
log.info("some fake info");
try
{throw new Exception("some fake exception");}catch (Exception e)
{log.error("Fake exception occurred", e);
}
I found only one disturbing this with this Appender: It doesn’t send the mail on a different Thread. This causes the the current thread to be stuck for a second when sending mail. I don’t find it as a big problem, since usually Exceptions don’t happen much often (at least on production environments where this Appender will mostly be used). But once such exception happens and it is being repeated in a loop, the system may hang and unpleasant things might happen. On the other hand, sending the mail on a different Thread won’t cause the current thread to be stuck, but may flood the system with big amount of mails.
No comments:
Post a Comment