miguialberto
miguialberto

Reputation: 326

Filter by class name in log4j2

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>

Upvotes: 13

Views: 18075

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

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>

Upvotes: 13

Related Questions