Posted on 2009-05-10 20:43
Gavin.lee 阅读(873)
评论(0) 编辑 收藏 所属分类:
Log && File Operate
这是我在500wan时候,手机wap项目中用到的日志类,很简单,只是记录报文。方便检查。
(有所更新 version 1.1)
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");
}
}
