欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

log4j自定义日志等级

程序员文章站 2022-06-09 09:04:29
...
文章源地址:http://www.186info.com/?p=61

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

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

public class TestLevel extends Level {
public TestLevel(int level, String levelStr, int syslogEquivalent) {
super(level, levelStr, syslogEquivalent);
}
}

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

public class MyLogFiter extends Filter {
/**
* Do we return ACCEPT when a match occurs. Default is
* <code>false</code>, so that later filters get run by default
*/
boolean acceptOnMatch = false;

int levelMin;
int levelMax;

/**
* Return the decision of this filter.
*/
@Override
public int decide(LoggingEvent event) {
int inputLevel = event.getLevel().toInt();
if (inputLevel >= levelMin && inputLevel <= levelMax) {
return Filter.ACCEPT;
}
return Filter.DENY;
}

/**
* Get the value of the <code>LevelMax</code> option.
*/
public int getLevelMax() {
return levelMax;
}

/**
* Get the value of the <code>LevelMin</code> option.
*/
public int getLevelMin() {
return levelMin;
}

/**
* Get the value of the <code>AcceptOnMatch</code> option.
*/
public boolean getAcceptOnMatch() {
return acceptOnMatch;
}

/**
* Set the <code>LevelMax</code> option.
*/
public void setLevelMax(int levelMax) {
this.levelMax = levelMax;
}

/**
* Set the <code>LevelMin</code> option.
*/
public void setLevelMin(int levelMin) {
this.levelMin = levelMin;
}

/**
* Set the <code>AcceptOnMatch</code> option.
*/
public void setAcceptOnMatch(boolean acceptOnMatch) {
this.acceptOnMatch = acceptOnMatch;
}

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

public class Logger {

private final static int priority = 40100;
private final org.apache.log4j.Logger logger;
private static final String FQCN;

//以下为自定义的日志级别
public static final Level PAY_LEVEL = new TestLevel(priority, “TEST”, SyslogAppender.LOG_LOCAL0);

static {
FQCN = Logger.class.getName();
}

private Logger(Class<?> clazz) {
logger = org.apache.log4j.Logger.getLogger(clazz);
}

private Logger() {                          http://192.168.226.114:8080/tfs/JDRD/RD/_workitems
logger = org.apache.log4j.Logger.getRootLogger();
}

public static Logger getLogger(Class<?> clazz) {
return new Logger(clazz);
}

public static Logger getRootLogger() {
return new Logger();
}

public void test(Object message) {
forcedLog(logger, PAY_LEVEL, message);
}

public void test(Object message, Throwable t) {
forcedLog(logger, PAY_LEVEL, message, t);
}

private static void forcedLog(org.apache.log4j.Logger logger, Level level, Object message) {
logger.callAppenders(new LoggingEvent(FQCN, logger, level, message, null));
}

private static void forcedLog(org.apache.log4j.Logger logger, Level level, Object message, Throwable t) {
logger.callAppenders(new LoggingEvent(FQCN, logger, level, message, t));
}

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

<appender name=”TEST”>
<param name=”File” value=”/var/Test.log”/>
<param name=”Append” value=”true”/>
<param name=”DatePattern” value=”‘.’yyyy-MM-dd-HH”/>
<param name=”BufferedIO” value=”true”/>
<!– 8K为一个写单元 –>
<param name=”BufferSize” value=”8192″/>
<layout>
<param name=”ConversionPattern” value=”%d{yyyy-MM-dd HH:mm:ss}\t%F\t%L\t%M\t%m%n”/>
</layout>
<filter class=”MyLogFiter”>
<param name=”LevelMin” value=”40100″/>
<param name=”LevelMax” value=”40100″/>
</filter>
</appender>

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

logger.test(“hahaha,test”);