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

📄 layoutformat.java

📁 设计模式关于LOG的CLL系统
💻 JAVA
字号:
/*
 *控制日志输出到控制台还是文本
 *解析格式,按其要求输出
 */
package cn.edu.nju.software.sd.cll;
import java.util.*;
import java.io.*; 
import java.text.SimpleDateFormat;
//得到格式要输出的信息,并输出到正确的地点
public class LayoutFormat
{
	public void layout(Logger log,String methodCallString,String message)
	{
		boolean writeToFile=false;
		PrintWriter output=null;
		String outputToFile=null;
		String logDestination=log.logging.logDestination;
		String destination=getDestination(logDestination);
		if(!destination.toUpperCase().equals("CONSOLE"))
		{
			
			try
			{
				output=new PrintWriter(new FileOutputStream(destination,true));
			}
			catch(FileNotFoundException e)
			{
				System.out.println("CREAT OR OPEN"+destination+"ERROR");
			}
			writeToFile=true;
		}
		
		String logFormat=log.logging.logFormat;
		String layout=null;
		if(logFormat.indexOf("%n")>0)
		{
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				System.out.print(log.logging.logName+"-");
			}
			
			if(writeToFile)
			{
				outputToFile=log.logging.logName;
				output.print(outputToFile+"-");
			}
			
		}
		
		if(logFormat.indexOf("%e")>0)
		{
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				System.out.print(log.logging.level.levelName+"-");
			}
			if(writeToFile)
			{
				outputToFile=log.logging.level.levelName;
				output.print(outputToFile+"-");
			}
	
		}

		if(logFormat.indexOf("%f")>0)
		{
			String methodCall=getMethodCall(methodCallString);
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				System.out.print(methodCall+"-");
			}
			if(writeToFile)
			{
				outputToFile=methodCall;
				output.print(outputToFile+"-");
			}
			
		}

		if(logFormat.indexOf("%s")>0)
		{
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				System.out.print(df.format(new Date())+"-");
			}
			if(writeToFile)
			{
				String s;
				s=df.format(new Date());
				output.print(s+"-");
			} 
      		 
		}
		if(logFormat.indexOf("%t")>0)
		{
			long timeEnd=System.currentTimeMillis();
			timeEnd=timeEnd-Logger.timeStart;
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				
				System.out.print(timeEnd+"-");
			}
			if(writeToFile)
			{
				output.print(timeEnd+"-");
		
			}
			
			
		}
		if(logFormat.indexOf("%m")>0)
		{
			if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
			{
				System.out.print(message+" ");
			}
			if(writeToFile)
			{
				output.print(message+" ");
			}
		}
		if(logDestination.toUpperCase().indexOf("CONSOLE")>=0)
		System.out.println();
		if(writeToFile)
		{
			output.println();
			output.close();
		}
		
		
	}
	
//得到调用LOG的函数
	public String getMethodCall(String methodCallString)
	{
		String methodCall=null;
		String getString=null;
		StringTokenizer callString=new StringTokenizer(methodCallString," (\n");
		while(callString.hasMoreTokens())
		{
			getString=callString.nextToken();
			if(getString.indexOf("Logger.log")>0)
			{
				getString=callString.nextToken();
				getString=callString.nextToken();
				getString=callString.nextToken();
				break;
			}
			
		}
		callString=new StringTokenizer(getString,".");
		while(callString.hasMoreTokens())
		{
			methodCall=callString.nextToken();
		}
		return methodCall;
		
	}
	//得到LOG的保存文件地点
	public String getDestination(String logDestination)
	{
		String destinations;
		StringTokenizer callString=new StringTokenizer(logDestination,",");
		destinations=callString.nextToken();
		return destinations;
	}
	
}

⌨️ 快捷键说明

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