Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active May 14, 2022 07:18
Show Gist options
  • Select an option

  • Save akunzai/89843669fa939cb5c194e2eef25a9a35 to your computer and use it in GitHub Desktop.

Select an option

Save akunzai/89843669fa939cb5c194e2eef25a9a35 to your computer and use it in GitHub Desktop.
My log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--
https://logging.apache.org/log4j/2.x/manual/async.html#AllAsync
Don't forget to set system property
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
to make all loggers asynchronous.
Levels: OFF,FATAL,ERROR,WARN,INFO,DEBUG,TRACE,ALL
-->
<Configuration monitorInterval="30" status="WARN">
<Properties>
<Property name="LOG_PATH">${env:LOG_PATH:-${sys:catalina.base:-/tmp}/logs}</Property>
</Properties>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%date|%highlight{%-5level}|%logger|%msg%n"/>
</Console>
<!-- https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender -->
<RollingFile name="File"
fileName="${sys:LOG_PATH}/app.log"
filePattern="${sys:LOG_PATH}/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}-%i.log.gz"
createOnDemand="true" immediateFlush="false">
<PatternLayout pattern="%date|%-5level|%logger|%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:LOG_PATH}" maxDepth="2">
<IfFileName glob="*/app-*.log*" />
<IfLastModified age="P90D" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<!-- https://logging.apache.org/log4j/2.x/manual/appenders.html#FailoverAppender -->
<Failover name="Mail" primary="SMTP">
<Failovers>
<AppenderRef ref="SendGrid"/>
</Failovers>
</Failover>
<!-- https://logging.apache.org/log4j/2.x/manual/appenders.html#SMTPAppender -->
<SMTP name="Mail"
bufferSize="3"
subject="Error Notification from ${env:LOG_HOSTNAME:-${sys:hostName}}"
from="${env:LOG_MAIL_FROM}" to="${env:LOG_MAIL_TO}"
smtpHost="${env:SMTP_HOST}" smtpPort="${env:SMTP_PORT:-25}">
<PatternLayout pattern="${%date|%level|%logger|%msg%n%rEx{5}%n}" />
<Filters>
<ThresholdFilter level="ERROR" />
<!-- https://logging.apache.org/log4j/2.x/manual/filters.html#BurstFilter
limit the burstInterval to 1 hour
rate = maxBurst/burstInterval = 1/3600
-->
<BurstFilter level="ERROR" rate="0.0002" maxBurst="1"/>
</Filters>
</SMTP>
<!-- https://github.com/akunzai/log4j2-sendgrid-appender -->
<SendGrid name="SendGrid"
bufferSize="3"
subject="Error Notification from ${env:LOG_HOSTNAME:-${sys:hostName}}"
from="${env:LOG_MAIL_FROM}" to="${env:LOG_MAIL_TO}"
apiKey="${env:SENDGRID_API_KEY}">
<PatternLayout pattern="${sys:MAIL_LOG_PATTERN}" />
<Filters>
<ThresholdFilter level="ERROR" />
<!-- https://logging.apache.org/log4j/2.x/manual/filters.html#BurstFilter
limit the burstInterval to 1 hour
rate = maxBurst/burstInterval = 1/3600
-->
<BurstFilter level="ERROR" rate="0.0002" maxBurst="1"/>
</Filters>
</SendGrid>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="ERROR" includeLocation="true" />
<Root level="INFO" includeLocation="true">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
<AppenderRef ref="Mail"/>
</Root>
</Loggers>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment