Add async logging with log4j2 and disruptor

This commit is contained in:
Serafeim Chatzopoulos 2024-03-01 16:39:45 +02:00
parent 636e5c8b6e
commit 6a509e8462
5 changed files with 81 additions and 0 deletions

3
.gitignore vendored
View File

@ -31,3 +31,6 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
### Application logs ###
logs/

30
pom.xml
View File

@ -20,6 +20,18 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude default loggers -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
@ -53,6 +65,24 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>com.lmax</groupId>-->
<!-- <artifactId>disruptor</artifactId>-->
<!-- <version>4.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<!-- Add Log4j2 Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -8,3 +8,7 @@ solr.collection=TMF-index-openaire
solr.zkHosts=zookeeper-solr-openaire-dev-1:2181,zookeeper-solr-openaire-dev-2:2181,zookeeper-solr-openaire-dev-3:2181 solr.zkHosts=zookeeper-solr-openaire-dev-1:2181,zookeeper-solr-openaire-dev-2:2181,zookeeper-solr-openaire-dev-3:2181
logging.level.org.springframework.web=DEBUG logging.level.org.springframework.web=DEBUG
#removes 'trace' field from error responses; alternatively remove 'spring-boot-devtools' dependency
server.error.include-stacktrace=never

View File

@ -0,0 +1 @@
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="trace">
<Properties>
<Property name="logpath-location">logs/</Property>
<Property name="logfile-name">api.log</Property>
<Property name="archive">${logpath-location}/archive/api</Property>
<Property name="interval">10</Property>
</Properties>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %C.%M():%L %X - %m%n"/>
</Console>
<RollingFile name="RollingFileAppender" fileName="${logpath-location}/${logfile-name}"
filePattern="${archive}/${logfile-name}.%d{yyyy-MM-dd-HH}.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c.%M ():%L %X - %m%n"/>
<Policies>
<!-- New log file every day -->
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
<!-- Keep 30 rolling files for logging -->
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.projects.spring_web_service" level="DEBUG" additivity="false" includeLocation="true">
<AppenderRef ref="Console" level="INFO"/>
<AppenderRef ref="RollingFileAppender" level="DEBUG"/>
</Logger>
<Root level="INFO" includeLocation="true">
<AppenderRef ref="Console" level="INFO"/>
<AppenderRef ref="RollingFileAppender" level="DEBUG"/>
</Root>
</Loggers>
</Configuration>