Java 使用 getSimpleName() 与 getName() 获取记录器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using getSimpleName() vs getName() for acquiring logger
提问by kuriouscoder
I've seen code that uses log4j
, which acquires logger
for a given Logger
using
我见过使用的代码log4j
,它获取logger
给定的Logger
使用
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?
前者 api 显式传递 with getSimpleName()
,而后者使用getName()
传递的Class
. 这两者有区别吗?如果我将各种包配置为在 log4j.properties 文件中以不同级别登录会影响吗?
回答by Armand
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.
如果您simpleName
在不同的包中有许多相同的类,您可能会感到困惑。拥有许多同名的记录器应该不是问题,否则可能会造成混淆。
回答by kdabir
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.
例如,我从不使用它simpleName
,Logger
因为它去掉了包名。除了在两个不同的包中存在相同的类名时会出现问题(导致两个类获得相同的记录器实例),您还失去了控制记录器继承的能力。
e.g. for two loggers:
例如对于两个记录器:
com.foo.A
com.foo.B
in properties, i can just have:
在属性中,我可以拥有:
log4j.logger.com.foo=DEBUG,CONSOLE
回答by Eyal Schneider
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.
我更喜欢使用全名(Class.getName())。当包被正确组织时,这允许调整 log4j 以处理源自 java 包树的不同部分的不同日志消息。
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.
例如,您可以轻松地配置以“com.mycompany.infra”开头的包中的所有类以使用特定的附加程序,并仅记录级别 WARN 或更高级别的消息。
回答by user2481237
E.g. My class ShapeDemo.java resides in com.test package, and I have written code like below.
例如,我的类 ShapeDemo.java 驻留在 com.test 包中,我编写了如下代码。
System.out.println("Name-->"+ShapeDemo.class.getName());
System.out.println("SimpleName-->"+ShapeDemo.class.getSimpleName());
This will output following
这将输出以下
Name-->com.test.ShapeDemo
SimpleName-->ShapeDemo
回答by Coman
Using of this.getClass().getName();
使用 this.getClass().getName();
Returns : alin.iwin.flickrbrowser.GetRawData
返回:alin.iwin.flickrbrowser.GetRawData
Meanwhile
同时
private String LOG_TAG = this.getClass().getSimpleName();
私人字符串LOG_TAG = this.getClass().getSimpleName();
Return only : GetRawData.
仅返回:GetRawData。