Filter by class name in log4j2

感情迁移 提交于 2021-01-27 04:06:48

问题


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?

Thank you in advance!

Update:

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>

回答1:


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

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.


Filtering

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>

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>


来源:https://stackoverflow.com/questions/16917008/filter-by-class-name-in-log4j2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!