Packagecom.someorg; Public classLogger { Public Static voidinfo (String something) {intLineno =Getcurrentlinenumber (); String MethodName=Getcurrentmethodname (); String ClassName=Getcurrentclassname (); System.out.println (ClassName+ "." + MethodName + "(" + Lineno + "):" +something); } Public Static intGetcurrentlinenumber () {returnThread.CurrentThread (). Getstacktrace () [3].getlinenumber (); } Public StaticString Getcurrentmethodname () {returnThread.CurrentThread (). Getstacktrace () [3].getmethodname (); } Public StaticString Getcurrentclassname () {returnThread.CurrentThread (). Getstacktrace () [3].getclassname (); }}
Packagecom.someorg; Public classApp { Public Static voidMain (string[] args) {M1 (); } Public Static voidM1 () {//Do some logic here;Logger.info ("Hello 1"); M2 (); } Public Static voidm2 () {//Do some logic here;Logger.info ("Hello 2"); }}
Java gets run-time file name, class name, method name, line number