我正在开发一个 spring boot(3.3.5) Web 服务,使用 log4j2 作为我的日志库。我从 spring 初始化程序生成了一个 spring boot 项目,从 web 模块中排除了日志记录,以从 spring boot 中排除了 logback,并添加了以下 log4j2 推荐的依赖项
<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-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-spring-boot</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.3.4</version>
</dependency>
添加了属性来配置如何记录
<log.entry.pattern><![CDATA[%d{MM/dd/yyyy HH:mm:ss.SSS} | %t | %-5p | %c{1}: %m%n]]></log.entry.pattern>
<log.file.root>/service/logs</log.file.root>
<log.file.name>${log.file.root}/${project.artifactId}/${project.artifactId}.log</log.file.name>
<log.file.pattern>${log.file.root}/${project.artifactId}/${project.artifactId}-%d{yyyy-MM-dd}.log</log.file.pattern>
在 src/main/resources 中添加 log4j2-spring.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RollingFile name="FILE"
fileName="/service/logs/@project.artifactId@/@[email protected]"
filePattern="/service/logs/@project.artifactId@/@project.artifactId@-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern><![CDATA[%d{MM/dd/yyyy HH:mm:ss.SSS} | %t | %-5p | %c{1}: %m%n]]></pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>
添加了控制器方法,仅包含一些基本的日志消息
@GetMapping("/testlogs")
@ResponseStatus(HttpStatus.OK)
public void fetch() {
LOGGER.debug("Debug log message");
LOGGER.info("Info log message");
LOGGER.error("Error log message");
LOGGER.warn("Warn log message");
LOGGER.trace("Trace log message");
}
我的 jboss-deployment-struct.xml 如下所示
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
<subsystem name="datasources" />
<subsystem name="ejb3" />
<subsystem name="infinispan" />
<subsystem name="jaxrs" />
<subsystem name="jca" />
<subsystem name="jmx" />
<subsystem name="jpa" />
<subsystem name="jsf" />
<subsystem name="mail" />
<subsystem name="pojo" />
<subsystem name="remoting" />
<subsystem name="resource-adapters" />
<subsystem name="sar" />
<subsystem name="security" />
<subsystem name="transactions" />
<subsystem name="webservices" />
<subsystem name="weld" />
<subsystem name="bean-validation" />
</exclude-subsystems>
<exclusions>
<module name="javax.validation.api" />
<module name="javax.faces.api" />
<module name="org.hibernate.validator" />
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
我的文件夹结构如下
在 Jboss EAP 8 中遇到以下错误
{"WFLYDC0074: Operation failed or was rolled back on all servers. Server failures:" => {"server-group" => {"ProvAccess" => {"host" => {"BCDEVINTRAJB099-primary" => {"ProvAccs1_1" => {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"logtest.war\".undertow-deployment" => "java.lang.RuntimeException: java.lang.IllegalStateException: java.lang.IllegalStateException: Could not initialize Log4J2 logging from classpath:log4j2-spring.xml
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: java.lang.IllegalStateException: Could not initialize Log4J2 logging from classpath:log4j2-spring.xml
Caused by: java.lang.IllegalStateException: java.lang.IllegalStateException: Could not initialize Log4J2 logging from classpath:log4j2-spring.xml
Caused by: java.lang.IllegalStateException: Could not initialize Log4J2 logging from classpath:log4j2-spring.xml
Caused by: java.net.ProtocolException: Protocol vfs has not been enabled as an allowed protocol"}}}}}}}}
Log4j 配置文件具有与Spring XML bean 定义类似的功能,因此它必须来自受信任的来源(请参阅威胁模型)。这就是 Log4j Core 对可用于检索配置文件的 URL 进行限制的原因(请参阅
log4j2.configurationAllowedProtocols
配置属性。
默认情况下,只能从具有
file
、jar
或 https
模式的 URL 检索配置文件。在您的情况下,您的 log4j2-spring.xml
文件的位置将向使用 vfs:
架构的用户公开。将 log4j2.configurationAllowedProtocols
Log4j 配置属性设置为 vfs
应该可以解决您的问题。
您还可以通过将 log4j2.component.properties
文件添加到类路径的根目录来完成此操作,其内容为:
log4j2.configurationAllowedProtocols = vfs