Reputation: 115
I just want to send logs with ERROR level via email in my spring boot application using log4j2.
After reading infinite tutorials about this, im unable to reach this.
I have a Docker SMTP server in localhost, and here are my configuration files:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Add Log4j2 Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Needed for SMTP appender -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
...
...
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C}{bright,yellow}: %msg%n%throwable" />
</Console>
<SMTP name="MailAppender"
subject="error log"
to="[email protected]"
from="[email protected]"
smtpHost="localhost"
smtpPort="25"
bufferSize="10"
ignoreExceptions="false"
smtpDebug="true"
>
<ThresholdFilter level="ERROR" onMatch="ACCEPT"/>
<PatternLayout>
<pattern>%d %p [%C] [%t] %m%n</pattern>
</PatternLayout>
</SMTP>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="MailAppender" level="debug" />
</Root>
</Loggers>
</Configuration>
application.yml
spring:
mail:
host: localhost
port: 25
auth: false
properties:
mail:
transport:
protocol: smtp
smtp:
auth: false
host: localhost
port: 25
With this configuration, log4j2 isnt able to send any mail. Aparently, log4j2 takes configuration correctly on startup:
2020-09-24 18:31:32,780 restartedMain DEBUG createAppender(Configuration(/XXX/target/classes/log4j2.xml), name="MailAppender", to="[email protected]", cc="null", bcc="null", from="[email protected]", replyTo="null", subject="error log", smtpProtocol="null", smtpHost="localhost", smtpPort="25", smtpUsername="null", smtpPassword="c0323768f8f5dc63ac2d877d8e65de66", smtpDebug="true", bufferSize="10", PatternLayout(%d %p [%C] [%t] %m%n), ThresholdFilter(ERROR), ignoreExceptions="false")
but then it cannot send any email.
The funniest thing is that I can use spring mail to send mails (using JavaMailSender API) with same configuration, so I think that there is a problem with log4j2 configuration. Maybe log4j2 overrides mail configuration for any reason?
Could anyone help me?
Upvotes: 1
Views: 1256
Reputation: 115
Ok, I finally solved it. It have been working everytime, but it doesnt recognizes my application logger, so it only works with spring framework log names, for example: org.springframework.boot
I solved adding a new logger referencing my SMTPAppender:
<Logger name="com.myapp" level="all" additivity="false">
<AppenderRef ref="AsyncMailer" level="debug" />
</Logger>
And on a log.error("XXX"), it sends a mail with the log info.
Upvotes: 0