2

I am android developer and love logging very much. I had been using a convention for my logs that is in form of

[ClassName:methodName()]

Whenever i have to log something, this help me find exact location from where a specific log is generated and helpful in many cases.

Instead of writing each time the

ClassName

and

MethodName

manually i wrote a method. Their may be better approach of doing this. Any answer without using loop is Accepted.

public static String getRef(Object object) {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        String method = "";
        if (elements.length > 0) {
            for (int i = 0; i < elements.length; i++) {
                if (elements[i].getClassName().equals(object.getClass().getCanonicalName())) {
                    method = elements[i].getMethodName();
                    break;
                }
            }
        }
        return "[" + object.getClass().getSimpleName() + "::" + method + "()]";
    }

Calling:

Log.d(STRING_TAG , GlobalConfig.getRef(this) + " Server Response:"+json);

i had already tried using

Object.getClass().getEnclosingMethod();

but its always returning me null and also it is supposed to return the method name where it is called right? as the documentation says

If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class. Returns null otherwise. In particular, this method returns null if the underlying class is a local or anonymous class immediately enclosed by a type declaration, instance initializer or static initializer.
Return
Method the immediately enclosing method of the underlying class, if that class is a local or anonymous class; otherwise null.

4
  • 2
    Use slf4j and log4j there you can simply configure the outpus as you like, no need to reinvent the wheel... Commented May 18, 2017 at 7:42
  • @TimothyTruckle thanks for your feedback, actually android does have its own Logging Library, all i want is to get the method name at any point in application. Commented May 18, 2017 at 7:46
  • " android does have its own Logging Library" - wrong: slf4j.org/android Commented May 18, 2017 at 8:48
  • @TimothyTruckle thanks i didn't knew that. Thanks Commented May 18, 2017 at 10:23

1 Answer 1

2

If you going to call it directly, I explain:

  1. You are on class A, and want to log that you are here
  2. You will call this method directly from class A

You can use this:

 StackTraceElement[] elements = Thread.currentThread().getStackTrace();
 String method = "";
 method = elements[elements.length - 2].getMethodName();
 return "[" + object.getClass().getSimpleName() + "::" + method + "()]";

Where you get the last but one class called. Ex:

public class Random {

 public static void main(String[] args) {
  Random r = new Random();
  r.test();
 }

 public void test() {
  System.out.println(Logger.getRef(this));
 }

}

class Logger {

 public static String getRef(Object object) {
  StackTraceElement[] elements = Thread.currentThread().getStackTrace();
  String method = "";
  method = elements[2].getMethodName();
  return "[" + object.getClass().getSimpleName() + "::" + method + "()]";
 }

}

Out: [Random::test()]

Sign up to request clarification or add additional context in comments.

9 Comments

you cannot find the index statically elements[elements.length - 2]
Sorry, but, why not? I mean, if you want the caller, it will be that last but one in the array. Even when you call Log.d(STRING_TAG , GlobalConfig.getRef(this) + " Server Response:"+json);. That is the only way to get the information you want without looping through all the index and checking by class name
what if there are other elements in StackTrace, according to your code that is right
public static Random r; public static void main(String[] args) { r = new Random(); new Thread(new Runnable(){ public void run(){ r.test(); for(int i=0;i<100;i=i+2){ //do something System.out.println("working:"); } } }).start(); }
Make the above change and then try, you cannot fix the element and its not always the second last one
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.