Jonathon Gore Jonathon Gore -4 years ago 97
Java Question

log4j.xml file on classpath but cannot be detected by log4j

I am trying to set up log4j in a simple Java project. I keep getting the following message when running my project.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.


However I have added log4j.xml to my classpath. For example am I doing this in my main method:

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

import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLClassLoader;

public class main {

private static Logger logger = LogManager.getLogger("Test");

static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}

public static void main(String [] args) {
InputStream cl = ClassLoader.getSystemClassLoader().getResourceAsStream("log4j.xml");
System.out.println(convertStreamToString(cl));
}
}


When I execute this code I get the contents of my log4j.xml file, which would make it seem the the file is being loaded onto my class path properly?

Here is the content of my xml 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>
<Logger name="com.foo.Bar" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>


Any ideas on what I am doing wrong?

Answer Source

Change the file name of log4j.xml to log4j2.xml.

I hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download