Force a specific logger in Spring Boot - java

We wanted to switch from standard logback to log4j in Spring Boot 2.4.x.
For most modules, it is a no-brainer by simply removing the dependency of logback, but there are some modules, which are using pact-jvm as a shadow-jar dependency to be able to create the pact files from unit tests.
Now the odyssey begins, because pact needs logback and with the pact-jar on the classpath Spring recognizes the logback class it is looking for and decides to use logback instead of log4j as the logger in the tests.
Is there a possibility to create something like a log bean or a hidden configuration, which allows forcing Spring to use log4j instead of logback, also if logback is on the classpath?
Pact should not be an issue at this point, because the server is started as a standalone server from gradle. We only require the dependency to be able to start everything and to have the classes available.
Thanks!

You may follow these steps to configure log4j with spring boot
Add the log4j dependency to your module
Create logger object in your respective class and use the logger object to log the messages.
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(<YourClassName>.class);
In your spring boot module/project add log4j.properties file in resources so that this will be available on class path at runtime. Following is sample content for the log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Make sure that the spring boot module bundles the log4j jar when you build.
Once spring boot finds the log4j jar and the log4j.properties on the class path it will be able to initialize the log4j logger for you.

Yes, that's clear. The issue is, that Spring Boots Autoconfiguration is loading the logger for slf4j (sorry forgot that). So here it iterates over the supported candidates in the order:
logback
log4j
apache-log-commons
And it seems that it does not look for beans, it simply looks for the classes by reflection.
If we now start the app for testing together with pact server, Spring Boot resolves an Append* class and thinks, that this has to be the logger of our choice.
It creates the logback instance, which is searching for its config, but then it fails, because the config is for log4j.

Related

Spark 2.2.0: Log4J configured before logger is instantiated

I have a project using custom log4j property files and levels that worked in Spark versions 2.0.2 or older. An upgrade to Spark 2.2.0 was needed to resolve some performance issues, but it breaks the log4j components.
The log4j properties file makes references to a system property "current.date", as well as a custom log level class in the package:
# Root logger option
log4j.rootLogger=INFO,file,file2
log4j.logger.com.package.project.PROJECTDriver=PROJECT#com.package.project.PROJECTLogLevel,file3
log4j.appender.file3=org.apache.log4j.RollingFileAppender
log4j.appender.file3.File=../../../../project/log/PROJECTUserLog_${current.date}.log
log4j.appender.file3.Append=false
log4j.appender.file3.MaxFileSize=100MB
log4j.appender.file3.MaxBackupIndex=1
log4j.appender.file3.layout=org.apache.log4j.PatternLayout
log4j.appender.file3.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file3.threshold=PROJECT#com.package.project.PROJECTLogLevel
log4j.appender.file3.filter.filter1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.file3.filter.filter1.levelMax=INFO
The references are declared in the code prior to instantiating the log4j logger:
val dateFormat = new SimpleDateFormat("yyyyMMddhhmmss")
System.out.println("Starting Project Driver")
System.setProperty("current.date", dateFormat.format(new Date()))
var log: Logger = LogManager.getLogger("com.package.project.PROJECTDriver")
After upgrading to Spark 2.2.0, Log4j started to complain that it could not find the custom log level class. It was also not appending the current.date value, just leaving it blank.
After some digging around and playing with log4j debug mode, I found that in Spark 2.2.0, log4j was being configured before the project code was ran. In the previous Spark versions, the debug output looks like this:
Starting Project Driver
log4j: *log4j configuration steps, going through the properties file*
In Spark 2.2.0, the order is reversed:
log4j: *log4j configuration steps, going through the properties file*
Starting Project Driver
I couldn't find any notes on this change in Spark 2.2.0. Is there any way to fix log4j so that configuration starts when the logger is called?

How do I control logging in 3rd party libraries

I have a Tomcat server running a Spring-based servlet.
I've set up [project root]/src/log4j.properties file as below:
# Root logger option
log4j.rootLogger=WARN, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss} %m [%c{3}:%L]%n
log4j.logger.com.martincarney.bugTracker=DEBUG
log4j.logger.com.martincarney.bugTracker.controller=ERROR
This correctly logs my own code just fine, but doesn't seem to have any effect on logging from within the various libraries I'm using. For example, I still get INFO logs from org.apache.* to the Eclipse console error stream during Tomcat startup, even if I add log4j.logger.org.apache=WARN to my log4j.properties.
I'm using slf4j-api and slf4j-log4j jars, obtained through Maven.
How can I take control of logging levels and targets outside my own code?
Some libraries use other logging frameworks like java.util.logging.
You could redirect logging with SLF4J, see SLF4J - Bridging legacy APIs:
Redirection for Jakarta Commons Logging:
To ease migration to SLF4J from JCL, SLF4J distributions include the jar file jcl-over-slf4j.jar. This jar file is intended as a drop-in replacement for JCL version 1.1.1. It implements the public API of JCL but using SLF4J underneath, hence the name "JCL over SLF4J."
Redirection for java.util.Logging (SLF4J API):
Installation via logging.properties configuration file:
// register SLF4JBridgeHandler as handler for the j.u.l. root logger
handlers = org.slf4j.bridge.SLF4JBridgeHandler
For configuration of java.util.Logging see JUL API.
Some libraries like Apache CXF supports more than one logging framework, see Apache CXF - Debugging and Logging .

