log4j简单封装

2015-08-24 0 724
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);

    }

}


遇见资源网 java log4j简单封装 http://www.ox520.com/9592.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务