问题
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:
But nothing when I set things to logger.debug:
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/50216893/log4j2-not-outputting-debug-information