Log4j Doesn't Log Messages - java

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.

Related

Log4j2 configuration file in Java EE project

I have simple Maven Java-EE based on Jersey web service in my Eclipse that runs in Windows 10. Project is created from jersey-quickstart-webapp artifact. I'm trying to use Log4j logger, but can't find where to place log4j2.xml file. I was trying to place it together with source code java files, but logger complains regarding configuration file not found. What is the right place to keep configuration file?
Put it into resources directory. Usually, all configuration files are kept there.
According to docs , Log4j2 will look for the configuration file in the following orders :
Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
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.
So you can see that apart from (1) , all are looking the configuration file from classpath . In the standard Maven project , /src/main/resources is the root of the classpath .So you can put log4j2.xml in /src/main/resources.
Otherwise, you have to explicitly specify the configuration file by log4j.configurationFile system property :
-Dlog4j.configurationFile=path/to/log4j2.xml

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.

the content of log4j.properties in log4j V2 and formating the logger output (for instance adding time stamp)

I am doing a Java project and I have already integrated the Log4j API Version 2 into my programme (CLearly for the first time and I have no idea how it works , thus if my question seems easy don't put the blame on me ) . The content of my log4j.properties is as follow :
log4j.rootLogger=DEBUG,SAWAN
log4j.appender.SAWAN=org.apache.log4j.ConsoleAppender
log4j.appender.SAWAN.layout=org.apache.log4j.SimpleLayout
and then I imported the log4j library into my class and for debugging purpose I've written the following and the output is shown as well
//My Code
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
//Output
DEBUG - Sample debug message
INFO - Sample info message
WARN - Sample warn message
ERROR - Sample error message
FATAL - Sample fatal message
which means that the log4j is working fine . How ever I'd like to change the format of the output and add time stamp to it . Based on my research on the other questions asked in this site and referring to https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html I know that I need to use something like :
[%t] %-5p %c %x - %m%n
but once I added this to my log4j.properties I recived an error .My question is where should I specify the output format for my log outputs .
Here are the libraries I've imported as well :
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.PatternLayout;
Worth mentioning that I've tried adding the following to my log4j.properties : log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n
yet I am keep getting errors that some thing is wrong with my log4j.properties which is not the case when I remove it and it would work fine :)
You need a file named log4j2.properties on your classpath to be picked up automatically.
Documentation from Log4j2:
Log4j has the ability to automatically configure itself during
initialization. When Log4j starts it will locate all the
ConfigurationFactory plugins and arrange them in weighted order from
highest to lowest. As delivered, Log4j contains four
ConfigurationFactory implementations: one for JSON, one for YAML, one
for properties, and one for XML.
Log4j will inspect the "log4j.configurationFile" system property and,
if set, will attempt to load the configuration using the
ConfigurationFactory that matches the file extension.
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.
You can find all documentation on log4j2 configuration here: https://logging.apache.org/log4j/2.x/manual/configuration.html
Thanks to D.B. for pointing out the correct answer.

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

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));

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.

Categories

Resources