Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Logging within the Shared Library jars

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have some webapps that call back to our own library jars we created.  We can log out of the wars using log4j, but the jars don't create log files.  Is it possible to have logging from these class files?
Posts: 20828
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not only done all the time, it's done in systems where the different JAR authors have different preferences in logging systems.

The logger is obtained via a static method. For example:

Static methods and pbjects are global to the classpath, so it doesn't matter whether this code is in the main WAR code or in an included JAR in WEB-INF/lib. All that's required is for you to code logging statements using that logger (or whatever logger applies) and to define one or more log appenders to output those log messages.

What's trickier is when your code uses log4j, but you want to also output log messages from third-party JARs that logged using apache commons logging or java.util.logging (JULI). In cases like that, you would have to include a log aggregator fa├žade such as slf4j that acts as a common backend for all the other loggers and routes everything to your logger of choice.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!