?? log.java
字號:
package com.hoten.util;
import java.io.*;
import java.util.*;
/**
*
* <p>Title: LOG 日志記錄</p>
* <p>Description:
* 此類主要用來記錄系統中發生的重大事件,以及由于程序本身所產生的錯誤信息</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: hoten </p>
* @author lqf
* @version 1.0
*/
////////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////
public class Log{
/**
* 用來記錄系統重大事件
* @param msg 重大事件
* @param fileName 日志文件的路徑及名稱
*/
public synchronized static void printEvent(String msg,String fileName)
{
msg = new String( "時間:"+CTime.getTime(CTime.YYMMDDhhmmss) + " 事件消息: " + msg);
if(fileName!=null) printToFile(msg,fileName);
else print(msg);
return;
}
public synchronized static void writeFile(String msg,String fileName)
{
if(fileName!=null) printToFile(msg,fileName,false);
else print(msg);
return;
}
/**
* 記錄應程序本身發生的錯誤,主要給程序員觀察。
* @param e 一個Exception
* @param mobile 用戶手機號碼
* @param msg 用戶發送的消息
* @param fileName 日志文件的路徑及名稱
*/
public synchronized static void printError(Throwable e,String mobile,String msg,String fileName)
{
StringBuffer errors=new StringBuffer(100);
errors.append("時間:");
errors.append(CTime.getTime(CTime.YYMMDDhhmmssxxx));
errors.append(" 手機號碼:");
errors.append(mobile);
errors.append(" 消息:");
errors.append(msg);
errors.append(" Exception: ");
if(fileName!=null) {
printToFile(errors.toString().trim(),fileName);
try {
e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));//
}
catch (Exception ex) {
}
}
else print(errors.toString().trim());
return;
}
public synchronized static void printState(String msg,String fileName)
{
if(fileName!=null) {
printToFile(msg,fileName);
}
else print(msg);
return;
}
public synchronized static void printError(Throwable e,String msg,String fileName)
{
StringBuffer errors=new StringBuffer(100);
errors.append("時間:");
errors.append(CTime.getTime(CTime.YYMMDDhhmmssxxx));
errors.append("消息:");
errors.append(msg);
errors.append(" Exception: ");
if(fileName!=null) {
printToFile(errors.toString().trim(),fileName);
try {
e.printStackTrace(new PrintWriter(new FileWriter(fileName,true),true));//
}
catch (Exception ex) {
}
}
else print(errors.toString().trim());
return;
}
/**把錯誤消息打印到屏幕上
*
* @param msg 錯誤消息
*/
private static void print(String msg)
{
System.out.println(msg);
}
/**
* 把消息打印到指定文件
* @param msg 錯誤消息
* @param fileName 指定的文件
*/
private static void printToFile(String msg,String fileName) //打印到文件中
{
printToFile(msg,fileName,true);
}
private static void printToFile(String msg,String fileName,boolean flag) //打印到文件中
{
BufferedWriter mBufWriter = null;
try
{
if(!createFile(fileName)) return ;
FileWriter fileWriter = new FileWriter(fileName, flag);
mBufWriter = new BufferedWriter(fileWriter);
mBufWriter.write(msg);
mBufWriter.newLine();
mBufWriter.flush();
mBufWriter.close();
}
catch (Throwable e)
{
try { mBufWriter.close(); } catch (Throwable t) {};
}
return;
}
/**
* 用來創建文件和文件夾
* @param fileName 文件或文件夾名稱
* @return
* @throws IOException
* @throws Exception
*/
public static boolean createFile(String fileName)throws IOException ,Exception{
File file = new File(fileName);
if (file.exists()) /* does file exist? If so, can it be written to */
{
if (file.canWrite() == false)
return false;
}
else
{
String path = null; /* Does not exist. Create the directories */
int firstSlash = fileName.indexOf(File.separatorChar);
int finalSlash = fileName.lastIndexOf(File.separatorChar);
if (finalSlash == 0) { /* error, not valid path */ }
else if (finalSlash == 1) /* UNIX root dir */
{
path = File.separator;
}
else if (firstSlash == finalSlash)
{ /* for example c:\ Then make sure slash is part of path */
path = fileName.substring(0,finalSlash+1);
}
else
{ path = fileName.substring(0,finalSlash); }
File dir = new File(path);
dir.mkdirs();
}
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -