欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

log4j简单封装,log4j封装,packageecp.f

来源: javaer 分享于  点击 3796 次 点评:174

log4j简单封装,log4j封装,packageecp.f


packageecp.framework.log;

importjava.net.InetAddress;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;

importorg.apache.log4j.Logger;importorg.apache.log4j.PropertyConfigurator;

publicclassLog{protectedLoggerlogger; //将Log类封装成单实例的模式,独立于其他类。以后要用到日志的地方只要获得Log的实例就可以方便使用
privatestaticLoglog;
//构造函数,用于初始化Logger配置需要的属性
privateLog(Loggerlog4jLogger)
{
//获得当前目录路径
//StringfilePath=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.propertiesPropertyConfigurator.configureAndWatch("/u01/logs/log4j.properties");

}
/
获取构造器,根据类初始化Logger对象 *
@paramClassClass对象 @returnLogger对象 / publicstaticLoggetLogger(ClassclassObject){ if(log!=null)
returnlog;
else returnnewLog(Logger.getLogger(classObject)); }

/ 该事件ID包含当前时间和主机IP,是标示日志时间的唯一符 @seecom.ccae.monitor.log.ILogInfor#eventID() / protectedStringeventID(){ //获取当前时间 //如果不需要格式,可直接用dt,dt就是当前系统时间 Datedt=newDate(); //设置显示格式,24小时制 DateFormatdf=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss"); StringnowTime=df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/ddHH:mm:ss格式显示

StringnowIP; try{ InetAddressaddr=InetAddress.getLocalHost(); nowIP=addr.getHostAddress(); } catch(Exceptionex){ nowIP=""; }
nowTime=nowTime.replaceAll("/",""); nowTime=nowTime.replaceAll("",""); nowTime=nowTime.replaceAll(":",""); nowIP=nowIP.replace(".",""); StringnowID=nowTime+nowIP; returnnowID; }

publicvoiddebug(StringKeyWord,StringContent){ if(logger.isDebugEnabled()){ Stringmessage=""+KeyWord+""+Content; logger.debug(message); } } publicvoiddebug(StringContent){ if(logger.isDebugEnabled()){ logger.debug(Content); } } publicvoidfatal(StringKeyWord,StringContent){ Stringmessage=""+KeyWord+""+Content; logger.fatal(message); } publicvoidfatal(StringContent){ logger.fatal(Content); } publicvoidinfo(StringKeyWord,StringContent){ if(logger.isInfoEnabled()){ Stringmessage=KeyWord+""+Content; logger.info(message); } } publicvoidinfo(StringContent){ if(logger.isInfoEnabled()) logger.info(Content); } publicvoidwarn(StringKeyWord,StringContent){ Stringmessage=KeyWord+""+Content; logger.warn(message); } publicvoidwarn(StringContent){ logger.warn(Content); } publicvoiderror(StringKeyWord,StringContent){ Stringmessage1=KeyWord+""+Content; logger.error(message1); } publicvoiderror(StringContent){ logger.error(Content); }}

相关栏目:

用户点评