0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • What kind of build system are you using? – schrieveslaach Jan 04 '16 at 10:31
  • 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 Jan 04 '16 at 10:35
  • 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: https://stackoverflow.com/questions/7390521/change-location-of-log4j-properties – Karl Dahlgren Jan 04 '16 at 10:36
  • I'm assuming that your folder src is not added as resource folder to your classpath. Did you specify resources (see [here](https://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse))? – schrieveslaach Jan 04 '16 at 10:37
  • 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 Jan 04 '16 at 10:40
  • I had to change the first line in the file to: log4j.rootLogger=DEBUG, stdout – Matthias Jan 04 '16 at 12:18

1 Answers1

0

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

Prim
  • 2,880
  • 2
  • 15
  • 29
  • root path of the project or within /src folder? – Matthias Jan 04 '16 at 10:46
  • must be on classpath during execution, but see the expected filename, you missed a character ;) – Prim Jan 04 '16 at 10:48
  • Okay, my file name was wrong, but the error remains. Should Log4j read the file even without the path argument? Or do I really need that argument? – Matthias Jan 04 '16 at 10:51
  • Log4j reads the file if it is correctly named and on classpath. Generally in a IDE, all files under a directory flagged as resource directory is put on classpath during execution. Setting property 'log4j.configurationFile' manually removes all automatic detection and puts the file you set – Prim Jan 04 '16 at 10:55