log4j簡單封裝

csp008 9年前發布 | 2K 次閱讀 Java log4j 封裝

package ecp.framework.log;

import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log {
protected Logger logger;
    //將Log類封裝成單實例的模式,獨立于其他類。以后要用到日志的地方只要獲得Log的實例就可以方便使用   
    private static Log log;   
    //構造函數,用于初始化Logger配置需要的屬性   
    private Log(Logger log4jLogger)   
    {   
        //獲得當前目錄路徑   
        //String filePath=this.getClass().getResource("/").getPath();   
        //找到log4j.properties配置文件所在的目錄(已經創建好)   
        //filePath=filePath.substring(1).replace("bin", "src");   
        //獲得日志類logger的實例   
        //loger=Logger.getLogger(this.getClass());
    logger = log4jLogger;
        
        //logger所需的配置文件路徑    D:\publish2\log4j.properties
        PropertyConfigurator.configureAndWatch("/u01/logs/log4j.properties");
    }   
    /*
     
獲取構造器,根據類初始化Logger對象
      
     
@param Class Class對象
      @return Logger對象
     
/
    public static Log getLogger(Class classObject) {
     if(log!=null)   
            return log;   
        else  
            return new Log(Logger.getLogger(classObject));
    }

    /
     
該事件ID包含當前時間和主機IP,是標示日志時間的唯一符
      @see com.ccae.monitor.log.ILogInfor#eventID()
     
/
     protected String eventID() {
    // 獲取當前時間
    //如果不需要格式,可直接用dt,dt就是當前系統時間
    Date dt=new Date();
    //設置顯示格式,24小時制
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String nowTime= df.format(dt);//用DateFormat的format()方法在dt中獲取并以yyyy/MM/dd HH:mm:ss格式顯示
  
    String nowIP;
    try{
    InetAddress addr = InetAddress.getLocalHost();
    nowIP = addr.getHostAddress(); 
    }
    catch(Exception ex){
    nowIP = "";
    }         
    nowTime=nowTime.replaceAll("/","");
    nowTime=nowTime.replaceAll(" ","");
    nowTime=nowTime.replaceAll(":","");
    nowIP=nowIP.replace(".","");
    String nowID=nowTime+nowIP;
    return nowID;
     }

     public void debug(String KeyWord,String Content) {
     if(logger.isDebugEnabled()){
     String message=" "+KeyWord+" "+Content;
     logger.debug(message);
     }
     }
     public void debug(String Content) {
     if(logger.isDebugEnabled()){
     logger.debug(Content);
     }
    }
     public void fatal(String KeyWord,String Content) {
     String message=" "+KeyWord+" "+Content;
     logger.fatal(message);
     }
     public void fatal(String Content) {
     logger.fatal(Content);
    }
     public void info(String KeyWord,String Content) {
    if(logger.isInfoEnabled()){
    String message=KeyWord+" "+Content;
    logger.info(message);
    }
     }
     public void info(String Content) {
    if(logger.isInfoEnabled())
    logger.info(Content);
     }
     public void warn(String KeyWord,String Content) {
     String message=KeyWord+" "+Content;
     logger.warn(message);
     }
     public void warn(String Content) {
     logger.warn(Content);
    }
     public void error(String KeyWord,String Content) {
     String message1=KeyWord+" "+Content;
     logger.error(message1);
     }
     public void error(String Content) {
     logger.error(Content);
    }
}


 本文由用戶 csp008 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!