How to make slf4j print its own configuration?

log4j has a system property called log4j.debug, that when set by adding -Dlog4j.debug=true to your command line, prints out the information about how log4j configures itself (for example, the location of the con file it found and loaded).
I am looking for a similar capability for slf4j. Can we tell slf4j to print out how it is set and configured?
slf4j is wrapper for other loggin systems (the f in the name stands for facade), it does not have its own configuration.
With slf4j you can even use log4j as real logging library. If you are using logback as logging library along with slf4j there is an attribute debug for the main configuration tag (if you are using an xml file for configuring logback).
<configuration debug="true">
....your conf here
</configuration>
the logback mnanual can be found here

unable to see spring logging with log4j

I have log4j configured in my j2ee application,
I am able to see log messages from my classes fine - I am unable to see messages from the spring framework (to debug #RequestMapping issues)
I am using latest spring (4.0) and log4j (1.2.17)
This is my log4j.properties:
log4j.appender.H=org.apache.log4j.RollingFileAppender
log4j.appender.H.File=c:/tmp/AppLog.html
log4j.appender.H.MaxFileSize=4000KB
log4j.appender.H.Append=false
log4j.appender.H.encoding=UTF-8
log4j.appender.H.layout=org.apache.log4j.HTMLLayout
log4j.rootCategory=DEBUG, H
I added this to allow Spring to log,
but no output from spring.....
##Spring Framework
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.web=DEBUG
In your log4j.properties file make the following changes:
From:
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.web=DEBUG
To:
log4j.category.org.springframework=DEBUG
log4j.category.org.springframework.web=DEBUG

Log4j doesn't log INFO Level

I have the following log4j.properties file, for an application deployed in WebSphere Portal:
log4j.rootLogger=DEBUG, InfoAppender, DebugAppender
log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/info.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/debug.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - %m%n
When I code, I define the logger at class level:
private static Logger logger = Logger.getLogger(IWannaLogThis.class);
And I log INFO messages with this:
logger.info(theObjectToLog);
When I deploy my application, the debug.log file gets everything I log with logger.debug() but ignores everything I write with logger.info(). On the other side, the info.log file keeps empty.
The weirdest thing is that in debug.log and info.log appears some INFO and DEBUG messages made by some JARS (like Hibernate Validator) I had in the classpath, but just ignores everything I try to log in my code.
Any ideas?
This is most likely a classloading-related problem. WebSphere Portal uses Log4J internally, so I'm guessing that you end up using WebSphere Portal's provided Log4J JAR file as well as its own Log4J properties.
You can verify that by adding the following to the JVM arguments of the server instance:
-Dlog4j.debug=true
And then inspect the SystemOut.log file. Log4J will spit out lots of tracing information about the configuration file(s) it reads.
The best way to avoid this is to do the following:
Bundle the Log4J JAR file with your application.
Associate a Shared Library with the server. In that Shared Library, place your Log4J configuration file.
As an alternative to step 2, you can bundle your Log4J configuration file with the application itself, however that would carry its own drawbacks (for example, having to repackage your application whenever you perform a Log4J configuration change).
Another common problem is that the JARs you have in your classpath also use log4j and also have their own appenders set. So depending on the settings that they use, and the packages that your classes reside in, this may lead to the problem you describe.
So:
Make sure that your package names are unique and not used by any of the third party libraries.
Check the log4j settings in all libraries in your classpath. They should not contain general settings which override yours.
Make sure your loggers use your log4j.properties (you can be sure if changes you make in your file affect your loggers as expected).
If you can, make sure that your log4j stuff loads last, in case any of the third party libs reset the configuration. They shouldn't, but who can stop them.
Normally, it should be one of these things. Post more explicit example if it doesn't work.
Good luck!
What I have done in the past is set specific logs for the classes I want to log. It sounds like you can try setting your root logger to INFO and see if that gets you the messages you want. Here's a little bit of my log4j property file. I set a logger for each class and assign it to my "data" appender, which defines the log layout. In the loggers I specify specific classes I want to log and set their Log level individually. Any class that logs that is not defined in the Loggers I have use the default log level for the rootCategory.
log4j.rootCategory=INFO, rollingFile, stdout
#GetData Loggers
log4j.logger.com.myapp.data=INFO, data
log4j.logger.com.myapp.data.SybaseConnection=DEBUG, data
log4j.logger.com.myapp.data.GetData=ERROR, data
# data appender
log4j.appender.data=org.apache.log4j.RollingFileAppender
log4j.appender.data.layout=org.apache.log4j.PatternLayout
log4j.appender.data.File=c\:\\Program Files\\MyApp\\logs\\MyApp-data.log
log4j.appender.data.Append=true
log4j.appender.data.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
you root logger opens the log properties in the debug mode,
use INFO instead of DEbug in the first line of your properties file.

Categories