• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging events with java.util.logging.Logger

 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've just re-read the API for java.util.logging.Logger and it indicates that using its simple methods ( info(String), severe(String), etc ) does not guarantee that class name and method name from which the event is logged will appear in the event message. The following is the extract from API:


* For the methods that do not take an explicit source name and
* method name, the Logging framework will make a "best effort"
* to determine which class and method called into the logging method.
* However, it is important to realize that this automatically inferred
* information may only be approximate (or may even be quite wrong!).
* Virtual machines are allowed to do extensive optimizations when
* JITing and may entirely remove stack frames, making it impossible
* to reliably locate the calling class and method.


So, I've decided to use Logger.logp methods, which allow to explicitly log class and method names. Do you think it's an overkill?


Thanks a lot for your time and help on this issue.

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

I think this may be overkill for what is only a potential problem.

An alternate way of looking at the problem is that your log message may appear to have come from an earlier stack frame (and I would suspect that unless you have some extremely trivial methods that only get called from one location, then it is unlikely that the JIT compiler would optimize out more than one stack frame). So the class details logged should still be pretty close to the real log message - if somebody was trying to work backwards from the log, they should be able to work our where the log message would have come from.

If your log messages are meaningful, you should be able to search for them fairly easily, so this should also not be a problem.

Regards, Andrew
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Andrew (i'm sorry for the late reply)

I'll be using simple logging methods such as info(String) , severe(String), etc plus log methods.

There are two more things that I'd like to get your opinion on.

1. LogManager uses standard config file ( logging.properties ) that is located in jdkHome/jre/lib directory IF no config file is associated with java.util.logging.config.file system property. As far as I understand we are prohibited of using any config file apart from suncertify.properties and therefore are forced to use standard logging configuration (which logs events of INFO level and higher into the console ). Is it how you understand it?

2. When constructing an event I use StringBuffer rather than String plus if the event is of FINE or lower level I perform the check before constructing and logging an event ( to save on cpu/memory usage, otherwise the event construction would be in vain):



What do you think about this technique?


Thanks a lot for your time and help on these issues.

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex
1. LogManager uses standard config file ( logging.properties ) that is located in jdkHome/jre/lib directory IF no config file is associated with java.util.logging.config.file system property. As far as I understand we are prohibited of using any config file apart from suncertify.properties [...]
Fine so far.

we are [...] forced to use standard logging configuration (which logs events of INFO level and higher into the console ).
There is nothing stopping you from programattically setting the logging level such that less information is logged. You could even write a small routine that checks whether a particular environmental variable has been set, and set your logging level accordingly (just because you have this extra functionality, the assessor does not have to use it - so you should be OK with the rule forbidding requiring extra command line parameters).

2. When constructing an event I use StringBuffer rather than String plus if the event is of FINE or lower level I perform the check before constructing and logging an event ( to save on cpu/memory usage, otherwise the event construction would be in vain):
Looks good. If you are using JDK 5 you might want to look at the StringBuilder class which will do the same thing, but is not internally sycnhronized.

Regards, Andrew
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,

StringBuilder tip is excellent as I was not aware of this new class at all. Thanks a lot again for your very useful answers.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic