Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with LogRecord.needToInferCaller

807589Jan 14 2009 — edited Jan 16 2009
All,

I have a custom java.util.logging.Formatter subclass that I would like to have log out the sourceMethodName and sourceClassName from the LogRecord only when the values have been explicitly set by someone. In this effort, I seem to be running into a few dead ends, and I'm looking for suggestions. In case you weren't aware, if no one has set the sourceMethodName or sourceClassName, LogRecord will lazily try to infer this information when you request it by creating a Throwable and then parsing out information from the stack trace. I don't want to incur this overhead, and I don't want to log out the information unless the person doing the logging really wanted this info included. The first dead end that I have encountered in working around this issue is that LogRecord's needToInferCaller is private, and there is no public or even protected method that permits access to this field. Further, the inferCaller method is also provide and, as such, cannot be overridden. My next thought was possibly creating my own LogRecord subclass and Logger subclass and having my Logger always return my special subclass of LogRecord that overrides the mutators and accessors for sourceMethodName and sourceClassName. This is doable, but Logger isn't organized in such a way that it is easy to override and return a custom subclass of LogRecord. Instead of having a single method on Logger called something like newLogRecord, Logger simply instantiates new LogRecords within many of its methods. This seems like an unfortunate oversight by the author of this class, but maybe I am missing something. In any event, I'm still looking for a clean solution to this. Any suggestions are welcome.

Regards,
Paul
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 13 2009
Added on Jan 14 2009
8 comments
124 views