這是我在500wan時候,手機wap項目中用到的日志類,很簡單,只是記錄報文。方便檢查。
package com.Gavin.tools.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**
* 一個簡單的日志類,啟動日志后,會在WebRoot下創建一個weglogs文件來存放日志。
* 該日志文件類能指定編碼讀、寫
* @author Gavin.lee
* @date 09-04-24 14:07pm
* @version 1.1
*/

public class WriteLog {
public synchronized static void writeLog(String className, String context) {
String projectPath = WriteLog.getProjectPath();
String dest_src = WriteLog.getSqe();
String datetime[] = WriteLog.getDate().split("\\\\");
//OK version 1.1
String filepath = projectPath;
String foldername = new StringBuffer("weblogs/").append(className).append("/")
.append(datetime[0]).append("/")
.append(datetime[1]).append("/")
.append(datetime[2]).toString();
String urlname = new StringBuffer(projectPath).append("weblogs/") .append(className).append("/")
.append(datetime[0]).append("/")
.append(datetime[1]).append("/")
.append(datetime[2]).append("/").append(dest_src).toString();
WriteLog.createFolder(filepath, foldername);
WriteLog.createFile(urlname, context);
}
public synchronized static void writeLog(String className, String request, String response) {
String projectPath = WriteLog.getProjectPath();
String dest_src = WriteLog.getSqe();
String datetime[] = WriteLog.getDate().split("\\\\");
//OK version 1.1
String filepath = projectPath;
String foldername = new StringBuffer("weblogs/").append(className).append("/")
.append(datetime[0]).append("/")
.append(datetime[1]).append("/")
.append(datetime[2]).toString();
String urlname = new StringBuffer(projectPath).append("weblogs/") .append(className).append("/")
.append(datetime[0]).append("/")
.append(datetime[1]).append("/")
.append(datetime[2]).append("/").append(dest_src).toString();
WriteLog.createFolder(filepath, foldername);
WriteLog.createFile(urlname + ".txt", request);
WriteLog.createFile(urlname + ".xml", response);
}
public static String getProjectPath() {// 找到 WebRoot
String path = WriteLog.class.getResource("/").getPath();
String projectPath = path.substring(0, path.length()-16);
return projectPath;
}
public static String getSqe(){// "200905090448454845"
String sqe="";
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmssms");
Date date = new Date();
sqe = sd.format(date);
return sqe;
}
public static String getDate(){ // "200905\\20090504\\2009050414"
SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
SimpleDateFormat sd2 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sd3 = new SimpleDateFormat("yyyyMMddHH");
Date date = new Date();
String sqe = new StringBuffer(sd.format(date)).append("\\")
.append(sd2.format(date)).append("\\")
.append(sd3.format(date)).toString();
return sqe; //
}
/**
* 創建目錄, 創建前,先檢查要創建的文件夾父文件夾是否存在。
* @param filePath 絕對路徑
* @param folderName 需要創建的文件夾(可以多級)
*/
public synchronized static void createFolder(String filePath, String folderName) {
try {
String[] st = folderName.split("/");
for (int i = 0; i < st.length; i++) {
filePath = filePath + st[i] + "/";
File file = new File(filePath);
File parentFile = new File(file.getParent());
if (!parentFile.exists()) {
parentFile.mkdir();
}
if (!file.exists()) {
file.mkdir();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @param filename 絕對路徑/文件
* @param content 需要寫入文件的內容
*/
public static void createFile(String filename, String content) {
FileOutputStream fos;
try {
fos = new FileOutputStream(filename);
byte str[] = content.getBytes();
fos.write(str);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("create file ERROR!");
} catch (IOException e) {
System.out.println("write file ERROR!");
}
}
/**
* 讀取文本文件內容
*
* @param filePathAndName
* 帶有完整絕對路徑的文件名
* @param encoding
* 文本文件打開的編碼方式
* @return 返回文本文件的內容
*/
public String readFileByEncoding(String filePathAndName, String encoding) {
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
String st = "";
try {
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if (encoding.equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding);
}
BufferedReader br = new BufferedReader(isr);
try {
String data = "";
while ((data = br.readLine()) != null) {
str.append(data + " ");
}
} catch (Exception e) {
str.append(e.toString());
}
st = str.toString();
} catch (IOException es) {
st = "";
}
return st;
}
/**
* @param filename 絕對路徑/文件,必須要求父文件夾
* @param content 需要寫入文件的內容
*/
public static void createFileByEncoding(String filename, String content, String encoding) {
try {
FileOutputStream fos = new FileOutputStream(filename);
OutputStreamWriter osr = new OutputStreamWriter(fos, encoding);
Writer out = new BufferedWriter(osr);
out.write(content);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
WriteLog.writeLog("WriteLog", "writeLog test");
}
}
(有所更新 version 1.1)












































































































































































































