Doug Hauf Doug Hauf - 2 months ago 10
Java Question

Why am i receiving the errors that I am getting?

I have this code below for a Log4j logger. I have to say that a lot of people have given me help on this on a different question. But I thought that since it was getting to a new question I would open one up.

My question is why am I getting these errors? Do I have everything configured correctly? How do I add an appender. I think that I am missing something somewhere but i am not certain as to what it is.

Code:

package TestMenu;

import org.apache.log4j.Logger;
import javax.swing.*;

public class TestLogs {
private static Logger logger = Logger.getLogger(TestLogs.class.getName());

public TestLogs() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
}

private void LogTester() {
logger.info("It works");
}

public static void main(String[] args) {
TestLogs tls = new TestLogs();
tls.LogTester();
}
}


Error:

log4j:WARN No appenders could be found for logger (TestMenu.TestLogs).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


Properties File:

# Root logger
log4j.rootLogger=INFO, file, stdout

# Write to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File="C:\Users\itpr13266\Desktop\test.log"
log4j.appender.file.MaxFileSize=50MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Write to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


enter image description here

xav xav
Answer

Log4j doesn't seem to find your configuration file. Have you tried running your application with a parameter like:

-Dlog4j.configuration=file:config/log4j.properties

So that Log4j finds your configuration file ("config/log4j.properties" using System Property log4j.configuration):

enter image description here

Alternatively, you can also put this configuration file in your classpath so that Log4j finds it, like in your source directory for example:

Log4j config file location

Else, here is what Log4j documentation explains or this error message:

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments. Also see FAQ: Why can't log4j find my properties in a J2EE or WAR application?

Comments