Java logger that automatically determines caller's class name

后端 未结 21 864
礼貌的吻别
礼貌的吻别 2020-12-07 17:53
public static Logger getLogger() {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[1];
    final Logger logger          


        
21条回答
  •  臣服心动
    2020-12-07 18:20

    Creating a stack trace is a relatively slow operation. Your caller already knows what class and method it is in, so the effort is wasted. This aspect of your solution is inefficient.

    Even if you use static class information, you should not fetch the Logger again for each message. From the author of Log4j,Ceki Gülcü:

    The most common error in wrapper classes is the invocation of the Logger.getLogger method on each log request. This is guaranteed to wreak havoc on your application's performance. Really!!!

    This is the conventional, efficient idiom for getting a Logger is during class initialization:

    private static final Logger log = Logger.getLogger(MyClass.class);
    

    Note that this gives you a separate Logger for each type in a hierarchy. If you come up with a method that invokes getClass() on an instance, you will see messages logged by a base type showing up under the subtype's logger. Maybe this is desirable in some cases, but I find it confusing (and I tend to favor composition over inheritance anyway).

    Obviously, using the dynamic type via getClass() will require you to obtain the logger at least once per instance, rather than once per class like the recommended idiom using static type information.

提交回复
热议问题