I am working on developing a spring boot(3.3.5) web service with log4j2 as my logging library. I generated a spring boot project from spring initializer, excluded the logging from web module to exclude the logback from the spring boot and added the following log4j2 recommended dependencies
<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>
added properties to config how to log
<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>
added log4j2-spring.xml as follows in the src/main/resources
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RollingFile name="FILE"
fileName="/service/logs/@project.artifactId@/@project.artifactId@.log"
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>
added controller method to just have some basic logging messages
@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");
}
My jboss-deployment-structure.xml looks as below
<?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>
my folder structure is as follows
experiencing the following error in 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"}}}}}}}}
The Log4j configuration file has similar capabilities as the Spring XML bean definitions and for these reason it must come from a trusted source (see the threat model). This is why Log4j Core places restrictions on the URLs that can be used to retrieve the configuration file (see log4j2.configurationAllowedProtocols
configuration property.
By default, configuration files can only be retrieved from URLs with a file
, jar
or https
schema. In your case the location of your log4j2-spring.xml
file is exposed to users using a vfs:
schema. Setting the log4j2.configurationAllowedProtocols
Log4j configuration property to vfs
should solve your problem.
You can also do it by adding a log4j2.component.properties
file to the root of your classpath with content:
log4j2.configurationAllowedProtocols = vfs