How to make slf4j print its own configuration? - java

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

Related

log4j1 to log4j2 bridge autoconfiguration is not being created

I followed steps for migrating log4j1 to log4j2 using bridge, https://logging.apache.org/log4j/2.x/manual/migration.html
The thing is it doesn't create automatic configuration even if I set the system property of log4j1.compatibility = true. I don't have idea what is happening because the console doesn't even show any errors that configuration file is missing.
Extra information:
It stopped logging when I replaced log4j 1.2.16 by these three jars
log4j-1.2-api-2.17.1
log4j-api-2.17.1
log4j-core-2.17.1

Not able to print logs to Tomcat's CATALINA_BASE by using log4j2

I have update our code from log4j to log4j 2.17.1 And I want to stored the log file to servers under the Apache tomcat. I am using the log4j2.properties mentioned below.
When I run the code, then the logs file is printed in under code structure(see in below attached screenshot)but I want to print the logs file in QA-Servers under apache tomcat.
Please help me to solve the issue.
TL;DR: use ${sys:catalina.base}.
The property substitution in Log4j 2.x differs from Log4j 1.x (cf. documentation). The most prominent change is that:
in Log4j 1.x ${catalina.base} is looked up in Java system properties and, if the system property does not exist, in the configuration file,
in Log4j 2.x ${catalina.base} is looked up only in the configuration file.
In both cases if the property can not be resolved the placeholder is left unchanged.
In Log4j 2.x all external property lookups must be prefixed using an appropriate prefix. The exact equivalent of Log4j 1.x behavior is ${sys:catalina.base}. Therefore you can use:
# Fallback
property.catalina.base=.
appender.rolling.fileName=${sys:catalina.base}/logs/aseq_wiptmobile_qa-1.applog

Force a specific logger in Spring Boot

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.

What is the preferred method for creating a custom appender in log4j2?

The log4j2 documentation shows the preferred method to produce custom components (such as appenders, filters and lookups). However, it is unclear on how to ensure that these custom components are picked up by the logging framework and useable by a logging configuration.
After placing a custom appender with the correct annotations in the classpath, what else do I need to do to be able to start including it in my log4j2.xml file and have it picked up by the logging framework?
To provide an example of this, if I annotate a custon appender with:
#Plugin(name = "Stub", category = "Core", elementType = "appender", printObject = true)
how do I get it to be picked up and used like this in the log4j2.xml configuration:
...
<Appenders>
<Stub name="stub">
...
</Stub>
...
</Appenders>
...
You need to put the package name (or names in a comma-separated list if multiple) in the packages attribute of the Configuration element of your log4j2.xml.
E.g.
<Configuration status="trace" packages="com.mycomp.myproject.appenders">
...
See the Log4j2 Configuration Documentation for more information.
I can't post a comment yet, so I'm writing this as an answer.
packages attribute in configuration element is no longer supported (since version 2.0-rc2), see this answer: https://stackoverflow.com/a/24931395/3145863.
It should be included in the 2.0.1 release of log4j (https://issues.apache.org/jira/browse/LOG4J2-741)
For now, I recommend you to use Maven to build your project.

config log4j via slf4j

we have a standalone java project using log4j for logging,and we don't config log4j via the classpath configuration. we config it in my code as bellow:
String configLocation = System.getProperty("S_HOME") + File.separator + "config" + File.separator + "xxxLog.properties";
PropertyConfigurator.configure(configLocation)
but if we move to slf4j,how can I config log4j via slf4j?
THKS
You can't use slf4j for that, you just keep your old configuration code for log4j.
slf4j does not handle the configuration part, which would be very difficult to generalize for all supported logging systems. slf4j is just an API for handling logging calls that dispatches to a particular logging implementation. It also offers a number of bridges, such as redirecting java.util.logging to slf4j.
What happens with the log output is not part of the slf4j API.

Categories