Log4j2 not logging to console
Asked Answered
N

2

9

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);
    }
}
Neglectful answered 29/5, 2014 at 18:25 Comment(2)
you should specify class which you want to log, LogFactory.getLogger(ABCHandler.class)Bernard
It'll default to the current class. See ths Log4j 2 API for more details.Neglectful
B
19

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.

Belligerence answered 29/5, 2014 at 19:25 Comment(4)
"One common error when using Log4j2 is placing the log4j2.xml in a file that is not in the classpath." That was it...Neglectful
Always forgetting about thatFolger
I am facing the same issue where its not printing anything with log.info but log.error gets printed. I added my project to classpath and my log4j2.xml is inside my classpath dir. Am i missing something here?April
Smh... it was sitting in the base project directory. Great write up and example!Otis
A
1

My problems were:

  1. I was using a log4j.properties file. Once renamed to log4j2.properties the main and test classpaths picked it up

  2. I had to add logging to standard output to build.gradle

    test {
       testLogging.showStandardStreams = true
    }
    

note: when I added sourcesets according to here it still didn't find the log4j.properties file only renaming it worked!

Analogous answered 28/7, 2021 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.