You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Thorsten Schöning <ts...@am-soft.de> on 2010/10/15 13:53:03 UTC

get method name of logging caller...

Hello,

is there any way to get the method name of the logging caller in the
log message? In some places %M is mentioned, but it doesn't seem to
work and is not in the documentation of class PatternLayout. With
log4perl and log4j I'm able to log the whole class path down to a
method and wondered if this is possible using log4cxx, too. Seems one
has to stay with line number, right?

Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow


Re: get method name of logging caller...

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag Thorsten Schöning,
am Freitag, 15. Oktober 2010 um 12:53 schrieben Sie:

> is there any way to get the method name of the logging caller in the
> log message? In some places %M is mentioned, but it doesn't seem to
> work and is not in the documentation of class PatternLayout.

Hello,

%M is not mentioned in the docs of PatternLayout, but it's made
available already. I didn't get any suitable result because, as Curt
Arnold mentioned, my compiler wasn't detected by the macros defining
__LOG4CXX_FUNC__. I changed this in locatininfo.h to recognize the
Borland compiler and %M and %C seem to work as expacted.

I created two patches, one to add Borland compiler in locationinfo.h
and the other to update the documentation of PatternLayout to state
that C and M may be supported, depending on the compiler used. Maybe
you are interested to merge.

Index: locationinfo.h
===================================================================
--- locationinfo.h      (Revision 1657)
+++ locationinfo.h      (Arbeitskopie)
@@ -109,28 +109,32 @@
 
         /** Caller's method name. */
         const char * methodName;
-        
 
+
       };
   }
 }
 
-  #if !defined(LOG4CXX_LOCATION)
-#if defined(_MSC_VER)
-#if _MSC_VER >= 1300
+#if !defined(LOG4CXX_LOCATION)
+  #if defined(_MSC_VER)
+    #if _MSC_VER >= 1300
       #define __LOG4CXX_FUNC__ __FUNCSIG__
-#endif
-#else
-#if defined(__GNUC__)
+    #endif
+  #else
+    #if defined(__GNUC__)
       #define __LOG4CXX_FUNC__ __PRETTY_FUNCTION__
+    #else
+      #if defined(__BORLANDC__)
+        #define __LOG4CXX_FUNC__ __FUNC__
+      #endif
+    #endif
 #endif
-#endif
 #if !defined(__LOG4CXX_FUNC__)
-#define __LOG4CXX_FUNC__ ""
+  #define __LOG4CXX_FUNC__ ""
 #endif
-      #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__, \
-           __LOG4CXX_FUNC__,                                                         \
-           __LINE__)
-  #endif
+#define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__,         \
+                                                      __LOG4CXX_FUNC__, \
+                                                      __LINE__)
+#endif
 
 #endif //_LOG4CXX_SPI_LOCATION_LOCATIONINFO_H


Index: patternlayout.h
===================================================================
--- patternlayout.h     (Revision 1657)
+++ patternlayout.h     (Arbeitskopie)
@@ -104,6 +104,29 @@
         </td>
         </tr>
 
+        <tr>
+          <td align="center">
+            <b>C</b>
+            <p><b>class</b></p>
+          </td>
+
+          <td>
+            Used to output the class of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            and can therefore be retrieved form the location info as needed.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
    <tr> <td align=center><b>d</b></td> <td>Used to output the date of
          the logging event. The date conversion specifier may be
          followed by a set of braces containing a
@@ -148,6 +171,30 @@
         </tr>
 
         <tr>
+          <td align="center">
+            <b>M</b>
+            <p><b>method</b></p>
+          </td>
+
+          <td>
+            Used to output the method of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            which is ignored in every attempt to retrieve the method from the
+            location info.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
+        <tr>
         <td align=center><b>n</b></td>
 
         <td>Outputs the platform dependent line separator character or


Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow

Re: get method name of logging caller...

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag Thorsten Schöning,
am Freitag, 15. Oktober 2010 um 12:53 schrieben Sie:

