⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logger.java

📁 设计模式关于LOG的CLL系统
💻 JAVA
字号:
/** *Logger类用来创建一个LOG,并对LOG进行初始化 */package cn.edu.nju.software.sd.cll;import java.util.*;import java.io.*;public class Logger extends LayoutFormat implements Comparable{	//第一次引用Logger的时候的时间,便于后面的程序计算程序运行的时间	public static long timeStart=System.currentTimeMillis();	//LOG的属性放在一个类中	public Logging logging=new Logging();	public Logger()	{		}		public Logger(String logName)	{		this.logging.logName=logName;		}	public Logger(Class clazz)	{		this.logging.logName=clazz.getName();	}		/**初始化LOG的时候,先在配置文件中找,有他的配置文件	 *则用配置文进行初始化,没有的化就进存放LOG的队列寻找	 *有没有父LOG,最后是使用根LOG的配置	 */		public static Logger getLogger(Class clazz) 	{		String loggerName=clazz.getName();		if(Manager.findit(loggerName))		{						return Manager.findThis(loggerName);		}		else		{			return Manager.findParent(loggerName);		}			}	public static Logger getLogger(String loggerName) 	{				if(Manager.findit(loggerName))		{			return Manager.findThis(loggerName);		}		else		{			return Manager.findParent(loggerName);		}			}	//LOG的输出的方法,LOG输出什么级别的什么信息	public void log(Level level, String message)	{		if(level.levelVal>=this.logging.level.levelVal)		{			StringWriter sw=new StringWriter();			PrintWriter	 pw=new PrintWriter(sw);			new Exception().printStackTrace(pw);			StringBuffer sb=sw.getBuffer();			String methodCallString=sb.toString();			layout(this,methodCallString,message);		}	}	/**	 *设置LOG的级别	 *如果有子LOG,	 *且子LOG的该属性未被配置,则也修改子LOG的LEVEL属性	 */ 		public void setLevel(Level level)	{		this.logging.level=level;		this.logging.initLevel=true;				Logger p=null;		int i=0;		for(;i<Que.que.length();i++)		{			if(((Logger)Que.que.elementAt(i)).logging.logName==this.logging.logName)			{				p=(Logger)Que.que.elementAt(i);				break;			}		}			i++;			if(i>=Que.que.length())			return;		Logger s=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,s)&&(s.logging.initLevel==false))		{			s.logging.level=p.logging.level;		}		i++;		if(i>=Que.que.length())		return;		Logger ss=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,ss)&&(ss.logging.initLevel==false))		{			if(!(Manager.isParent(s,ss)&&s.logging.initLevel))			do			{				ss.logging.level=p.logging.level;				i++;				if(i>=Que.que.length())				return;				ss=(Logger)Que.que.elementAt(i);			}while(Manager.isParent(p,ss)&&(ss.logging.initLevel==false));				}	}		/**	 *设置LOG的输出格式	 *如果有子LOG,	 *且子LOG的该属性未被配置,则也修改子LOG的格式属性	 */ 	public void setFormat(String logFormat)	{		this.logging.logFormat=logFormat;		this.logging.initFormat=true;				Logger p=null;		int i=0;		for(;i<Que.que.length();i++)		{			if(((Logger)Que.que.elementAt(i)).logging.logName==this.logging.logName)			{				p=(Logger)Que.que.elementAt(i);				break;			}		}			i++;			if(i>=Que.que.length())			return;		Logger s=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,s)&&(s.logging.initFormat==false))		{			s.logging.logFormat=p.logging.logFormat;		}		i++;		if(i>=Que.que.length())		return;			Logger ss=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,ss)&&(ss.logging.initFormat==false))		{			if(!(Manager.isParent(s,ss)&&s.logging.initFormat))			do			{				ss.logging.logFormat=p.logging.logFormat;				i++;				if(i>=Que.que.length())				return;				ss=(Logger)Que.que.elementAt(i);			}while(Manager.isParent(p,ss)&&(ss.logging.initFormat==false));		}					}		/**	 *设置LOG的输出方式,文件还是屏幕	 *如果有子LOG,	 *且子LOG的该属性未被配置,则也修改子LOG的输出属性	 */ 	 public void setDestination(String logDestination) 	 {	 	this.logging.logDestination=logDestination;		this.logging.initDestination=true;				Logger p=null;		int i=0;		for(;i<Que.que.length();i++)		{			if(((Logger)Que.que.elementAt(i)).logging.logName==this.logging.logName)			{				p=(Logger)Que.que.elementAt(i);				break;			}		}			i++;			if(i>=Que.que.length())			return;		Logger s=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,s)&&(s.logging.initDestination==false))		{			s.logging.logDestination=p.logging.logDestination;		}		i++;		if(i>=Que.que.length())		return;		Logger ss=(Logger)Que.que.elementAt(i);		if(Manager.isParent(p,ss)&&(ss.logging.initDestination==false))		{			if(!(Manager.isParent(s,ss)&&s.logging.initDestination))			do			{				ss.logging.logDestination=p.logging.logDestination;				i++;				if(i>=Que.que.length())				return;				ss=(Logger)Que.que.elementAt(i);			}while(Manager.isParent(p,ss)&&(ss.logging.initDestination==false));		}					 }		/**	 *定义排序的规则	 *按日志的名字进行排序	 */	public   int   compareTo(Object   o)     	{     		Logger   lg=(Logger)o;     		return   this.logging.logName.compareTo(lg.logging.logName);       	}   	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -