Spring Boot with log4j2 and tomcat… here is a solution to the problem.
Spring Boot with log4j2 and tomcat
I’m having some trouble using the Spring Boot web application. I didn’t use embedded tomcat, I disabled it. My web application uses Log4j2 as the logger. No problem debugging in Eclipse and creating my log file, but not with external tomcat.
Here is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>pos.com.incidents</groupId>
<artifactId>pos-com-incidents</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>pos-com-incidents</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring -->
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Informix -->
<dependency>
<groupId>JDBCDrivers.Informix</groupId>
<artifactId>ifxjdbc</artifactId>
<version>4.10.JC4</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
<!-- Joda -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My Log4j2 configuration file. I love yml files.
Configuration:
status: warn
appenders:
Console:
name: AppConsole
target: SYSTEM_OUT
PatternLayout:
Pattern: "%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}} {bright,yellow}: %msg%n%throwable"
RollingFile:
- name: AppDetail
fileName: ${catalina.home}/logs/Incidents.log
filePattern: "${catalina.home}/logs/Incidents-%d{-yyyy-MM-dd-}-%i.log.gz"
PatternLayout:
pattern: "%d %p %C{1.} [%t] %m%n"
Policies:
OnStartupTriggeringPolicy: {}
SizeBasedTriggeringPolicy:
size: 50 MB
DefaultRollOverStrategy:
max: 30
Loggers:
logger:
- name: pos.sams.incidents.logging
level: info
additivity: false
AppenderRef:
- ref: AppDetail
- ref: AppConsole
Root:
level: info
AppenderRef:
ref: AppConsole
I don’t want to use Log4j2 as a logger for tomcat, but just for my application. What are the possible problems and solutions?
Solution
If anyone runs into this issue, please change ${catalina.home} to ${sys:catalina.home}
, the documentation will help.
http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution