Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method name 2

Status
Not open for further replies.

Mausolo

Programmer
Jul 2, 2002
16
0
0
ES
Can i get the name of current method in java program?


Thanks
 
With JDK1.4

public class MyTest {
public static void main(String args[]) {
new MyTest().doit();
}
public void doit() {
System.out.println(new Exception().getStackTrace()[0].getMethodName());
}
}
| Real Gagnon from Quebec, Canada
+ LookingForJavaOrPowerBuilderSnippets? VisitReal'sHow-to
|
 
Hey realgagnon, thanks!

I have a Logger class that logs application events and exceptions, and I currently have to use a static string for each method name to pass to it. Now I can eliminate those and the maintenance that goes with them.

And thanks to Mausolo for asking...
 
there is a java logging API in jdk 1.4

it does this for you dude - dont bother with a quick fix. head in book all day breeds good results
 
I know that I can replace my home-built Logger class with the new 1.4 standard, but...

I looked at the API for the Logger class in 1.4 and it does log the method name, but as far as I can tell it's just a String that I have to provide to it. I doesn't seem to "know" the method name automatically.

Is there more to it?
 
i havent looked at it to be honest.

if you are trying to record every time a method is invoked, why not invoke the logging api from the class that is being invoked. therefore you always know the name.

you could say:

if (logging)
myLogger(**log something**)

obviously this is at a very high level! but it does avoid the mess that you will be creating by invoking calls to the runtime - dont bother with a quick fix. head in book all day breeds good results
 
That's essentially what I do, only the logging boolean is a static property of the Logger class so I can turn it on and off with a menu item ("turn on debug logging").

I'm going to check out the 1.4 logging. Thanks.
 
For a detailed explanation see:

Specifically:
[ignore]
There are two different styles of logging methods, to meet the needs of different communities of users.

First, there are methods that take an explicit source class name and source method name. These methods are intended for developers who want to be able to quickly locate the source of any given logging message. An example of this style is:

void warning(String sourceClass, String sourceMethod, String msg);

Second, there are a set of methods that do not take explicit source class or source method names. These are intended for developers who want easy-to-use logging and do not require detailed source information.

void warning(String msg);

For this second set of methods, the Logging framework will make a "best effort" to determine which class and method called into the logging framework and will add this information into the LogRecord. However, it is important to realize that this automatically inferred information may only be approximate. The latest generation of virtual machines perform extensive optimizations when JITing and may entirely remove stack frames, making it impossible to reliably locate the calling class and method.
[/ignore]
Hope this helps :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top