Log4j、patternLayout、类和类别
当在 log4j PatternLayout 中使用时,我无法确定这两个 log4j 转换字符之间的确切区别(log4j patternLayout)
- 类别 (%c)
- 类 (%C)
有人能给我一个例子吗那两个将是 不同的?
类别不是总是与类名匹配吗?
问候,
I am having trouble establishing the exact difference between using those two log4j conversion characters when used in a log4j PatternLayout (log4j patternLayout)
- category (%c)
- class (%C)
Can someone please give me an example where those two would be different?
Doesn't the category always match the class name?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您按照文档建议的流行方式初始化记录器,并在
X
类中使用它,那么您将得到相同的
%c
和%C
,因为记录器名称(由“com.foo.X.class.getName()”构造)将与发出日志记录语句的类名称相匹配。将您的记录器称为“something”
,您将获得
%c
的“something”和%C
的类名称。请注意,
%C
是由 log4j 从当前线程的堆栈跟踪中计算出来的,因此它会对性能产生很大的影响,这与%c
不同,后者只是一个字符串。您可以进行一个有趣的实验来验证它:假设
B
位于包com.foo
[%c][%m] 的输出> 将是:无论
B
的位置如何,模式[%C][%m]
的输出都将是:It will be the same if you initialize the logger in the popular way suggested by the documentation, and use it inside the
X
class:then you'll get the same for
%c
and%C
, because logger name (constructed by "com.foo.X.class.getName()") would match the class name where a logging statement was issued.Call your logger "something"
and you'll have "something" for
%c
and the class name for%C
.Note that
%C
is computed by log4j out of the current thread's stack trace, so it carries big performance impact, unlike%c
, which is simply a String. You can conduct an interesting experiment to validate it:The output for pattern
[%c][%m]
assumingB
is in packagecom.foo
will be:The output for pattern
[%C][%m]
regardless of the location ofB
will be: