[英]Filter by class name in log4j2
I am using log4j2 and I don't know how I can filter by class name.我正在使用 log4j2,但我不知道如何按类名过滤。 I have tried with RegexFilter but it only filters the text message.我尝试过使用 RegexFilter,但它只过滤文本消息。 In old log4j was enough with tag 'filter'在旧的 log4j 中,带有标签“过滤器”就足够了
<filter class="aaaa.bbbb.cccc.ClassName">
Somebody knows how to do now?有人知道现在怎么办吗?
Thank you in advance!先感谢您!
Update:更新:
Ok, I did it!好的,我做到了! I need to define a logger and set the class name in attribute 'name':我需要定义一个记录器并在属性“name”中设置类名:
<loggers>
<logger name="aaaa.bbbb.cccc.ClassName" additivity="false" level="info">
<appender-ref ref="RollingFile" />
</logger>
<root level="error">
<appender-ref ref="RollingFile" />
</root>
</loggers>
This works automatically in Log4j if you follow the naming convention for loggers.如果您遵循记录器的命名约定,这将在 Log4j 中自动工作。 In your code, declare loggers with their class name:在您的代码中,使用类名声明记录器:
Logger logger = LogManager.getLogger(MyClass.class);
The logger is automatically assigned the name fully.qualified.class.name.of.MyClass
.记录器会自动分配一个名称fully.qualified.class.name.of.MyClass
。 Now, in your configuration you can use this fully qualified name (or the package name or the first part of the package ) to do filtering and routing .现在,在你的配置,你可以使用这个完全限定域名(或包名称或包的第一部分)做过滤和路由。
The below example shows how to filter log events based on the package of the class doing the logging: all DEBUG
and TRACE
level log events by classes in package com.other.company
will be ignored, and for classes in package com.another.project
only ERROR
and FATAL
logging will be included.下面的示例显示了如何根据执行日志记录的类的包过滤日志事件:包com.other.company
中的类的所有DEBUG
和TRACE
级别的日志事件将被忽略,对于包com.another.project
类仅包含ERROR
和FATAL
日志记录。
<Configuration status="warn">
<Appenders>
<File name="MyFile" fileName="logs/my.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
</File>
</Appenders>
<Loggers>
<!-- drops all DEBUG and TRACE logging done by any class in this package -->
<Logger name="com.other.company" level="INFO" />
<!-- log only ERROR and FATAL logging by classes in this package -->
<Logger name="com.another.project" level="ERROR" />
<!-- by default, all log events are written to MyFile -->
<Root level="trace">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
The below example shows how to route log events to separate log files based on the package of the class doing the logging: all logging by classes in package com.other.company
will not be written to my.log
by only to other.log
.下面的例子演示了如何根据包之类的做记录路线记录事件独立的日志文件:在包中的所有记录的类com.other.company
将不会被写入到my.log
仅向other.log
。
<Configuration status="warn">
<Appenders>
<File name="MyFile" fileName="logs/my.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
</File>
<File name="OtherFile" fileName="logs/other.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
</File>
</Appenders>
<Loggers>
<!-- all logging from this package and subpackages goes to OtherFile -->
<!-- Note: set additivity=false or logging will also go to the root logger -->
<Logger name="com.other.company" additivity="false">
<AppenderRef ref="OtherFile"/>
</Logger>
<Root level="trace">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.