Spring boot版本3.3.3,在我的linux系统中,我将在/opt下有两个tomcat实例,一个名为
/opt/tomcat
(这是开发环境,在conf/catalina.properties中用spring.profiles.active=dev
指定),另一个文件夹叫做 /opt/tomcat_qa
,也指定了 profile 给 qa。
我想将
SLF4j
与 log4j2
一起使用,因此在我的 pom.xml
中,它看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<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>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xxx</groupId>
<artifactId>yyy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>OAuth2Server</name>
<description>OAuth2Server</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-oauth2-authorization-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.8.4</version>
</dependency>
<!-- comman lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.15.0</version>
</dependency>
<!-- 实现对 Actuator 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- SLF4J API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<!-- Log4j2 Core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.24.0</version> <!-- 使用适合你的项目的版本 -->
</dependency>
<!-- Log4j2 API -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.24.0</version>
</dependency>
<!-- Log4j to SLF4J Bridge -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.24.0</version>
</dependency>
</dependencies>
<build>
<finalName>EppAuthServer</finalName>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>4.0.0.4121</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</project>
我还创建了
log4j2-spring.xml
文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!--TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF-->
<Configuration status="OFF" monitorInterval="30">
<!-- 定义一些变量值,可供下面配置使用,例如日志存储目录等 -->
<Properties>
<Property name="APP_NAME">AuthServer</Property>
<Property name="LOG_HOME">${spring:logging.base}/logs</Property>
<Property name="SPLIT_SIZE">20 MB</Property>
</Properties>
<!-- appenders that kind of the stuff.... -->
</Configuration>
在我的
application.yaml
文件中我将其设置如下:
# other config
logging:
base: /opt/tomcat #(for the QA will be /opt/tomcat_qa)
config: classpath:log4j2-spring.xml
我的
QA
很好,所有INFO
或更高版本都会写入AuthServer-info.log
,ERROR
日志将写入AuthServer-Error.log
,也与catalina.out
一起写入。
但是对于我的
dev
,无论我如何更改配置,AuthServer-info.log
和AuthServer-error.log
都不会更新,并且所有日志都写入到catalina.out
。
我的
dev
tomcat 实例是否需要修改某个地方?
我不知道如何使用弹簧配置文件来控制
log4j2-spring.xml
。
我知道您可以在
log4j2-spring.xml
中使用环境变量或 JVM 参数,使用标准 log4j2
属性替换,如 记录在此处:
dev/qa
中的log4j2-spring.xml
配置文件指定正确的日志文件。
dev
export APP_LOG_HOME=/opt/logs/dev
环境变量示例
qa
export APP_LOG_HOME=/opt/logs/qa
在
log4j2-spring.xml
中注入环境变量。
<?xml version="1.0" encoding="UTF-8"?>
<!--TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF-->
<Configuration status="OFF" monitorInterval="30">
<!-- 定义一些变量值,可供下面配置使用,例如日志存储目录等 -->
<Properties>
<Property name="APP_NAME">AuthServer</Property>
<Property name="LOG_HOME">${env:APP_LOG_HOME}/logs</Property>
<Property name="SPLIT_SIZE">20 MB</Property>
</Properties>
<!-- appenders that kind of the stuff.... -->
</Configuration>
如果上面的选项 1 不实用,请使用 JVM 参数(在
log4j2
文档中称为系统属性)为 dev/qa
中的
log4j2-spring.xml
配置文件指定正确的日志主目录
确保在
log4j2-spring.xml
中正确使用 LOG_HOME
属性和 APP_NAME
属性。
<Appenders>
<RollingFile name="MainLog" fileName="${LOG_HOME}/${APP_NAME}.log" filePattern="${LOG_HOME}/${APP_NAME}_%d{MM-dd-yyyy}_%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>