logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: get method name of logging caller...
Date Mon, 18 Oct 2010 13:14:12 GMT
If you are using the LOG4CXX_DEBUG and similar macros and some of the more popular compilers,
then you hopefully will get class and method information.  If you transliterated java code
and are just doing "logger.debug("Msg"), then that information is not available to you.

The performance issue mentioned in log4j isn't applicable to log4cxx.  In earlier version
of log4j obtaining that information meant creating an exception, printing its stack trace
to a memory stream and then parsing that stream to get the location.  With the LOG4CXX_DEBUG
macros, that information is added to the generated logging call at compile time.

If you are able to see file and line, but not class and method, then explore extending __LOG4CXX_FUNC__
to support your compiler.

On Oct 15, 2010, at 4:08 PM, Jacob L. Anawalt wrote:

> On 10/15/2010 5:53 AM, Thorsten Schöning wrote:
>> Hello,
>> is there any way to get the method name of the logging caller in the
>> log message?
> As you say, the log4j docs document %M in the PatternLayout (while log4cxx does not)
but caution against the inefficiency of it's use. Additionally "The Complete Log4J Manual"
(a good document to have) points out that even in Java this information can be discarded by
the compiler.
> In log4cxx %F and %L provide the file name and line number respectively, but that is
because they can rely on the ubiquitious __FILE__ and __LINE__ macros. The LOG4CXX_LOCATION
macro is documented to use __LOG4CXX_FUNC__ with a value of "".
> If your compiler and flags support __FUNCTION__, you could work that into your message
or change the definition of __LOG4CXX_FUNC__ and go on to implement %M.
> I haven't even glanced at the code to see where that's at. I just inflexibly typed the
function name into a DEBUG message near the top of the message call, or sometimes used a NDC
to track the call stack in debug logs.
> Good luck,
> -- 
> Jacob Anawalt
> Gecko Software, Inc.
> janawalt@geckosoftware.com
> 435-752-8026

View raw message