测试程序源代码
- import org.apache.log4j.Logger;
- /**
- * 使Log4j的log输出重定向到指定函数
- * Last Update Date: 2014-01-23
- * 依赖库
- * [1]log4j-1.2.9.jar
- * [2]JDK 1.6.x
- * @author lijun
- *
- */
- public class TestMain{
- static Logger log = Logger.getLogger(TestMain.class.getName());
- public static void main(String[] args)
- {
- try
- {
- TestMain tm = new TestMain();
- /*
- 打印所有的Appender实例
- Enumeration enumd=log.getRootLogger().getAllAppenders();
- while(enumd.hasMoreElements()){
- Appender app=(Appender)enumd.nextElement();
- System.out.println(app.getName());
- System.out.println("in appender");
- }
- */
- /*
- 取Class的Method的方法
- Class ptypes[] = new Class[1];
- ptypes[0] = Class.forName("java.lang.String");
- Method m = TestMain.class.getMethod("DebugMsg",ptypes);
- */
- {
- CallbackAppender ca = (CallbackAppender)Logger.
- getRootLogger().getAppender("ca");
- if(ca!=null)
- ca.SetCallback(tm.getClass().getMethod("DebugMsg",
- new Class[] {String.class}));
- }
- log.debug("from my appender!");
- }catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
- public static void DebugMsg(String msg) {
- System.out.println("现在log输出被指定到这里了 => "+msg);
- }
- }
Appender源代码
- import java.lang.reflect.Method;
- import org.apache.log4j.AppenderSkeleton;
- import org.apache.log4j.spi.LoggingEvent;
- /**
- *
- * 这个Appender的作用,是使log输出重定向到某个函数里去
- * @author lijun
- *
- */
- public class CallbackAppender extends AppenderSkeleton {
- Method m_ian;
- @Override
- protected void append(LoggingEvent event) {
- try {
- String msg = this.getLayout().format(event);
- //调用方法重定向log输出.begin
- if( m_ian!=null )
- {
- Object[] args = {msg};
- m_ian.invoke(null, args);
- }
- //调用方法重定向log输出.end
- } catch (Exception ie) {
- ie.printStackTrace();
- }
- }
- @Override
- public void close() {
- if (closed)
- return;
- //Close动作
- //Close标记
- closed = true;
- }
- @Override
- public boolean requiresLayout() {
- return true;
- }
- public void SetCallback(Method ian)
- {
- m_ian = ian;
- }
- }
log4j.xml的配置清单
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
- <appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
- </layout>
- <!--过滤器设置输出的级别-->
- <filter class="org.apache.log4j.varia.LevelRangeFilter">
- <param name="levelMin" value="debug" />
- <param name="levelMax" value="warn" />
- <param name="AcceptOnMatch" value="true" />
- </filter>
- </appender>
- <!-- 指定logger的设置,additivity指示是否遵循缺省的继承机制-->
- <appender name="ca" class="CallbackAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern"
- value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
- </layout>
- </appender>
- <!-- 根logger的设置-->
- <root>
- <priority value ="debug"/>
- <!-- <appender-ref ref="myConsole"/> -->
- <appender-ref ref="ca"/>
- </root>
- </log4j:configuration>