How to integrate application logging errors with JEMH
- 1.1 Overview
- 1.2 LOG4J
- 1.2.1 Configuration
- 1.2.1.1 Root Logger config
- 1.2.1.2 SSL
- 1.2.1.3 JEMH Email Appender
- 1.2.1.4 Non-SSL
- 1.2.2 Java
- 1.2.2.1 LOG4J Example
- 1.2.1 Configuration
- 1.3 SLF4J
- 1.3.1 Configuration
- 1.3.1.1 SLF4J Example
- 1.3.1 Configuration
- 1.4 Examples
- 2 Related articles
Overview
Log to Email is a pair of log appenders that allow LOG4J or SLF4J logging frameworks to send error or fatal messages to JEMH via an email message. JEMH can then be configured to create a JIRA issue based on that error or fatal message.
The appenders have the following features:
Only messages of the type error and fatal are used, the other messages are ignored
An email is sent to JEMH for every logged message
Emails are queued up and sent in their own threads
As a safety measure if 2 emails are currently in the queue then all new messages will be dumped (until the queue falls below 2 items). This will reduce the number of emails being sent in case a large number of errors occur
LOG4J
To use the LOG4J features you will need to include the following Maven dependences: log4j.log4j and javax.mail.mail
Configuration
To configure LOG4J you will need to specify the correct appender and the email details in the log4j.properties file. Below are two examples, one that uses a SSL connection and the other uses a non-SSL connection. Both examples require you to define the appender shown below.
Root Logger config
log4j.rootLogger=DEBUG, A1,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
The above sample defines an appender called A1 that reports on error and fatal messages. Anything below the level of ERROR will be ignored. The appender for A1 is "com.thepluginpeople.jemhloggingsupport.log4j.Log4JEmailAppender".
SSL
JEMH Email Appender
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1.emailSubject=Application X had a problem
log4j.appender.A1.applicationName=My Test Application
log4j.appender.A1.ssl=true
log4j.appender.A1.auth=true
log4j.appender.A1.socketClass=javax.net.ssl.SSLSocketFactory
log4j.appender.A1.socketPort=465
log4j.appender.A1.port=587
log4j.appender.A1.host=smtp.gmail.com
log4j.appender.A1.username=myGMailUser
log4j.appender.A1.password=myPassword
log4j.appender.A1.fromAddress=myGMailUser@gmail.com
log4j.appender.A1.toAddress=notifyUser@gmail.com
Non-SSL
log4j.appender.A1.Threshold=ERROR
log4j.appender.A1.emailSubject=Application X had a problem
log4j.appender.A1.applicationName=My Test Application
log4j.appender.A1.ssl=false
log4j.appender.A1.auth=true
log4j.appender.A1.tls=true
log4j.appender.A1.port=587
log4j.appender.A1.host=smtp.gmail.com
log4j.appender.A1.username=myGMailUser
log4j.appender.A1.password=myPassword
log4j.appender.A1.fromAddress=myGMailUser@gmail.com
log4j.appender.A1.toAddress=notifyUser@gmail.com
Java
Once you have configured LOG4J any use of a LOG4J logger for a message type of error or fatal will be emailed to JEMH. Below is a simple example of logging an error:
LOG4J Example
SLF4J
SLF4J uses the LOG4J framework for processing logging messages. To use the SLF4J features you will need to include the following Maven dependences: org.slf4j.slf4j-api, org.slf4j.slf4j-log4j12 and the dependencies from the LOG4J section.
Configuration
To Configure SLF4J you will need to use the configuration outlined in the LOG4J Configuration section.
Java
Once you have configured SLF4J any logging of an error message will be emailed to JEMH. Below is an example of an error message being logged.
SLF4J Example
Examples
The examples used on this page can be found in the zip file attached.