Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Print out the label of the current method

Thread view: 
Oliver Hirschi - 19 Dec 2007 09:22 GMT
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,
Signature

Oliver Hirschi
http://www.FamilyHirschi.ch

Chronic Philharmonic - 19 Dec 2007 09:34 GMT
> Hi
>
[quoted text clipped - 5 lines]
> 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
Joshua Cranmer - 19 Dec 2007 22:11 GMT
> Hi
>
[quoted text clipped - 7 lines]
>
> 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.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.