Send log4j2 errors via SendGrid service
- Java 11 runtime
- a SendGrid account with your API key
dependencies {
implementation 'com.github.akunzai:log4j2-sendgrid-appender:3.1.0'
}
<dependency>
<groupId>com.github.akunzai</groupId>
<artifactId>log4j2-sendgrid-appender</artifactId>
<version>3.1.0</version>
</dependency>
The following is the minimum configuration needed for log4j2.xml
to send an error email
By default, logger events will be buffering with previous 512 messages and filtered by ThresholdFilter, formatted as HTML.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="WARN">
<Appenders>
<SendGrid name="SendGrid"
subject="Error Notification from ${sys:hostName}"
from="${env:LOG_MAIL_FROM}"
to="${env:LOG_MAIL_TO}"
apiKey="${env:SENDGRID_API_KEY}">
</SendGrid>
</Appenders>
<Loggers>
<Root Level="WARN">
<AppenderRef ref="SendGrid"/>
</Root>
</Loggers>
</Configuration>
Sending error message with throttling
The following configuration was throttled to send 1 error email in 1 hour by combine the ThresholdFilter and BurstFilter
rate = maxBurst/burstInterval = 1/3600 ~= 0.0002
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="WARN">
<Appenders>
<SendGrid name="SendGrid"
bufferSize="3"
subject="Error Notification from ${sys:hostName}"
from="${sys:LOG_MAIL_FROM:-${env:LOG_MAIL_FROM}}"
to="${sys:LOG_MAIL_TO:-${env:LOG_MAIL_TO}}"
apiKey="${env:SENDGRID_API_KEY}">
<PatternLayout pattern="%date|%level|%logger|%msg%n%rEx{5}%n"/>
<Filters>
<ThresholdFilter level="ERROR"/>
<BurstFilter level="ERROR" rate="0.0002" maxBurst="1"/>
</Filters>
</SendGrid>
</Appenders>
<Loggers>
<Root Level="WARN">
<AppenderRef ref="SendGrid"/>
</Root>
</Loggers>
</Configuration>