You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Simon Kitching <sk...@apache.org> on 2005/04/01 15:31:08 UTC
Re: commons logging class level logging
Trenton D. Adams wrote:
> What's the quickest and easiest way of my code knowing what class called
> a method? The reason I ask is because we have some wrapper methods in
> our main class that every other class calls to do logging. I would like
> that method to be able to determine what class it was that called, so
> that I could use the proper logger/category.
I don´t believe it is possible, except by requiring the calling method
to pass the associated information as a parameter.
Java 1.5 does provide an API to get a stack trace. Earlier versions of
java provides Throwable.printStackTrace method that can be parsed to
extract the calling method [WARNING: stack trace format can vary by
platform and JVM vendor!]. However there is no guarantee that this
information is actually available at runtime; optimising JVMs can do all
sorts of things that mean that stacktrace information is incomplete or
simply not available; inlining method calls is just one case.
You will see that log4j appenders have %C and %M (I think that´s the
right chars) to insert class and method info into log messages. But I
know from experience that they don´t always work.
If you don´t want to require the source code to explicitly include the
caller information as a parameter, then maybe you can use an
"aspect-oriented" tool (eg aspectJ) to automatically insert the
necessary parameter.
>
> Also, for log4j, is Logger.getLogger("classname") an efficient way of
> getting a logger for each class? e.g. should I be calling this method
> every time my *wrapper* log method is called? How does the commons
> logging do this properly, surely it has to do something similar since
> it's a wrapper too?
commons-logging keeps a hashmap of all the Log objects, keyed by the
category name. Calling getLogger("foo") just does a hashmap lookup to
find and return the appropriate Log object.
And I´m pretty sure log4j internally does the same thing; it just has a
hashmap of all the Logger objects that already exist.
Cheers,
Simon
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org