I'm building a backend using Spring Boot 3.1.0-SNAPSHOT
with the spring-boot-starter-log4j2
for logging.
Due to we are using log4j2 and not Logback, we have excluded it as follows:
configurations {
all*.exclude module: 'spring-boot-starter-logging'
}
So far, all is working as expected.
Now I have a necessity to add an internal dependency of another department to my gradle file, example:
implementation 'com.test.ws:example:5.4'
Warning:
log4j:WARN No appenders could be found for logger (com.test.ws.jaxrpc.ExamplePortProxyFactoryBean). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Looking at the class of the dependency that is causing this warning, I can see that indeed it has a Logger:
import org.apache.log4j.Logger;
import org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean;
public class ExamplePortProxyFactoryBean extends JaxRpcPortProxyFactoryBean {
private static final Logger logger = Logger.getLogger(ExamplePortProxyFactoryBean.class);
public ExamplePortProxyFactoryBean() {
}
protected void postProcessJaxRpcService(Service arg0) {
if (logger.isDebugEnabled()) {
logger.debug("postProcessJaxRpcService(Service arg0=" + arg0 + ") - start");
}
TypeMappingRegistry registry = arg0.getTypeMappingRegistry();
TypeMapping mapping = registry.createTypeMapping();
PortProxyFactoryBeanUtil.registerBeanMapping(mapping, List.class, "List", this.getNamespaceUri());
registry.register("http://schemas.xmlsoap.org/soap/encoding/", mapping);
if (logger.isDebugEnabled()) {
logger.debug("postProcessJaxRpcService(Service arg0=" + arg0 + ") - end ${message_user}");
}
}
}
Inside the dependency's pom.xml file, I cannot see any reference to the org.apache.log4j
library.
The warning is just a warning, not an error, the app works fine, but I feel that I need to fix this warning because is ugly to see it at service start up.
I have tried to exclude log4j as follows:
implementation('com.orange:odo-ws-endpoints:5.4') {
exclude group: 'log4j', module: 'log4j'
}
Error:
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[?:?]
at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[?:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]
at com.test.ws.jaxrpc.ExamplePortProxyFactoryBean.<clinit>(ContractPortProxyFactoryBean.java:28)
Not the best idea I guess.
Other threads indicate (questions already on Stack Overflow) that I should create a file called log4j.properties
inside the /resources folder.
log4j.properties:
# Root logger option
log4j.rootLogger=DEBUG, console
# Redirect log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %5p %c{1} : %msg%n
Now with this file, the error is gone and I can see the logs that the class above is firing. The thing is that is weird for me to have this file because of that class, guilty.
This thread https://stackoverflow.com/a/12532442/10977224 talks about another option to configure a basic appender by adding this line in my main method:
BasicConfigurator.configure();
Effectively, the warning is gone and I can see the logs of the class. But still, why do I need to handle a necessity of a class that is not under my power.
Questions:
- What other things can I do to fix the warning without having a log4j.properties or adding more code?
- There is a way to avoid showing the warnings of that class in the console?
- To avoid confusion within my team and weird future log behaviours, if any, of having things related to log4j while we use
spring-boot-starter-log4j2
, there is a way to apply a fix only to the guilty class (or package)?
Any other approach or advice will be appreciated. I'm not sure how to handle this correctly.