• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14414
  • Last Modified:

How to write log4j properties file to specify logging level hierarchically?

I would like to use a properties file to configure log4j.  I am wondering how I need to edit the log4j.properties file to accomplish these goals below:

1. Root logger is set to WARN logging level.  So the default logging for any class will always be WARN.
2. Any logging for classes in the hierarchy "com.mycompany.myproject" will have DEBUG logging level.  I don't want to specify every class explicitly in the properties file.
3. Both 1 and 2 are logging to the same appender.

Here is an example properties file that accomplishes #1 above, but not #2.  No logging output for #2 is captured other than WARN level and higher.

log4j.rootLogger=WARN,theappender
log4j.logger.com.thecompany.theproject=DEBUG,theappender

log4j.appender.theappender.layout=org.apache.log4j.PatternLayout
log4j.appender.theappender.layout.ConversionPattern=%d{ISO8601} %-5p - %m%n
log4j.appender.theappender=org.apache.log4j.RollingFileAppender
log4j.appender.theappender.File=/tmp/theappender.log
log4j.appender.theappender.MaxFileSize=500KB
log4j.appender.theappender.MaxBackupIndex=50
0
ErikH2000
Asked:
ErikH2000
  • 4
  • 4
  • 3
2 Solutions
 
CEHJCommented:
>>Here is an example properties file that accomplishes #1 above, but not #2.

For 2. you just need

log4j.logger.com.mycompany.myproject=DEBUG,theappender
0
 
CEHJCommented:
Oh and you need

log4j.additivity.logger.com.mycompany.myproject=false
0
 
objectsCommented:
> log4j.logger.com.thecompany.theproject=DEBUG,theappender

that logger  name doesn't match the name you mentioned above, maybe just a typo.


And read:  http://logging.apache.org/log4j/docs/manual.html

"Appender Additivity

    The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

    However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and it's ancestors upto and including P but not the appenders in any of the ancestors of P.

    Loggers have their additivity flag set to true by default. "
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ErikH2000Author Commented:
Sorry, for legal reasons, I was anonymizing names of my company and project.  So I wrote "myproject" and "mycompany".  Accidentally, I used "the" instead of "my", but unfortunately in the real log4j.properties file, the same error is not present.  So the advice to change "the" to "my" isn't really applicable.  I do apologize for the confusion, though.  The corrected log4j.properties example is found below.

log4j.rootLogger=WARN,theappender
log4j.logger.com.mycompany.myproject=DEBUG,theappender

log4j.appender.theappender.layout=org.apache.log4j.PatternLayout
log4j.appender.theappender.layout.ConversionPattern=%d{ISO8601} %-5p - %m%n
log4j.appender.theappender=org.apache.log4j.RollingFileAppender
log4j.appender.theappender.File=/tmp/theappender.log
log4j.appender.theappender.MaxFileSize=500KB
log4j.appender.theappender.MaxBackupIndex=50
0
 
ErikH2000Author Commented:
To cehj:

"log4j.additivity.logger.com.mycompany.myproject=false"

I made this change, but I did not begin seeing DEBUG messages in classes found at com.mycompany.myproject.  So this didn't change the results.

After reading about additivity, it seemed like I actually wanted that flag to be true, not false.  So I explicitly set it to true, with a line like so:

log4j.additivity.logger.com.mycompany.myproject=true



To objects:

It seems to me that I want additivity and that is the default.  I'm not setting additivity to false, so it should be true.  However, it appears that even with the additivityflag explicity set to true, the ancestors of com.mycompany.myproject will not have DEBUG logging.  I am only seeing WARN level messages for ancestors of com.mycompany.myproject.

-Erik
0
 
ErikH2000Author Commented:
My thanks to Cehj and Object who replied, but I still consider my problem unresolved.

-Erik
0
 
CEHJCommented:
This demonstrates what i said above

http://www.cehjohnson.btinternet.co.uk/misc/test-log.jar

I've included the source.

java -jar test-log.jar

(make sure the log4j classes are in your classpath)
0
 
objectsCommented:
check how you are creating your logger
0
 
objectsCommented:
ie. in the logger in your code that you ar4e logging debug messages to
0
 
ErikH2000Author Commented:
In an effort to make my problem simpler to understand, I left out a factor that turned out to be the cause of the problem.  The problem only occurs when I log from code in a SOAP object served by Apache Axis.  Logging works as expected with a standard Java application.

I don't think it's fair to keep the question open since the original posters spent a fair amount of effort troubleshooting from the limited context I gave.  I'm going to accept the solutions and split points equally between Objects and CEHJ.

For the record, I ended up writing a replacement class for Log4j's Logger to solve the problem.  It's not as good as Log4j, but I ran out of time on my project to mess with Axis/Log4j configuration issues.

-Erik
0
 
CEHJCommented:
:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now