log4j自定义日志等级

文章源地址:http://www.186info.com/?p=61

log4j自定义日志等级,步骤如下

1.继承logj的level类,定义一个自己的日志级别:

publicclassTestLevelextendsLevel{

publicTestLevel(intlevel,StringlevelStr,intsyslogEquivalent){

super(level,levelStr,syslogEquivalent);

}

}

2.继承Filter类,定义一个自己的filter:

publicclassMyLogFiterextendsFilter{

/**

*DowereturnACCEPTwhenamatchoccurs.Defaultis

*<code>false</code>,sothatlaterfiltersgetrunbydefault

*/

booleanacceptOnMatch=false;

intlevelMin;

intlevelMax;

/**

*Returnthedecisionofthisfilter.

*/

@Override

publicintdecide(LoggingEventevent){

intinputLevel=event.getLevel().toInt();

if(inputLevel>=levelMin&&inputLevel<=levelMax){

returnFilter.ACCEPT;

}

returnFilter.DENY;

}

/**

*Getthevalueofthe<code>LevelMax</code>option.

*/

publicintgetLevelMax(){

returnlevelMax;

}

/**

*Getthevalueofthe<code>LevelMin</code>option.

*/

publicintgetLevelMin(){

returnlevelMin;

}

/**

*Getthevalueofthe<code>AcceptOnMatch</code>option.

*/

publicbooleangetAcceptOnMatch(){

returnacceptOnMatch;

}

/**

*Setthe<code>LevelMax</code>option.

*/

publicvoidsetLevelMax(intlevelMax){

this.levelMax=levelMax;

}

/**

*Setthe<code>LevelMin</code>option.

*/

publicvoidsetLevelMin(intlevelMin){

this.levelMin=levelMin;

}

/**

*Setthe<code>AcceptOnMatch</code>option.

*/

publicvoidsetAcceptOnMatch(booleanacceptOnMatch){

this.acceptOnMatch=acceptOnMatch;

}

}

3.在不侵入log4j源代码的情况下,对log4j的logger输出进行封装,并且添加自己定义的级别的输出方法:

publicclassLogger{

privatefinalstaticintpriority=40100;

privatefinalorg.apache.log4j.Loggerlogger;

privatestaticfinalStringFQCN;

//以下为自定义的日志级别

publicstaticfinalLevelPAY_LEVEL=newTestLevel(priority,“TEST”,SyslogAppender.LOG_LOCAL0);

static{

FQCN=Logger.class.getName();

}

privateLogger(Class<?>clazz){

logger=org.apache.log4j.Logger.getLogger(clazz);

}

privateLogger(){http://192.168.226.114:8080/tfs/JDRD/RD/_workitems

logger=org.apache.log4j.Logger.getRootLogger();

}

publicstaticLoggergetLogger(Class<?>clazz){

returnnewLogger(clazz);

}

publicstaticLoggergetRootLogger(){

returnnewLogger();

}

publicvoidtest(Objectmessage){

forcedLog(logger,PAY_LEVEL,message);

}

publicvoidtest(Objectmessage,Throwablet){

forcedLog(logger,PAY_LEVEL,message,t);

}

privatestaticvoidforcedLog(org.apache.log4j.Loggerlogger,Levellevel,Objectmessage){

logger.callAppenders(newLoggingEvent(FQCN,logger,level,message,null));

}

privatestaticvoidforcedLog(org.apache.log4j.Loggerlogger,Levellevel,Objectmessage,Throwablet){

logger.callAppenders(newLoggingEvent(FQCN,logger,level,message,t));

}

}

4.修改log4j的xml配置文件,添加自定义的appender:

<appendername=”TEST”>

<paramname=”File”value=”/var/Test.log”/>

<paramname=”Append”value=”true”/>

<paramname=”DatePattern”value=”‘.’yyyy-MM-dd-HH”/>

<paramname=”BufferedIO”value=”true”/>

<!–8K为一个写单元–>

<paramname=”BufferSize”value=”8192″/>

<layout>

<paramname=”ConversionPattern”value=”%d{yyyy-MM-ddHH:mm:ss}\t%F\t%L\t%M\t%m%n”/>

</layout>

<filterclass=”MyLogFiter”>

<paramname=”LevelMin”value=”40100″/>

<paramname=”LevelMax”value=”40100″/>

</filter>

</appender>

5.在需要输出日志的地方使用封装的logger输出方法即可:

logger.test(“hahaha,test”);

相关推荐