These are the logging dependencies in my pom file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.20.0</version>
</dependency>
This is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Development -->
<Configuration status="trace" strict="true" monitorInterval="30">
<Properties>
<Property name="logs">../logs/rpay/batch</Property>
</Properties>
<Appenders>
<Appender type="Console" name="console" target="SYSTEM_OUT">
<Layout type="PatternLayout" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [executionId:%X{executionId}] %level: %msg%n"/>
</Appender>
<Appender type="RollingFile" name="standard" filename="${logs}/standard.log" filePattern="${logs}/standard.%d{yyyy-MM-dd}.log.gz">
<Layout type="PatternLayout" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [executionId:%X{executionId}] %level: %msg%n"/>
<Policy type="TimeBasedTriggeringPolicy" />
</Appender>
<Appender type="RollingFile" name="error" filename="${logs}/error.log" filePattern="${logs}/error.%d{yyyy-MM-dd}.log.gz">
<Layout type="PatternLayout" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [executionId:%X{executionId}] %level: %msg%n%exception%n"/>
<Policy type="TimeBasedTriggeringPolicy" />
</Appender>
<Appender type="RollingFile" name="outside" filename="${logs}/outside.log" filePattern="${logs}/outside.%d{yyyy-MM-dd}.log.gz">
<Layout type="PatternLayout" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS},%t,%X{executionId},%msg%n"/>
<Policy type="TimeBasedTriggeringPolicy" />
</Appender>
</Appenders>
<Loggers>
<Logger name="error" level="debug" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="standard" />
<AppenderRef ref="error"/>
</Logger>
<Logger name="outside" level="info" additivity="false">
<AppenderRef ref="outside" />
</Logger>
<Root level="debug">
<AppenderRef ref="console" />
<AppenderRef ref="standard" />
</Root>
</Loggers>
</Configuration>
I tried all the solutions listed here.
How do I fix "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"."
But the error did not get resolved.
Your setup should work in GlassFish 7.0.10 or newer.
In an older GlassFish version or in Payara server it doesn't work because of how classes are loaded by the classloader. There, the slf4j classes are present in the GlassFish/Payara installation. They are loaded by a parent classloader and they don't see log4j binding classes in your application. This was discussed in https://stackoverflow.com/a/77074140/784594 and confirmed that it works in GlassFish 7.0.10.
A workaround is to copy the log4j and slf4j JAR files and the log4j.xml file from your application into GlassFish domain, into the lib directory, so that they are loaded by the same classloader which loads libraries in the GlassFish installation.