使用 getSimpleName() 与 getName() 获取记录器

发布于 2024-10-05 05:52:08 字数 403 浏览 0 评论 0原文

我见过使用 log4j 的代码,它使用通过 getSimpleName 显式传递的前一个 api 获取给定 Logger

static public Logger getLogger(String name)

logger

static public Logger getLogger(Class clazz)

(),而后者在传递的 Class 上使用 getName()。这两者有区别吗?如果我在 log4j.properties 文件中配置各种包以不同级别进行日志记录,会产生影响吗?

I've seen code that uses log4j, which acquires logger for a given Logger using

static public Logger getLogger(String name)

and

static public Logger getLogger(Class clazz)

with the former api passed explicitly with getSimpleName(), while the latter uses getName() on the passed Class. Is there a difference between these two? Would it affect if I configure various packages to log at different level in log4j.properties file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南…巷孤猫 2024-10-12 05:52:08

是的,存在巨大差异。

我从不使用 simpleName 作为 Logger 实例,因为它会删除包名称。
除了在两个不同的包中存在相同的类名时出现问题(导致两个类获得相同的记录器实例)之外,您还失去了控制记录器继承的能力。

例如,对于两个记录器:

com.foo.A
com.foo.B

在属性中,我可以有:

log4j.logger.com.foo=DEBUG,CONSOLE

Yes there is a huge difference.

I never use simpleName for Logger instance as it strips down the package name.
Apart from having problems when same class name exists in two different packages (leading to both classes getting the same logger instance), you lose the ability to control logger inheritance.

e.g. for two loggers:

com.foo.A
com.foo.B

in properties, i can just have:

log4j.logger.com.foo=DEBUG,CONSOLE
西瓜 2024-10-12 05:52:08

例如,我的类 ShapeDemo.java 位于 com.test 包中,我编写了如下代码。

System.out.println("Name-->"+ShapeDemo.class.getName());
System.out.println("SimpleName-->"+ShapeDemo.class.getSimpleName());

这将输出以下内容

Name-->com.test.ShapeDemo
SimpleName-->ShapeDemo

E.g. My class ShapeDemo.java resides in com.test package, and I have written code like below.

System.out.println("Name-->"+ShapeDemo.class.getName());
System.out.println("SimpleName-->"+ShapeDemo.class.getSimpleName());

This will output following

Name-->com.test.ShapeDemo
SimpleName-->ShapeDemo
待"谢繁草 2024-10-12 05:52:08

使用 this.getClass().getName();

返回:alin.iwin.flickrbrowser.GetRawData

同时

private String LOG_TAG = this.getClass().getSimpleName();

仅返回:GetRawData。

Using of this.getClass().getName();

Returns : alin.iwin.flickrbrowser.GetRawData

Meanwhile

private String LOG_TAG = this.getClass().getSimpleName();

Return only : GetRawData.

一人独醉 2024-10-12 05:52:08

我更喜欢使用全名(Class.getName())。当包被正确组织时,这允许调整 log4j 来处理来自 java 包树的不同部分的不同日志消息。

例如,您可以轻松配置以“com.mycompany.infra”开头的包中的所有类以使用特定的附加程序,并仅记录 WARN 或更高级别的消息。

I prefer using the full name (Class.getName()). When packages are organized correctly, this allows tuning log4j to handle differently log messages originating from different parts of the java packages tree.

For example, you can easily configure all classes in packages starting with "com.mycompany.infra" to use a specific appender, and log only messages of level WARN or above.

℡Ms空城旧梦 2024-10-12 05:52:08

如果不同包中有许多具有相同 simpleName 的类,您可能会感到困惑。拥有许多同名的记录器应该不是问题,否则可能会造成混乱。

You might get confused if you have many classes with the same simpleName in different packages. Having many loggers with the same name should not be a problem otherwise - just could be confusing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文