gpt4 book ai didi

java - 使用 log4j2 和 tomcat 进行 Spring 启动

转载 作者:行者123 更新时间:2023-12-01 16:23:47 27 4
gpt4 key购买 nike

我在使用 Spring Boot Web 应用程序时遇到了一些麻烦。我没有使用嵌入式tomcat,我禁用了它。我的网络应用程序使用 Log4j2 作为记录器。在 Eclipse 中调试时没有问题并创建我的日志文件,但使用外部 tomcat 则不会。这是我的 pom 文件:

<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>

我的Log4j2配置文件。我喜欢 yml 文件。

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

我不想使用 Log4j2 作为 tomcat 的记录器,而只是为了我的应用程序。问题和解决方案可能是什么?

最佳答案

如果有人遇到此问题,请将 ${catalina.home} 更改为 ${sys:catalina.home},文档会有所帮助。

http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

关于java - 使用 log4j2 和 tomcat 进行 Spring 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62201035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com