This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing 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

Logging - obvious?

 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to decouple my business logic from my servlets, but have come up with the following concern.

When something goes wrong in my pojo, I'll want to log the problem so I can track down what happened.

It seems exceedingly difficult to get the application's log and write to it from a pojo, so I should just declare that my pojo methods throw any caught exceptions that might occur and handle them in the calling servlet?
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray,

Why arent you using java.util.logging or log4j to log from POJOs?
 
Marshal
Posts: 67293
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One point: logging and exceptions are different animals. Exceptional conditions should throw exceptions that propogate out to the container where you can use declarations in web.xml to specify how they are to be handled. Logging is best handled with one of the packages Sonny mentioned.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sonny Gill:
Why arent you using java.util.logging or log4j to log from POJOs?



I am, the log4j that's setup in my context.xml logger tag. Maybe I overlooked how to get ahold of the log from a pojo (I got a log, but not the one for the context), but even if I did, it seemed to be a lot of work (as opposed to the easy access from a servlet)

What I'm after here is the ability to create a record of something that goes wrong. Let's face it, the user will almost always call and say "the program didn't work right...no, no, I don't recall what it said...no, I don't recall what I did before that...um, I don't know...can you fix it?"

As far as propogating out to handling with the web.xml, I'm not familiar with the technique, even if I was, I'd like to log the event and send it on it's merry way and/or give the user a "something bad happened, please call support" message.

:\

Anyway, I'm not talking about suppressing RuntimeExceptions, I guess it's more of a design question. ie: should I pass a reference to the log I want to use to my database handling pojo so it can log problems and return error conditions, or should the database handler throw exceptions that calling servlets log?
 
Sonny Gill
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray,

Have a look at the documentation for log4j or logging packages.
You create a logger in your class, and then use it to log the error or trace messages.

A typical usage would be to have an instance or class variable like -

private final (static) Logger myLogger = Logger.getLogger(getClass().getName());

and then use that Logger object in your methods.

You also need to set up the 'level' of logging for a heirarchy of loggers somewhere, in JDK1.4 logging, it is in logging.properties file in your JRE_HOME.

A very important requirement for logging is that you should be able to specify the logging level at runtime, for example log only error messages, log debug messages or log all messages etc. The little extra work needed to setup logging using java.util.logging or log4j is justified by satisfying this requirement alone.

cheers
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than 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!