Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of __FUNCTION__ of C lanauage in Java

Tags:

java

debugging

For debug purpose, I want to print out current executing function name in Java. If it were C, I would just do printf("%s \n" ,__FUNCITON__).

like image 906
pierrotlefou Avatar asked Jul 06 '10 05:07

pierrotlefou


2 Answers

new Exception().getStackTrace()[0].getMethodName();
like image 101
Sjoerd Avatar answered Oct 08 '22 07:10

Sjoerd


I'd use one of the logging frameworks (logback with slf4j is probably the best one at the moment, but log4j should suffice), then you can specify a layout that will print the method name logback layout documentation here

like image 43
Goibniu Avatar answered Oct 08 '22 06:10

Goibniu