I cannot get Log4j 2 to log to the console. Nothing is showing up when running with gradle.
log4j2.xml
in the projects root directory:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ALL">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Usage in my classes:
public class ABCHandler {
private final Logger logger = LogManager.getLogger();
public ABC(String serialPortName) {
logger.info("Opening serial port {}", serialPortName);
}
}
This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.
We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.
log4j's internal debugging can be enabled by setting the log4j. debug system property. During startup, log4j logs shows from where and how the configuration is loaded.
Loading your file and configurations on my machine works.
This was the class I used:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test
{
private final Logger logger = LogManager.getLogger(Test.class);
public Test(String serialPortName) {
System.out.println(logger.isInfoEnabled());
logger.entry();
logger.info("info! {}", serialPortName);
logger.error("error! {}", serialPortName);
logger.debug("debug! {}", serialPortName);
}
public static void main(String args[])
{
Test h1 = new Test("1001");
}
}
This is the log4j2.xml:
<ThresholdFilter level="all"/>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p method: [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
and finally, this is the output:
true
2014-05-29 12:19:15,266 TRACE method: [main] Test (Test.java:10) - entry
2014-05-29 12:19:15,268 INFO method: [main] Test (Test.java:11) - info! 1001
2014-05-29 12:19:15,269 ERROR method: [main] Test (Test.java:12) - error! 1001
2014-05-29 12:19:15,269 DEBUG method: [main] Test (Test.java:13) - debug! 1001
One common error when using Log4j2 is placing the log4j2.xml in a file that is not in the classpath.
To diagnose if that is the problem, change the line
logger.info("Opening serial port {}", serialPortName);
to
logger.error("Opening serial port {}", serialPortName);
If you see any output it is because log4j can't load your file. This is because the default log level when the file is not found is ERROR, not DEBUG.
The location of the log4j2.xml on my project (Maven) is in src/main/resources, which I know it is in my classpath.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With