package com.gobusiness.eus.util;
import java.io.*;
import java.util.Vector;
import java.sql.SQLException;
import com.gobusiness.eus.exception.EusException;
/*
* Global e-Business Services Limited
* GETS - End User Solutions
*
* Program Unit ID: PU-EUS-001
* Description: A class to insert File log
*
* Attribute:
* bwWriter - BufferedWriter to write to file
*
* Method:
*
* logDebug - Log debug message when log.mode=Debug with debug message only
* logDebug - Log debug message when log.mode=Debug with appName and debug message
* logMsg - Log the message to file
* logErr - Log the message to file with EusEception Error Code List
* logErr - Log the message to file with Exception message
* logSQLErr - Log the message to file with SQL exception message
*
* Revision History:
*
* Author Date Details
* --------- ---------- -------
* Lily Ling 2003-02-26 Create
* Karen Shiu 2004-12-28 Fix open too many files problem
*
*/
/**
* @author Lily Ling
* @version 0.1
*/
public class Logger {
private static BufferedWriter bwWriter;
private static boolean isDebug = (SysProp.getProperty("log.mode")
.equals("Debug")) ? true : false;
// Added by Karen Shiu on 20041228: Fix too many open file
private static String WritingToLogFile = "";
/**
* Init BufferedWriter for printing - Get Log File Name from properties -
* Get System Date - Append system date to log file - Init BufferedWriter
* for printing
*/
private static boolean init() {
synchronized (Logger.class) {
// Get system date
String sLogDate = DateUtil.getNowString(Constants.DB2_DTTM_FMT);
// Get log file name
String sLogFile = SysProp.getProperty("log.file");
// get the full path of System Log File
sLogFile = EusUtil.getFullPath(sLogFile);
// Append system date to log file
StringBuffer sbLogFile = new StringBuffer(sLogFile.toString());
sbLogFile.append("." + sLogDate.substring(0, 10));
// Modified by Karen Shiu on 20041228: : Fix open too many files
if (sbLogFile.toString().equals(WritingToLogFile)) {
return true;
}
// End 20041228
boolean isSuccess = true;
// Init Buffered Writer for printing
try {
File file = new File(sLogFile);
String sLogPath = file.getParent();
File filePath = new File(sLogPath);
isSuccess = filePath.exists();
if (!isSuccess) {
// Create a directory; all non-existent ancestor directories
// are automatically created
isSuccess = filePath.mkdirs();
if (!isSuccess) {
System.out.println("Directory creation failed - "
+ sLogPath);
} else {
System.out.println("Directory creation success - "
+ sLogPath);
}
}
if (isSuccess) {
// Modified by Karen Shiu on 20041228 : Fix open too many
// files
File fileLog = new File(sbLogFile.toString());
// boolean bNewFile = (fileLog.isFile()) ? false : true;
bwWriter = new BufferedWriter(new FileWriter(sbLogFile
.toString(), true));
WritingToLogFile = sbLogFile.toString();
/*
* if (bNewFile) { //System.out.println( //
* sbLogFile.toString() + " - Create file"); } else {
* //System.out.println(sbLogFile.toString() + " - Append
* file"); }
*/
// End 20041228
}
} catch (IOException ioexp) {
System.out.println("IOException - init " + ioexp.getMessage());
} catch (Exception e) {
System.out.println("Exception - init " + e.getMessage());
} finally {
return isSuccess;
}
}
}
/**
* Check the debug level in property file.
*
* @return true for log.mode=Debug
* @return false for log.mode=Normal
*/
private static boolean isDebug() {
return isDebug;
}
public static void setDebug(boolean iDebugMode) {
isDebug = iDebugMode;
}
/**
* Format first line
*
* @param appName
* The application, module or class calling this method
*/
private static void format1Line(String appName) {
try {
if (appName == null)
appName = "";
String logName = appName.substring(appName.lastIndexOf(".") + 1);
if (logName.equals(""))
logName = appName;
String outstr = "<<" + DateUtil.getNowString(Constants.DT_FMT)
+ ">> (" + logName + ") ";
bwWriter.newLine();
bwWriter.write(outstr);
} catch (IOException ioexp) {
System.out.println("IOException - format1Line " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out.println("Exception - format1Line " + appName
+ e.getMessage());
}
}
/**
* Print debug messages to logfile. Only print when log.mode=Debug
*
* @param appName
* The application, module or class calling this method
* @param msg
* The debug message to be printed out.
*/
public static void logDebug(String appName, String msg) {
if (isDebug) {
try {
if (init()) {
synchronized (Logger.class) {
format1Line(appName);
bwWriter.write("logDebug : ");
bwWriter.write(msg + "");
bwWriter.flush();
}
}
} catch (IOException ioexp) {
System.out.println("IOException - logDebug " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out.println("Exception - logDebug " + appName
+ e.getMessage());
}
}
}
/**
* Print debug messages to logfile. Only print when log.mode=Debug
*
* @param msg
* The debug message to be printed out.
*/
public static void logDebug(String msg) {
logDebug("Debug", msg);
}
/**
* Print messages to logfile.
*
* @param appName
* The application, module or class calling this method
* @param msg
* The message to be printed out
*/
public static void logMsg(String appName, String msg) {
try {
if (init()) {
synchronized (Logger.class) {
format1Line(appName);
bwWriter.write("LogMsg : ");
bwWriter.write(msg + "");
bwWriter.flush();
}
}
} catch (IOException ioexp) {
System.out.println("IOException - logMsg " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out
.println("Exception - logMsg " + appName + e.getMessage());
}
}
/**
* EusException handling routine for generic errors.
*
* @param appName
* The application, module or class calling this method
* @param errMsg
* The error message to be printed out
* @param e
* The EusException object for this error
*/
public static void logErr(String appName, String errMsg, EusException logExp) {
try {
if (init()) {
synchronized (Logger.class) {
format1Line(appName);
bwWriter.write("ErrorMsg : ");
bwWriter.write(errMsg + "");
bwWriter.newLine();
if (logExp == null)
bwWriter.write("logErr - Null from EusException");
else {
Vector errList = logExp.getErrList();
for (int i = 0; i < errList.size(); i++) {
bwWriter.write("ErrList(" + i + "): ");
bwWriter.write(EusUtil.getErrMsgEn((String) errList
.elementAt(i)));
bwWriter.newLine();
}
}
bwWriter.flush();
}
}
} catch (IOException ioexp) {
System.out.println("IOException - logErr " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out
.println("Exception - logErr " + appName + e.getMessage());
}
}
/**
* Exception handling routine for generic errors.
*
* @param appName
* The application, module or class calling this method
* @param errMsg
* The error message to be printed out
* @param e
* The Exception object for this error
*/
public static void logErr(String appName, String errMsg, Exception logExp) {
try {
if (init()) {
synchronized (Logger.class) {
format1Line(appName);
bwWriter.write("ErrorMsg : ");
bwWriter.write(errMsg + "");
bwWriter.newLine();
if (logExp == null)
bwWriter.write("logErr - Null from Exception");
else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
logExp.printStackTrace(pw); // put exception information into stream
bwWriter.write("ExceptionMsg: ");
bwWriter.write(sw.toString()); // write into log file
}
bwWriter.flush();
}
}
} catch (IOException ioexp) {
System.out.println("IOException - logErr " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out
.println("Exception - logErr " + appName + e.getMessage());
}
}
/**
* Exception handling routine for SQL errors.
*
* @param appName
* The application, module or class calling this method
* @param errMsg
* The error message to be printed out
* @param e
* The SQLException object for this error
*/
public static void logSQLErr(String appName, String errMsg,
SQLException logExp) {
try {
if (init()) {
synchronized (Logger.class) {
format1Line(appName);
bwWriter.write("ErrorMsg : ");
bwWriter.write(errMsg + "");
bwWriter.newLine();
if (logExp == null)
bwWriter.write("logSQLErr - Null from SQLException");
else {
bwWriter.write("SQLErrorCode : ");
bwWriter.write(logExp.getErrorCode() + "");
bwWriter.newLine();
bwWriter.write("SQLState : ");
bwWriter.write(logExp.getSQLState() + "");
bwWriter.newLine();
bwWriter.write("SQLErrorMsg : ");
bwWriter.write(logExp.getMessage() + "");
}
bwWriter.flush();
}
}
} catch (IOException ioexp) {
System.out.println("IOException - logSQLErr " + appName
+ ioexp.getMessage());
} catch (Exception e) {
System.out.println("Exception - logSQLErr " + appName
+ e.getMessage());
}
}
}
分享到:
相关推荐
TCL(Tool Command Language)是一种强大的脚本语言,由John Ousterhout教授在1988年开发,主要用于自动化任务、系统管理、GUI编程以及...通过学习这些内容,你将能够掌握TCL的基本技能,并能在实际项目中灵活运用。
* 海量日志数据,提取出某日访问百度次数最多的那个IP(大数据处理) * 有10个文件,每个文件1G,每个文件的每一行都存放的是用户的query,每个文件的query都可能重复。如何按照query的频度排序?(大数据处理) ...
- 使用错误处理机制:如设置返回值、抛出异常或记录日志,以便在发生错误时能及时发现并处理。 - 避免错误传播:确保错误处理代码不会将错误状态传递到其他部分,除非这是预期行为。 3. 数据访问: - 避免缓冲区...
### Oracle培训讲义知识点梳理——性能分析与调整 #### 一、Oracle SQL执行计划解析 在Oracle数据库中,SQL执行计划对于理解SQL语句如何被执行至关重要。它详细地描述了数据库优化器为执行特定SQL语句所选择的路径...
【课程实践作业四1】是东南大学能源与环境学院针对Python编程的一次实践任务,源自Guttag, John的著作《Introduction to Computation and Programming Using Python》中的两个章节——6.2.3 "When the Going Gets ...
- **Tcl (Tutorial for Cool Languages) for Tcl/Tk**:这个标题直接指出了文档的主要内容——一个针对Tcl 和 Tk 的详尽教程。Tcl(Tool Command Language)是一种用于脚本编写的强大语言,而Tk则是与之配套的图形...