log4j.properties, where to put it

16,490

Solution 1

maybe the file log4j.properties is being read correctly and the problem is with the configuration in log4j.properties file. Does adding this line to the begining of log4j.properties make any difference (assuming that you are have some logging statements in your class files)

log4j.rootLogger=DEBUG, dest1

Solution 2

This is mostly a question of convention and/or personal preference. What I am accustomed to is to create another source directory (e.g. config or resources) in the project root (next to src and test), then place the log4j.properties there. This can by done by you right-clicking on the newly created folder and choosing Build Path -> Use as Source Folder. Optionally you can specify some inclusion/exclusion patterns too.

Log4j looks for the properties file on the root of the classpath by default. With the above setup the properties file gets copied to the output directory and it will be on the root of your classpath during development.

Later during the deployment the log4j.properties would get bundled in the jar/war file too. You might want to override the bundled properties by specifying an alternative config folder on the classpath during runtime. (see this other question and the docs about the order entries take precedence on the classpath)

Solution 3

You need to put the log4j.properties file on the application classpath.

Share:
16,490
v4r
Author by

v4r

Updated on June 15, 2022

Comments

  • v4r
    v4r almost 2 years

    According to log4j manual, I should put log4j.properties to the src folder. I copied this file to all the possible places I think it will affect log4j. However, this does not work.

    TestEM class contains many unit test functions (I use testng). I run one of these test functions which references a class in the feedback.strategy package.

    My eclipse project

    Here is the content of the log4j.properties file:

    #log for class1
    log4j.category.Demo1=DEBUG, dest1
    log4j.appender.dest1=org.apache.log4j.FileAppender
    log4j.appender.dest1.File=C:/Users/Asus/workspace/FeedbackProcess/logs/class1.log
    log4j.appender.dest1.layout = org.apache.log4j.PatternLayout
    log4j.appender.dest1.layout.ConversionPattern= %d %p [%t] (%c) \u2013 %m%n