How to programmatically get the LogBack configuration file path/name? - java

I'm using LogBack with Slf4j.
At program startup LogBack searches the configuration file from various places and in case of finding nothing, it configures itself automatically by using BasicConfigurator.
http://logback.qos.ch/manual/configuration.html
At program startup I need to print out the used LogBack configuration file name.
How can I programmatically get the name/path of the loaded LogBack xml configuration file or some information if LogBack did not find any configuration file and configured itself automatically?

Would it be enough to set debug=true on logback.xml files? If not, following seems to work at least with logback 1.1.5 version but will create a direct dependency to logback:
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
System.out
.println(ConfigurationWatchListUtil.getConfigurationWatchList(context).getCopyOfFileWatchList().get(0));

Related

log4j2 log file is not generating

the log file is generated when I run the code within IDE (Intellij IDEA).
as soon as I create runnable jar of the code and then try to run the jar then the logs are not generating.
I have made sure the log4j2.xml file is a part of classpath.
is there anything extra I have to do while creating jar in the Intellij IDEA?
Taken from the FAQ: How do I debug my configuration?
First, make sure you have the right jar files on your classpath. You need at least log4j-api and log4j-core.
Next, check the name of your configuration file. By default, log4j2 will look for a configuration file named log4j2.xml on the classpath. Note the “2” in the file name! (See the configuration manual page for more details.)
From log4j-2.9 onward
From log4j-2.9 onward, log4j2 will print all internal logging to the console if system property log4j2.debug is either defined empty or its value equals to true (ignoring case).
Prior to log4j-2.9
Prior to log4j-2.9, there are two places where internal logging can be controlled:
If the configuration file is found correctly, log4j2 internal status logging can be controlled by setting in the configuration file. This will display detailed log4j2-internal log statements on the console about what happens during the configuration process. This may be useful to trouble-shoot configuration issues. By default the status logger level is WARN, so you only see notifications when there is a problem.
If the configuration file is not found correctly, you can still enable log4j2 internal status logging by setting system property -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE.

Log4j Doesn't Log Messages

So I am programming a game and I decided I should switch from System.out.println to an actual logging api and decided to use Log4j. I followed a tutorial to use the basic configuration but when I call logger.info("string stuff"); it doesn't actually log anything.
I have:
public static final Logger logger = Logger.getLogger("Main.class");
And
public static void main(String[] args) {
BasicConfigurator.configure();
logger.info("Starting Game...");
}
You should configure Log4j with either command line options or a configuration file to enable the info level logging to an appender e.g. system console. Assuming you are using Log4j2 check out the Configuration docs.
The order of processing configuration in Log4j2 is:
Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension. Note that this
is not restricted to a location on the local file system and may
contain a URL.
If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the
classpath.
If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
If you haven't configured anything you are most likely on point 10 and using the default configuration. This will only log error level and higher to console while you are using info level, which is lower. As per docs:
Log4j will provide a default configuration if it cannot locate a configuration file. The default configuration, provided in the DefaultConfiguration class, will set up:
A ConsoleAppender attached to the root logger.
A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppender
Note that by default Log4j assigns the root logger to Level.ERROR.

How to know if logback configuration file has loaded?

I am new to SLF4j and I don't know if the logback.xml file has loaded properly or not. The logback.xml file is in PROJECTNAME/src/main/java where all my packages are found.
My questions are:
How can I know if the configuration file has properly loaded or not
?
How can restrict the logging only from an explicit set of class,
only to avoid logging from libraries
You can add the debug="true" attribute to the <configuration> element to enable debug of the logback configuration. It will print the configuration to the console. See https://logback.qos.ch/manual/configuration.html#dumpingStatusData.
Simple answer, if the configuration file is loaded properly, you will see results in log file or console, depending on your configuration.
By default, logback searches file in src/main/resources instead of src/main/java if I remember correctly.
In the configuration file, you can define log lever on a specific logger. Normally you'll still want to see logs of the libraries, but maybe only WARN or ERROR, so you could set the root level to WARN/ERROR, and add a logger of your root package with DEBUG/INFO level.
Also, use a logback-test file (under src/test/resources) for your own dev environment.

How to set the log level on a class in log4j2 properties

In log4j I could specify a class in the properties file to log at the debug level like this:
log4j.logger.com.mycompany.mypackage.ClassName=DEBUG
How do I do this in log4j2? Note I still need to use the new property file (not xml or json).
As the log4j2 configuration documentation states
As of version 2.4, Log4j now supports configuration via properties
files. Note that the property syntax is NOT the same as the syntax
used in Log4j 1.
It then provides a substantial example for all types of configuration elements.
Concerning your question, you need to specify your loggers in a loggers element, then configure each of them. For example
loggers = mine
logger.mine.name = com.mycompany.mypackage.ClassName
logger.mine.level = DEBUG
Note that log4j2 looks for a .properties file on the classpath by default.
If a test file cannot be located the properties ConfigurationFactory
will look for log4j2.properties on the classpath.
But you can also configure the location yourself. You can use the system property
-Dlog4j.configurationFile=conf/log4j.properties
with an appropriate path.
If you are using spring boot, you can use
logging.level.com.mycompany.mypackage.ClassName=DEBUG
in application.properties.

log4j logger overwriting into jxl.log file

I am using log4j for logger purpose. At the same time I am also using JXL to read/write Excel file.
But instead of writing log into log4j logger file, it is writing into jxl.log file.
What can be issue?
Looks like you have been using jxl-2.6.3.jar or similar version.
Log4j picks up the first configuration file with default file name ( i.e. log4j.xml or log4j.properties ) in your classpath if you haven't specified a specific name via JVM parameters. As jxl-2.6.3.jar contains a log4j.xml you ended up printing everything to jxl.log as defined in the log4j.xml
The best way to deal with these kind of problems is to run your application with -Dlog4j.debug JVM parameter. This would print a few line snippet when the log4j is initialized.
log4j: Using URL [jar:file:/C:/YourApp/WEB-INF/lib/jxl-2.6.3.jar!/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
...{Blah Blah Blah}
There are many ways in which you can solve this problem.
Use the newer versions of jxl which doesn't contain log4j.xml.
Make sure your log4j.properties file is on top of classpath.
Remove the log4j.xml from the jxl-2.6.3.jar (Dirty solution).
Pass the configuration file name in VM parameter as -Dlog4j.configuration=log4j.properties. This would atleast make sure log4j.xml inside jxl-2.6.3.jar will not be used. (But what if another jar with same name as log4j.properties?).
Rename your log4j.properties file to log4j-yourApp.properties and add VM parameter -Dlog4j.configuration=log4j-yourApp.properties This would definitely help and this is how it should be done to avoid these kind of situations.
More details on Log4j here

Categories

Resources