Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

commons.logging and log4j.properties

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found there is some problem when I use commons.logging in java code, like


Then in log4j.properties


I even have a commons-logging.properties:



Still, it doesn't write to the log file, it creates the file but can't write anything to it.

However, if I simply change my java code into



it immediately works and writes to the file.  WHy ? and how can I resolve the issue without changing my java code ?
 
Sheriff
Posts: 6378
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get commons-logging log4j working on my test project without configuring more than log4j.  All I can offer is some "gotta ask" dumb questions.

* I noticed that you have log4j.rootCategory=INFO, file in your log4j configuration, but you have log.debug() in your code.
* Have you tried it without any commons-logging configuration?
 
Hilda Stone
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I was able to get commons-logging log4j working on my test project without configuring more than log4j.  All I can offer is some "gotta ask" dumb questions.

* I noticed that you have log4j.rootCategory=INFO, file in your log4j configuration, but you have log.debug() in your code.
* Have you tried it without any commons-logging configuration?



Thanks for pointing the INFO thing, but actually it wasn't the reason since I was just giving an example, even when I used DEBUG it was same.  I also found when my java code uses SLFJ it also works.  Only when my java code uses common.logging it somehow can't write to the file using the log4j.properties.
 
Knute Snortum
Sheriff
Posts: 6378
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it were me, I'd remove all commons.logging configuration and remove the commons.logging jar, then download it again.
 
Saloon Keeper
Posts: 21268
138
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The commons.logging and log4j loggers are two entirely different logging systems, with different APIs and different implementations. And different config files.

The SL4J component provides a "Simple Logging Facade for Java", allowing applications using different loggers to consolidate their output and channel it through a single logging system. It's a very useful facility when you have lots of third-party libraries, since I've seen 3, possibly even 4 different loggers being used in a project because each library had its own JAR preferences.

Actually, doing a quick review of the SLF4J project, it appears that it also provides a common logging API itself. But regardless, the important thing is that whichever logging system an application subsystem uses, SLF4J can ensure that all of the logging can be directed to a single common logger and controlled from that logger's configuration file. If the base logger for your project is log4j, it would be the log4j.properties or log4j.xml file. If it's commons.logging, it would be that logger's config.
 
Knute Snortum
Sheriff
Posts: 6378
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that commons.logging is a wrapper for underlying logging systems.  I was able to get a project to use commons.logging simply by putting the commons jar in the classpath.  It auto-configured to use Log4J without any configuration files of its own.
 
Tim Holloway
Saloon Keeper
Posts: 21268
138
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:My understanding is that commons.logging is a wrapper for underlying logging systems.  I was able to get a project to use commons.logging simply by putting the commons jar in the classpath.  It auto-configured to use Log4J without any configuration files of its own.



commons logging has been a longstanding point of confusion for me. The impression I got was that there were two entirely different commons logging systems, plus a rename from jakarta commons logging to apache commons logging  

I have definitely had to use slf4j in projects that also had modules using commons logging. I may be wrong, but I think that the difference is that commons logging mandates using itself as a portable front-end with an option to choose an arbitrary back-end, whereas slf4j can also accept logging from many different front-ends at the same time. In fact, until I read the description more closely today, I didn't even reallize that slf4j jhad a front-end of its own - my usage has always been as a director to a backend.

I don't guarantee I'm not missing something, and on top of that, what I knew back when I last had to wrestle this stuff may have been superseded, but my experience at least was that if you wanted to mix stuff from different logging front-ends, you needed slf4j. I actually first encountered slf4j as a dependency when libraries I was incorporating into a complex project demanded it.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!