加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

log4j简单封装

发布时间:2020-12-15 03:20:05 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package ecp.framework.log; import java.net.InetAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Dat

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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:publish2log4j.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);
? ? }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读