LOG4J2 Custom Filter

Source: Internet
Author: User

Using the Druid data source, you can print all of the executed SQL, adding the following logger to the LOG4J2

<logger name= "druid.sql.Statement" level= "Debug"/> <logger "name=" Druid.sql.ResultSet "
Debug"/ >
However, after the integrated quartz distributed version, quartz in the background of the regular execution of SQL, these SQL also through the log4j2 non-stop printing, and constantly brush screen.

These SQL logs are not what I need and I don't want to record it.

After spending some time on log4j2 research, I found that it has filter function, it can filter unwanted log, very convenient. More details look here: Log4j2 Filters

But I didn't find the filter I needed, and I needed to filter the quartz log. So I have a custom filter that solves my problem.

After analysis, found that the print Quartz log thread name contains quartz and schedulerfactory, so starting from here, wrote a filter to exclude the specified thread name records log.

@Plugin (name = "Threadfilter", category = Node.category, ElementType = filter.element_type, PrintObject = true) public CLA
	 
    SS Threadfilter extends Abstractfilter {private final String threadname;
        Private Threadfilter (String threadname, result onmatch, result Onmismatch) {super (Onmatch, Onmismatch);
    This.threadname = ThreadName; Public result filter (Logger Logger, level level, Marker Marker, String msg, object[] params) {return filter (
    Thread.CurrentThread ()); Public result filter (Logger Logger, level level, Marker Marker, Object msg, Throwable t) {return filter (thre
    Ad.currentthread ()); Logger Logger, Level level, Marker Marker, message msg, Throwable t) {return filter (THR
    Ead.currentthread ());
    @Override Public result Filter (LogEvent event) {return filter (Thread.CurrentThread ()); Public result filter (thread thread) {if (Thread.getname () ==null) return onmismatch; Return Thread.getname (). IndexOf (threadname) >=0?
    Onmatch:onmismatch;
    @Override public String toString () {return threadname.tostring ();
					@PluginFactory public static Threadfilter Createfilter (@PluginAttribute ("ThreadName") String ThreadName, @PluginAttribute ("Onmatch") final result match, @PluginAttribute ("Onmismatch") final result mismatch) {RE
    Turn New Threadfilter (ThreadName, match, mismatch); }
}
The Log4j2.xml is then configured as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <configuration status= "Off" monitorinterval= "" packages= " Org.myapp.common.log4j ">   <Properties>   <property name=" appname ">myapp</Property> & nbsp   <property name= "LogDir" >/data/myapp/logs</Property>   </Properties>   < appenders>     <console name= "stdout" target= "system_out" >       <patternlayout 
Pattern= "%d{absolute}%5p [%c{2}:%line]-%m%n"/>       <!--%d%-5p [%t]%c{2} (%f:%l)-%m%n-->     </Console>     <!--programmer Debug Log-->       <rollingrandomaccessfile name
= "LogFile" filename= "${logdir}/${appname}.log" filepattern= "${logdir}/${appname}.%d{yyyy-mm-dd}.log.gz" >         <!--<regexfilter regex= "quartz.*" onmatch= "DENY" onmismatch= "ACCEPT"/>--> & nbsp       <Filters>      <threaDfilter threadname= "Quartz" onmatch= "DENY" onmismatch= "NEUTRAL"/>      <threadfilter threadname= "Schedulerfactory" onmatch= "DENY" onmismatch= "ACCEPT"/>    </Filters>         &L T Patternlayout pattern= "%date{mm-dd HH:mm:ss}%level [%thread][%file:%line]-%msg%n"/>         ; policies>             <timebasedtriggeringpolicy interval= "5" modulate= "true"/> & nbsp           <sizebasedtriggeringpolicy size= "MB"/>         </poli cies>         <DefaultRolloverStrategy>        <delete basepath= "${l Ogdir} "maxdepth=" 1 ">          <iffilename glob=" ${appname}.*.log.gz "/>   &nbs P      <iflastmodified age= "30d"/>        </Delete>   &NBSP;&LT;/DE Faultrolloverstrategy>     </RollingRandomAccessFile>     </Appenders>   <Loggers>   & nbsp <root level= "Error" >     <appenderref ref= "stdout"/>     <appenderref  ref= "log File "/>     </Root>     <logger name=" Org.myapp "level=" Debug "/>     <log
Ger name= "Org.springframework" level= "info"/>     <logger name= "org.hibernate" level= "error"/>     <logger name= "druid.sql.Statement" level= "Debug"/>     <logger name= "

   
   
   
    Druid.sql.ResultSet "level=" Debug "/>   </Loggers> </Configuration>
Note: The Packages property of the configuration label is set to the package name of the custom filter (threadfilter), which means that log4j2 will look for the extended class under this package.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.