Java: How to get a class object of the current class from a static context?

后端 未结 8 1816
谎友^
谎友^ 2021-02-14 04:10

I have a logging function that takes the calling object as a parameter. I then call getClass().getSimpleName() on it so that I can easily get the class name to add to my log en

8条回答
  •  [愿得一人]
    2021-02-14 04:17

    Well, if you really don't want to hardcode something like ClassName.class, then you can try to infer the class by traversing the stacktrace. Fortunately, someone already did it : ). Also, consider using a logger that allows you to log something without specifying the calling object.

提交回复
热议问题