LoggerFactory.getLogger(ClassName.class)vs LoggerFactory.getLogger(this.getClass()。getName()) [英] LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName())

查看:1330
本文介绍了LoggerFactory.getLogger(ClassName.class)vs LoggerFactory.getLogger(this.getClass()。getName())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高Java的优化技能。为了实现这一目标,我制作了一个旧程序,并且我正在努力让它变得更好。在这个程序中,我使用SL4J进行日志记录。为了得到我做的记录器:

I'm trying to improve my optimization skills in Java. In order to achieve that, I've got an old program I made and I'm trying my best to make it better. In this program I'm using SL4J for logging. To get the logger I did:

private static final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

在我编写代码的时候,我认为这是最好的选择,因为我删除了一个引用类名(可以重构)。但现在我不再那么肯定...

At the time I wrote the code, I thought this was the best option, because I remove a reference to the class name(which may be refactored). But now I'm not so sure anymore...

private static final Logger logger = LoggerFactory.getLogger(ClassName.class);

另一方面,保留对类名的引用,但它会删除一个方法调用。对于一个班级来说,这可能不是一个很大的改进,但是当你有很多班级时,这可能就是一个问题。

On the other side, keeps the reference to the class name, but it removes one method call. This may not be a big improvement in performance for one class, but when you have lots of class, this may be something.

所以我的问题是:

哪种方法更好?使用类名还是通过反射获取?

Which approach is better? Using the class name or getting it through reflection?

请用赞成和反对来激励你的回答。谢谢。

Please, motivate your answer with pro and cons. Thank you.

推荐答案

我会在这里分享我的观点。我想说的是,你不应该从性能的角度来打扰你。可能在代码中有一些部分可以比这个更优化:)

I'll share my opinion here. I would say that this is the case that you shouldn't be bothered from the performance point of view. Probably in the code there are parts that can be optimized much more than this thing :)

现在,关于你的问题。看看 LoggerFactory的代码

Now, regarding your question. Take a look on LoggerFactory's code

请注意 getLogger(Class<?> name)只需调用重载方法:

Note that getLogger(Class<?> name) just calls the overloaded method:

Logger logger = getLogger(clazz.getName());

并进行一些额外的计算。因此,使用String的方法显然会稍快一些。

And makes some additional calculations. So the method with String is obviously slightly faster.

通常,模式是将Logger引用保持为类中的静态字段,如下所示:

In general the pattern is to maintain the Logger reference as a static field in the class, something like this:

public class SomeClass {
   private static final Logger LOG =   LoggerFactory.getLogger(SomeClass.class);
}

在这种情况下你不能真正使用这个.getClass()因为这个实际上并不存在(你在静态上下文中运行)。

In this case you can't really use this.getClass() because this doesn't actually exists (you're running in a static context).

根据我的经验,最好使用 ClassName.getClass()作为参数,除非你真的想要使用来自不同类的相同记录器。在这种情况下,您最好使用一些表示记录器的逻辑常量。

From my experience its better to use the ClassName.getClass() as a parameter unless you really want to use the same logger from different classes. In such a case you better use some logical constant that denotes the logger.

例如,假设您尝试使用3个不同的类来访问数据库。
所以你创建logger'DB',分配一个文件追加器,它将写入database.log,你想在这3个不同的类中重复使用相同的记录器。

For example, let's say you're trying to use 3 different classes to access the database. So you create logger 'DB', assign a file appender that will write to database.log and you want to reuse the same logger among these 3 different classes.

所以你应该使用以下代码:

So you should use the following code:

public class SomeClass {
   private static final Logger LOG =   LoggerFactory.getLogger("DB");
}

希望这会有所帮助

这篇关于LoggerFactory.getLogger(ClassName.class)vs LoggerFactory.getLogger(this.getClass()。getName())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