java自定義日志輸出


public class LogUtil
{
/**
* 將信息寫入到日志
*/
public static boolean writeLog(String className,String ErrorInfo,String ErrorContent){
String content=info(className, ErrorInfo, ErrorContent);
File fileName = new File(System.getProperty("user.dir")+"/config/log.log");
RandomAccessFile mm = null;
boolean flag = false;
FileOutputStream o = null;
try {
o = new FileOutputStream(fileName,true);
o.write(content.getBytes("utf-8"));
o.close();
flag = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
return flag;
}

/**
*
* @param className 類名稱
* @param ErrorInfo 錯誤信息
* @param ErrorContent 錯誤內容
* @return
*/
public static String info(String className,String ErrorInfo,String ErrorContent){
StringBuffer buffer=new StringBuffer();
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = sd.format(date)+"\r\n";
buffer.append(d);
buffer.append("錯誤類名為:").append(className).append("\r\n");
buffer.append("錯誤信息為:").append(ErrorInfo).append("\r\n");
buffer.append("錯誤內容為:").append(ErrorContent).append("\r\n");
buffer.append("\r\n");
return buffer.toString();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM