GetS项目——日志处理

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());
		}
	}
}

 

你可能感兴趣的:(sql,db2)