0
votes

I'm trying to use log4j in my Java application (no web-app). Therefore I added the configuration file directly within my /src folder.

log4j.properties

log4j.rootLogger=DEBUG

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

My main class is in a package with a sub-namespace and within there I'm trying to log as follows:

final static Logger logger = Logger.getLogger(MainClass.class);
public static void main(String[] args) {
    //BasicConfigurator.configure();
    logger.warn("some warning");
}

But still I'm getting an error message and I don't know what to do. You see the commented line BasicConfigurator above. As far as I know, the log4j.properties should be read without calling that line, right?

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

1
What kind of build system are you using?schrieveslaach
No specific build system, just a plain simple java project within Eclipse. The dependencies within the build-path are set correctly. If I use that line above, then I see output on the console, but then the configuration is not read from the properties-file.Matthias
The property file should be placed in the root folder and not the src folder. That might be your problem. This thread might be of interest for you: stackoverflow.com/questions/7390521/…Karl Dahlgren
I'm assuming that your folder src is not added as resource folder to your classpath. Did you specify resources (see here)?schrieveslaach
If the file is in the src-folder, the file is copied to the /bin folder on execution. If I move the file to the root-folder, then it is not copied. The error is the same for both ways.Matthias

1 Answers

0
votes

According to documentation, file should be on classpath and named 'log4j2.properties'

You can also set directly the configuration by setting -Dlog4j.configurationFile="<your config filepath>"

Source: https://logging.apache.org/log4j/2.x/manual/configuration.html