Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class Name as Default Tag: Support Inheritance #8

Open
commonsguy opened this issue Jan 9, 2016 · 2 comments
Open

Class Name as Default Tag: Support Inheritance #8

commonsguy opened this issue Jan 9, 2016 · 2 comments

Comments

@commonsguy
Copy link

Not sure if this is practical, but...

class Foo {
    public void foo() {
        Log.d("Hello"); // prints 'D/Foo: Hello'
    }
}

As your docs show, this uses Foo as the tag, if somebody calls foo() on an instance of Foo.

class Foo {
    public void foo() {
        Log.d("Hello"); // prints 'D/Foo: Hello'
    }
}

class Bar extends Foo {
    public void baz() {
        foo();
    }
}

Calling baz() on an instance of Bar also logs Foo as the tag, rather than Bar. IOW, the class name used for the tag is the class containing the method doing the logging, not the class whose instance is the one calling that method.

IMHO, ideally, Bar would be logged in this case. If you disagree and feel that Foo is the right answer, that's cool, but you might want to mention this in the docs, as it caught me by surprise.

@zserge
Copy link
Owner

zserge commented Jan 10, 2016

I see your point and it probably makes certain sense. However I wonder if there is a technical possibility to achieve this?

Currently, the default tag is extracted from the stack trace (from the caller class name) and that's why the class actually calling the log is used as a tag.

For the behaviour you describe it will require to scan the stack trace upwards while the caller is an ancestor of the given class. But this assumption is still weak, for example:

class Foo {
  public void foo() { Log.d("Hello"); }
}
class Bar extends Foo {
  private Foo mFoo = new Foo();
  public void baz() { foo(); } // Ok, this should print "D/Bar: Hello"
  public void qux() { mFoo.foo(); } // But this should still be "D/Foo: Hello", isn't it?
}

How to tell these two cases? I don't think it's possible to tell one from another using the information from the StackTraceElement...

@commonsguy
Copy link
Author

Yeah, that's why I started off with "Not sure if this is practical"... :-)

You have infinitely more experience with StackTraceElement than I do, so you'll know far better than I what is and is not possible here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants