[
https://issues.apache.org/jira/browse/LOGGING-137?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12900006#action_12900006
]
Doug Bateman commented on LOGGING-137:
--------------------------------------
It's not quite that simple, because the output of
Throwable.printStackTrace(stream) isn't standard. For one thing, it varies
based on the availability of line numbers. So it is very important that the
length of the header be calculated dynamically, at least once.
Doing that calculation in getStackHeaderCharCount() doesn't work, because that
method signature doesn't match getCallerJava11(int). So what it means is that
I've got to call "public static int getCallerJava11(depth)" itself to calculate
the stack size up front.... and that means I'm using that public function even
before the final field STACK_HEADER_CHAR_COUNT is even initialized. It's messy.
However, fortunately I think it's still workable. I can rely on the fact that
static-initializers are synchronized by the class loader, so I need not worry
about race conditions caused by having a public static method reading a static
field prior to initialization. And I can rely on the non-initialized field
having the default value zero prior to it's initialization. Otherwise it would
be a real nightmare.
Anyway, I've got to finish installing these updates. For the Java 1.4 version,
I take it you prefer Example CallStackUtil.java.2 where I simply assert
STACK_HEADER_FRAME_COUNT=2, (e.g. drop 2 frames and then read at depth) rather
than doing any dynamic calculations. Is that correct?
> LogFactory.getLog()
> -------------------
>
> Key: LOGGING-137
> URL: https://issues.apache.org/jira/browse/LOGGING-137
> Project: Commons Logging
> Issue Type: New Feature
> Affects Versions: 1.1.2
> Reporter: Doug Bateman
> Attachments: CallStackTestCase.java, CallStackUtil.java.0,
> CallStackUtil.java.1, CallStackUtil.java.2, LogFactory.java
>
> Original Estimate: 0.5h
> Remaining Estimate: 0.5h
>
> Presently, in Apache Commons, the most common way to get a logger is to do
> something like:
> public class MyClass {
> private static Log log = LogFactory.getLog(MyClass.class);
> }
> Notice how MyClass.class (or alternatively a string name) is passed as a
> parameter. The annoying aspect of this is that sometimes the class name
> doesn't get updated when doing copy/paste operations. A desirable
> alternative might be:
> public class MyClass {
> private static Log log = LogFactory.getLog(); //class name inferred from
> call stack
> }
> With such an approach there are two possible concerns I can foresee:
> * Call stack inspection isn't terribly fast. However since Loggers are
> generally initialized only once, when the class is first loaded, performance
> isn't likely to be a major problem.
> * Commons-logging is Java 1.1 compatible. Thus care must be taken to
> ensure compatibility isn't broken.
> * Commons-logging doesn't depend on commons-lang, and thus the utilities
> in commons-lang cannot be used.
> In Java 1.4, the call stack is easily obtained using Thread.getCallStack().
> Prior to Java 1.4, the only way to obtain the call stack is to inspect the
> stack trace of an exception.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.