1

I've installed log4j2 and set up a properties file, yet I can't get log4j2 output debug information to the console. I'm suspecting my configuration is wrong.

Here's my source code:

package com.smt.trimble.poc;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class NMEAReader {

      private static final String hostName = "192.168.0.2";
      private static final int portNumber = 5017;
      private static Socket nmeaSocket;

      private static final Logger logger = LogManager.getLogger(NMEAReader.class);


      public static void main(String[] args) {

        try {

        nmeaSocket = new Socket(hostName, portNumber);
        logger.debug("Creating socket");
        PrintWriter out = new PrintWriter(nmeaSocket.getOutputStream(), true);
        BufferedReader in = 
          new BufferedReader(new InputStreamReader(nmeaSocket.getInputStream()));

        String userInput;

        while (true) {
            logger.debug("Reading Data");
            userInput = in.readLine();
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
        }


    } catch (Exception e){
        System.out.println("An error occured, " + e.getMessage());
    }
}

}

And here's my log4j2 configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <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="Debug">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>

Log4J2 works, as I'm able to see output when I set logger.debug to logger.error.

I'm probably missing something trivial.

edit:

I've updated my properties file to

<?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="trace">
      <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-level %logger{36} - %msg%n"/>
          <Filters>
            <ThresholdFilter level="debug"/>
          </Filters>
        </Console>
      </Appenders>
      <Loggers>
        <Root level="Debug">
          <AppenderRef ref="Console"/>
        </Root>
      </Loggers>
  </Configuration>

Changing Configuration level to trace also didn't produce any further output. Still, when I change logger.debug to logger.error, I can see log4j2 output:

enter image description here

But nothing when I set things to logger.debug:

enter image description here

Fang
  • 2,199
  • 4
  • 23
  • 44
  • Try adding threshold filter in your appender like [this](https://stackoverflow.com/a/26691671/738746). – Bhesh Gurung May 07 '18 at 15:02
  • @BheshGurung Sadly, that didn't do the trick. – Fang May 07 '18 at 15:07
  • Please update the post with updated configuration. – Bhesh Gurung May 07 '18 at 15:20
  • 1
    Also, try changing configuration `status` to `trace` instead of `warn` (``). That way, you can see how log4j is being configured. – Bhesh Gurung May 07 '18 at 15:26
  • Logging level names are case sensitive according to some docs... have you tried `` ? – Roddy of the Frozen Peas May 09 '18 at 16:05
  • can you please run you application with `-Dlog4j2.debug` parameter and check the following line `DEBUG StatusLogger createLogger(additivity="null", level="DEBUG", includeLocation="null", ={Console}, ={}, Configuration(log4j2-test.xml), Filter=null)` – Anton Balaniuc May 09 '18 at 16:16
  • Are you sure that you don't have any custom settings for eclipse console? Can you please execute you app not from the IDE and check if you still can't see anything in the console? – Anton Balaniuc May 09 '18 at 16:22
  • @RoddyoftheFrozenPeas: I'm fairly sure that I have tried all combinations of case sensivity, but will do so again as soon as I get back to work. – Fang May 09 '18 at 16:26

2 Answers2

6

It seems your log4j2 configuration file is not getting used. If there is no log4j2 configuration, By default, Root Logger prints ERROR level logs and that is what happening in your case.

Your log4j2 configuration file must be placed in direct classpath of the application and name of the file must be log4j2.xml. If it is a maven project, your file should be placed at /src/main/resources/log42.xml

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • This sort of brought me on the right track. My xml configuration file was named log4j2-test.properties, which, according to https://logging.apache.org/log4j/2.x/manual/configuration.html is legal. Renaming to log4j2.xml did the trick. – Fang May 11 '18 at 07:20
0

Inside your Loggers tag you should add a Logger with level="trace". Your code inside the Loggers tag will look like this

    <Loggers>
        <Root level="Debug">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.smt.trimble.poc" level="trace"></Logger>
    </Loggers>

And also I prefer Root level="info". It does the job.

sam
  • 1,800
  • 1
  • 25
  • 47