Pierre Rymiortz
Pierre Rymiortz

Reputation: 1127

Is there a getSimpleName() equivalent for writing method names in log statements?

Currently I write my log statements like this:

Log.i(TAG, MyClass.class.getSimpleName() + "#methodName" + someThing);

Is there a way to reference a method name similar to a Class's getSimpleName() that doesn't involve a hard coded String?

Upvotes: 2

Views: 479

Answers (1)

Juvanis
Juvanis

Reputation: 25950

You can use Thread.currentThread().getStackTrace()[1].getMethodName();

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
Log.i(TAG, MyClass.class.getSimpleName() + methodName + someThing);

Upvotes: 3

Related Questions