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>
Filters allow Log Events to be evaluated to determine if or how they should be published. A Filter will be called on one of its filter methods and will return a Result, which is an Enum that has one of 3 values - ACCEPT, DENY or NEUTRAL.
Log4j2 Multiple Appenders – XML Configuration It does following things: Uses dynamic log root path where log files will be created. Pass environment variable as -DAPP_LOG_ROOT=c:/temp to configure it. Demo the usage of property constants defined in config file e.g. LOG_PATTERN in below file.
PatternLayout to format your logging information. The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.
The name of log4j2 loggers are case sensitive. Except root logger, all loggers can be obtained through passing their name into LogManager. getLogger() . LoggerContext is a vocal point for Logging system as you may have multiple LoggerContexts inside your application.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With