学习 JavaWeb项目开发案例精粹14(新闻发布系统)之三

这一节写Log日志类,在原JavaPrj_14里用的是debug类,我觉得里面其实最主要的功能就是Log我就改了一下名字。
package com.ppcms.common;

import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Properties;

public class Log {
	public Log() {
		
	}
	
	public static PrintWriter getLogStream(String logFileName) {
		PrintWriter pw = null;
		SimpleDateFormat _sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
		String CurrentTime = null;
		try{
			File CurrentFile = new File(logFileName);
			if(CurrentFile.length() >1000000L){
				File backupFile = new File(logFileName+".bak");
				if(backupFile.exists()){
					CurrentTime = _sdf.format(new Date(System.currentTimeMillis()));
					File backupRenameFile = new File(logFileName+".bak"+CurrentTime);
					try{
						backupFile.renameTo(backupRenameFile);
					}catch(Exception e){
						e.printStackTrace();
					}
				}
				try{
					CurrentFile.renameTo(backupFile);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			
			logNo++;
			FileOutputStream fos = new FileOutputStream(logFileName,true);
			BufferedOutputStream bufos = new BufferedOutputStream(fos);
			pw = new PrintWriter(bufos, true);
			pw.print("\n");
		}catch(Exception e){
			// e.printStackTrace();
			pw = new PrintWriter(System.err,true);
			_sdf = new SimpleDateFormat("'In' yyyyMMdd 'at' HH a mm 'minutes' ss 'seconds' ");
			CurrentTime = _sdf.format(new Date(System.currentTimeMillis()));
			pw.print("\n");
			pw.print(CurrentTime + ":" + " Exception Occured While Trying To Open Log File: " + logFileName + ".[" + e.toString() + "]");
		}
		return pw;
	}

	public void wirteLog(String info){
		String isOn = "";
		Properties p = EnvironmentConfig.getInstance().getProperties("/SiteConf/conf.properties");
		isOn = p.getProperty("isLog");
		if(isOn == null || isOn.trim()==""||!isOn.trim().equalsIgnoreCase("on")&&!isOn.trim().equalsIgnoreCase("ture")){
			isOn = "off";
		}
		String logPath = p.getProperty("logPath");
        if(logPath == null || logPath.trim().equalsIgnoreCase(""))
            logPath = "/log/";
        logPath.replace('\\', '/');
        if(!logPath.endsWith(File.separator))
            logPath = logPath + File.separator;
        String logFile = p.getProperty("logFile");
        if(logFile == null || logFile.trim().equalsIgnoreCase(""))
            logFile = "Site.log";
        String logFileName = logPath + logFile;
        SimpleDateFormat _dateFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String currentTime = null;
        PrintWriter pw = null;
        if(isOn.equalsIgnoreCase("true") || isOn.trim().equalsIgnoreCase("on"))
        {
            pw = getLogStream(logFileName);
            _dateFormatter = new SimpleDateFormat("'On' yyyyMMdd 'at' HH:mm:ss '---'");
            currentTime = _dateFormatter.format(new Date(System.currentTimeMillis()));
            pw.println(currentTime + " Debug Starts No " + String.valueOf(logNo) + " ************");
            pw.println("\n");
            pw.println(info);
            pw.flush();
            pw.close();
        }
	}
	public static int logNo = 0;
}
主要有两个方法。public static PrintWriter getLogStream(String logFileName) 利用传入的logFileName 返回PrintWriter
FileOutputStream fos = new FileOutputStream(logFileName,true);
BufferedOutputStream bufos = new BufferedOutputStream(fos);

pw = new PrintWriter(bufos, true);
返回PrintWriter pw ,其它的其实就是备份日志文件。

另一个方法是public void wirteLog(String info)通过EnvironmentConfig类来获取是否写日志,以及日志文件在那里。以及向日志文件中写的内容。

你可能感兴趣的:(学习 JavaWeb项目开发案例精粹14(新闻发布系统)之三)