log4j自定義日志等級(jí),步驟如下 1.繼承l(wèi)ogj的level類,定義一個(gè)自己的日志級(jí)別: public class TestLevel extends Level { public TestLevel(int level, String levelStr, int syslogEquivalent) { super(level, levelStr, syslogEquivalent); } } 2.繼承Filter類,定義一個(gè)自己的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源代碼的情況下,對(duì)log4j的logger輸出進(jìn)行封裝,并且添加自己定義的級(jí)別的輸出方法: public class Logger { private final static int priority = 40100; private final org.apache.log4j.Logger logger; private static final String FQCN; //以下為自定義的日志級(jí)別 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() { 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為一個(gè)寫單元 –> <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”);
|