Print out the label of the current method

O

Oliver Hirschi

Hi

I want to print out (System.out.println) the method label of the
method, where I make the print out. With "this.getClass()" I get the
label of the class, but how can I get the name of the current method?
Is there a way to do that?

Background information: I need it to print error and log messages to a
log file. So I want to see in the logfile where the printout is made...

Thanks & regards,
 
C

Chronic Philharmonic

Oliver Hirschi said:
Hi

I want to print out (System.out.println) the method label of the
method, where I make the print out. With "this.getClass()" I get the
label of the class, but how can I get the name of the current method?
Is there a way to do that?

Background information: I need it to print error and log messages to a
log file. So I want to see in the logfile where the printout is made...

The java.util.logging package does most of this for you. Without you having
to do anything other than log a message to the logger, the default
SimpleFormatter will add the time, the fully qualified class name and the
method name to your message.

see http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html
 
J

Joshua Cranmer

Oliver said:
Hi

I want to print out (System.out.println) the method label of the
method, where I make the print out. With "this.getClass()" I get the
label of the class, but how can I get the name of the current method?
Is there a way to do that?

Background information: I need it to print error and log messages to a
log file. So I want to see in the logfile where the printout is made...

Thanks & regards,

The easiest way to do this is to hard-code the method name. If that is
impractical, then grabbing the stack trace and selecting the right
element will do the trick. Fortunately, the java.util.logging package is
smart enough to do this in most circumstances ("most" excluding JNI and
potentially some other corner cases). In fact, the java.util.logging
package is probably going to be more efficient for what you want to do
than writing it yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top