How to print the method name using log4j

Discussions

General J2EE: How to print the method name using log4j

  1. How to print the method name using log4j (3 messages)

    Dear Driends,

    I would like to know how to print the method name being invoked using the log4j API. The conversion pattern %L displays the file name only and not the method.

    Please give the solution for this.

    Sachin
  2. There is no general solution for this. Java provides no simple way to determine the calling method. The best way to do this is:

    a) Pass in the method name as part of the logged method.

    b) Print out an exception stacktrace, which will include the method name.
  3. Use the M pattern as indicated in the log4j manual.

    http://logging.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html
  4. Thanks friend.

    As you indicated, the %M pattern as indicated in the log4j manual works out.

    Thanks for your help.

    The point that i observed is that,

    %m -message
    %M - method name
    %c- category
    %C- Class name