在log4j2中按类别名称过滤 [英] Filter by class name in log4j2

查看:102
本文介绍了在log4j2中按类别名称过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4j2,但我不知道如何按类名进行过滤.我已经尝试过RegexFilter,但它仅过滤文本消息.在旧的log4j中,使用标记过滤器"就足够了

I am using log4j2 and I don't know how I can filter by class name. I have tried with RegexFilter but it only filters the text message. In old log4j was enough with tag 'filter'

<filter class="aaaa.bbbb.cccc.ClassName">

有人知道现在该怎么做吗?

Somebody knows how to do now?

提前谢谢!

更新:

好,我做到了!我需要定义一个记录器,并在属性名称"中设置类名称:

Ok, I did it! I need to define a logger and set the class name in attribute '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>

推荐答案

如果您遵循记录器的命名约定,则此方法在Log4j中会自动运行.在您的代码中,声明记录器及其类名称:

This works automatically in Log4j if you follow the naming convention for loggers. In your code, declare loggers with their class name:

Logger logger = LogManager.getLogger(MyClass.class);

自动为记录器分配名称 fully.qualified.class.name.of.MyClass .现在,在您的配置中,您可以使用此完全限定的名称(或软件包名称 package 的第一部分)来执行过滤路由.

The logger is automatically assigned the name 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.

以下示例显示了如何根据执行日志记录的类的包来过滤日志事件:所有 DEBUG TRACE 级别的日志事件均按包中的类进行> com.other.company 将被忽略,并且对于包 com.another.project 中的类,仅记录 ERROR FATAL 将包括在内.

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.

<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>


路由

下面的示例显示如何根据进行日志记录的类的包将日志事件路由到单独的日志文件:不会将包 com.other.company 中所有类的日志记录写入日志 my.log 只能访问 other.log .


Routing

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.

<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>

这篇关于在log4j2中按类别名称过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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