log 4 jカスタムラベルレベル指定ファイルへ出力

4174 ワード

需要:システムがアクセスされたログとシステム負荷のログは、最初の設計時にデータベースに配置され、大きすぎることを防止し、専用のディレクトリの下に配置する必要があります.
1.
package com.ats.adapter;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.net.SyslogAppender;


public class CustomerLog {
	
	/** 
     *  ,  
     */  
    private static final Level TimerResourceLever = new CustomerLogLevel(20050,"timerResourceLever",SyslogAppender.LOG_LOCAL0); 
    
    private static final Level ThreadSysLog = new CustomerLogLevel(20051,"threadSysLog",SyslogAppender.LOG_LOCAL1);  
      
	/** 
     *  log4 
     */  
    private static class CustomerLogLevel extends Level{  
        public CustomerLogLevel(int level, String levelStr, int syslogEquivalent) {  
            super(level, levelStr, syslogEquivalent);  
        }         
    }  
    
    /** 
     *  
     */  
    public static void customerLogTimer(Logger logger,Object objLogInfo){  
        logger.log(TimerResourceLever, objLogInfo);  
    }  
   /**
    *  
    */
    public  static void customerLogThread(Logger logger,Object objLogInfo){
    	logger.log(ThreadSysLog, objLogInfo); 
    }
}

2.
package com.ats.adapter;
import org.apache.log4j.spi.Filter;  
import org.apache.log4j.spi.LoggingEvent;
public class CustomerLogFilter extends Filter {
    /**
     * log4jfilter 
     */
	boolean acceptOnMatch = false;  
    private int levelMin;  
    private int levelMax;  
      
    public int getLevelMin() {  
        return levelMin;  
    }  

    public void setLevelMin(int levelMin) {  
        this.levelMin = levelMin;  
    }  

    public int getLevelMax() {  
        return levelMax;  
    }  
  
    public void setLevelMax(int levelMax) {  
        this.levelMax = levelMax;  
    }  
  
    public boolean isAcceptOnMatch() {  
        return acceptOnMatch;  
    }  
  
    public void setAcceptOnMatch(boolean acceptOnMatch) {  
        this.acceptOnMatch = acceptOnMatch;  
    }  
    @Override  
    public int decide(LoggingEvent lgEvent) {  
        int inputLevel = lgEvent.getLevel().toInt();  
          
        if(inputLevel>=levelMin && inputLevel <= levelMax){  
            return 0;  
        }         
        return -1;  
    }  
}

3.
 #TimerResources
log4j.additivity.timerResourceLever=false
log4j.logger.timerResourceLever=info,timerResourceLever
log4j.appender.timerResourceLever=org.apache.log4j.DailyRollingFileAppender
log4j.appender.timerResourceLever.filter.a=com.ats.adapter.CustomerLogFilter
log4j.appender.timerResourceLever.filter.a.levelMin=20050
log4j.appender.timerResourceLever.filter.a.levelMax=20050
log4j.appender.timerResourceLever.filter.a.AcceptOnMatch=true
log4j.appender.timerResourceLever.encoding=UTF-8
log4j.appender.timerResourceLever.Append=true
log4j.appender.timerResourceLever.layout=org.apache.log4j.PatternLayout
log4j.appender.timerResourceLever.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] <%l> %m%nx
log4j.appender.timerResourceLever.DatePattern='-'yyyy-MM-dd'.log'
log4j.appender.timerResourceLever.File=${catalina.home}/logs/ats/resource.log
 #ThteadSysLog
log4j.additivity.threadSysLog=false
log4j.logger.threadSysLog=info,timerResourceLever,threadSysLog
log4j.appender.threadSysLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.threadSysLog.filter.b=com.ats.adapter.CustomerLogFilter
log4j.appender.threadSysLog.filter.b.levelMin=20051
log4j.appender.threadSysLog.filter.b.levelMax=20051
log4j.appender.threadSysLog.filter.b.AcceptOnMatch=true
log4j.appender.threadSysLog.encoding=UTF-8
log4j.appender.threadSysLog.Append=true
log4j.appender.threadSysLog.layout=org.apache.log4j.PatternLayout
log4j.appender.threadSysLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] <%l> %m%nx
log4j.appender.threadSysLog.DatePattern='-'yyyy-MM-dd'.log'
log4j.appender.threadSysLog.File=${catalina.home}/logs/ats/sysLog.log

4.
 resources  sysLog   model  
 
CustomerLog.customerLogTimer(log, resources); 

CustomerLog.customerLogThread(log, sysLog);