2

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
thd
  • 2,380
  • 1
  • 25
  • 33
  • 1
    place it in the application class path – Ezhil V Mar 20 '13 at 13:47
  • This is a [duplicate](http://stackoverflow.com/questions/5081316/where-is-the-correct-location-to-put-log4j-properties-in-an-eclipse-project). – Daniel Dinnyes Mar 20 '13 at 14:27
  • @GJ13: your comment is correct. The problem is that there is no such class as Demo1 as in log4j.category.Demo1 in my project. When I change it to a specific class in my project, it works. Your solution works also. So please make it an answer so I can mark it as correct. – thd Mar 20 '13 at 23:12
  • @DuongThang You don't have to have a `Demo1` class in your project. Maybe it is included in one of the dependencies (e.g. the log4j jar). Now if that `Demo1` class uses SLF4J logging inside, then log4j will be plugged to handle the logging in accordance with the configuration. – Daniel Dinnyes Mar 21 '13 at 11:12
  • @DuongThang Glad to know that it worked. Like you suggested, I have made my comment an answer. – GJ13 Mar 28 '13 at 13:38

4 Answers4

6

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)

Community
  • 1
  • 1
Daniel Dinnyes
  • 4,898
  • 3
  • 32
  • 44
4

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

Stijn Haus
  • 481
  • 3
  • 18
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
GJ13
  • 488
  • 3
  • 11
0

Place log4j.properties in WEB-INF\classes of the project .

Put log4j-xx.jar under WEB-INF\lib see here for details

Community
  • 1
  • 1
PSR
  • 39,804
  • 41
  • 111
  • 151