> is there any way to get the method name of the logging caller in the
> log message? In some places %M is mentioned, but it doesn't seem to
> work and is not in the documentation of class PatternLayout.

Hello,

%M is not mentioned in the docs of PatternLayout, but it's made
available already. I didn't get any suitable result because, as Curt
Arnold mentioned, my compiler wasn't detected by the macros defining
__LOG4CXX_FUNC__. I changed this in locatininfo.h to recognize the
Borland compiler and %M and %C seem to work as expacted.

I created two patches, one to add Borland compiler in locationinfo.h
and the other to update the documentation of PatternLayout to state
that C and M may be supported, depending on the compiler used. Maybe
you are interested to merge.

Index: locationinfo.h
===================================================================
--- locationinfo.h      (Revision 1657)
+++ locationinfo.h      (Arbeitskopie)
@@ -109,28 +109,32 @@
 
         /** Caller's method name. */
         const char * methodName;
-        
 
+
       };
   }
 }
 
-  #if !defined(LOG4CXX_LOCATION)
-#if defined(_MSC_VER)
-#if _MSC_VER >= 1300
+#if !defined(LOG4CXX_LOCATION)
+  #if defined(_MSC_VER)
+    #if _MSC_VER >= 1300
       #define __LOG4CXX_FUNC__ __FUNCSIG__
-#endif
-#else
-#if defined(__GNUC__)
+    #endif
+  #else
+    #if defined(__GNUC__)
       #define __LOG4CXX_FUNC__ __PRETTY_FUNCTION__
+    #else
+      #if defined(__BORLANDC__)
+        #define __LOG4CXX_FUNC__ __FUNC__
+      #endif
+    #endif
 #endif
-#endif
 #if !defined(__LOG4CXX_FUNC__)
-#define __LOG4CXX_FUNC__ ""
+  #define __LOG4CXX_FUNC__ ""
 #endif
-      #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__, \
-           __LOG4CXX_FUNC__,                                                         \
-           __LINE__)
-  #endif
+#define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__,         \
+                                                      __LOG4CXX_FUNC__, \
+                                                      __LINE__)
+#endif
 
 #endif //_LOG4CXX_SPI_LOCATION_LOCATIONINFO_H


Index: patternlayout.h
===================================================================
--- patternlayout.h     (Revision 1657)
+++ patternlayout.h     (Arbeitskopie)
@@ -104,6 +104,29 @@
         </td>
         </tr>
 
+        <tr>
+          <td align="center">
+            <b>C</b>
+            <p><b>class</b></p>
+          </td>
+
+          <td>
+            Used to output the class of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            and can therefore be retrieved form the location info as needed.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
    <tr> <td align=center><b>d</b></td> <td>Used to output the date of
          the logging event. The date conversion specifier may be
          followed by a set of braces containing a
@@ -148,6 +171,30 @@
         </tr>
 
         <tr>
+          <td align="center">
+            <b>M</b>
+            <p><b>method</b></p>
+          </td>
+
+          <td>
+            Used to output the method of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            which is ignored in every attempt to retrieve the method from the
+            location info.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
+        <tr>
         <td align=center><b>n</b></td>
 
         <td>Outputs the platform dependent line separator character or


Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow

Re: get method name of logging caller...

Posted by Thorsten Schöning <ts...@am-soft.de>.
Guten Tag Curt Arnold,
am Montag, 18. Oktober 2010 um 15:14 schrieben Sie:

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

Thanks for the hint, but even if I do that, I just get what %l would
print, right? It seemed that it combines file and line, but I would
have wanted just the method. LocationInfo::write doesn't seem to print
the method name, either it's provided or not, too. I'm stuck with an old
Borland Builder and pretty sure I already searched for a way to get
the method name quite a while ago and didn't find anything helpful.

Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow


Re: get method name of logging caller...

Posted by Curt Arnold <ca...@apache.org>.
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


Re: get method name of logging caller...

Posted by "Jacob L. Anawalt" <ja...@geckosoftware.com>.
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