Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Logging - obvious?  RSS feed

 
Ray Stojonic
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?
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
IntelliJ IDE Mac
  • 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 sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!