logback custom log level handling
I want to extend log开发者_如何学JAVAback to send logs with ERROR log levels to our internal logging service (with http post and some custom parameters).
Is writing a custom logback Filter the best way to do this? The word 'filter' to me sounds more to "filter out logs".
You can use the ThresholdFilter
, that will only log messages with level given log level (or above) on a specific appender. Here is an example, on how to configure a ThresholdFilter
for log level ERROR. It will log all ERROR messages using the surrounding ConsoleAppender
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- ... -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!-- log messages with ERROR (and above) only -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>%date{yyyy-MM-dd - HH:mm:ss} %-5level %logger{60} - %message%n</pattern>
</encoder>
</appender>
<!-- ... -->
</configuration>
So you must replace the sourrounding ConsoleAppender
with your appender implementation, which will log to your custom logging service via HTTP Post. The ThresholdFilter
can be used as shown in the example above.
For learning how to implement your own Appender, you might want to take a look at the simpledb-appender project, which implements a custom appender for Amazon SimpleDB.
精彩评论