⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logentry.java

📁 本文论述了一个前台笔记本销售系统的开发过程
💻 JAVA
字号:
package com.set.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;

/**
 * LogEntry represents log message entry in the application. It contains all the
 * information that are to be logged.
 * 
 * @author Oliver Yip
 * @version 1.0, 11/04/03
 * @since 1.0
 * @see LogManager
 */
public class LogEntry {
	/**
	 * No logging at all
	 * 
	 */
	public static final int LOGTYPE_NONE = 0;

	/**
	 * information type logging
	 * 
	 */
	public static final int LOGTYPE_INFO = 1;

	/**
	 * error type logging
	 * 
	 */
	public static final int LOGTYPE_ERROR = 2;

	/**
	 * fatal error type logging
	 * 
	 */
	public static final int LOGTYPE_FATAL = 3;

	/**
	 * user ID for log message
	 * 
	 */
	public static final int LOGDATA_USERID = 0;

	/**
	 * type of logging: information, error or fatal error
	 * 
	 */
	public static final int LOGDATA_TYPE = 1;

	/**
	 * log message
	 * 
	 */
	public static final int LOGDATA_MESSAGE = 2;

	/**
	 * exception associated with the log message, if any
	 * 
	 */
	public static final int LOGDATA_EXCEPTION = 3;

	/**
	 * exception trace associated with the log message, if any
	 * 
	 */
	public static final int LOGDATA_EXCEPTIONTRACE = 4;

	/**
	 * time of logging
	 * 
	 */
	public static final int LOGDATA_LOGTIME = 5;

	private String userID;

	private int type;

	private String logMessage;

	private Exception ex;

	private String trace;

	private Timestamp logTime;

	/**
	 * constructs a <code>LogEntry</code> object.
	 * 
	 * @param type
	 *            type of log message. Should be one of the LOGTYPE_XXX values
	 * @param logMessage
	 *            log message
	 * @param userID
	 *            user ID for the logging
	 * @param logTime
	 *            time stamp for the logging
	 * @param excp
	 *            exception associated with the log message, if any
	 * 
	 */
	public LogEntry(int type, String logMessage, String userID,
			Timestamp logTime, Exception excp) {
		this.type = type;
		this.logMessage = logMessage;
		this.userID = userID;
		this.ex = excp;
		this.trace = LogEntry.getTraceInfo(ex);
		this.logTime = logTime;
	}

	/**
	 * constructs a <code>LogEntry</code> object.
	 * 
	 * 
	 */
	public LogEntry() {
	}

	/**
	 * returns the type of log message. Should be one of the LOGTYPE_XXX values
	 * 
	 * @return type of log message. Should be one of the LOGTYPE_XXX values
	 * 
	 */
	public int getType() {
		return type;
	}

	/**
	 * specifies the type of log message. Should be one of the LOGTYPE_XXX
	 * values
	 * 
	 * @param int
	 *            type of log message. Should be one of the LOGTYPE_XXX values
	 * 
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * returns the log message
	 * 
	 * @return the log message
	 * 
	 */
	public String getLogMessage() {
		return this.logMessage;
	}

	/**
	 * specifies the log message
	 * 
	 * @param logMessage
	 *            log message
	 * 
	 */
	public void setLogMessage(String logMessage) {
		this.logMessage = logMessage;
	}

	/**
	 * returns the exception associated with the log message, if any
	 * 
	 * @return exception associated with the log message, if any
	 * 
	 */
	public Exception getException() {
		return this.ex;
	}

	/**
	 * specifies the exception associated with the log message, if any
	 * 
	 * @param excp
	 *            exception associated with the log message, if any
	 * 
	 */
	public void setException(Exception excp) {
		this.ex = excp;
		this.trace = LogEntry.getTraceInfo(ex);
	}

	/**
	 * returns the exception stack trace associated with the log message, if any
	 * 
	 * @return exception stack trace associated with the log message, if any
	 * 
	 */
	public String getExceptionTrace() {
		return this.trace;
	}

	/**
	 * returns the user ID for the log message
	 * 
	 * @return user ID for the logging
	 * 
	 */
	public String getUserID() {
		return this.userID;
	}

	/**
	 * specifies the user ID for the log message
	 * 
	 * @param userID
	 *            user ID for the logging
	 * 
	 */
	public void setUserID(String userID) {
		this.userID = userID;
	}

	/**
	 * returns the time stamp for the logging
	 * 
	 * @return time stamp for the logging
	 * 
	 */
	public Timestamp getLogTime() {
		return logTime;
	}

	/**
	 * specifies the time stamp for the logging
	 * 
	 * @param logTime
	 *            time stamp for the logging
	 * 
	 */
	public void setLogTime(Timestamp logTime) {
		this.logTime = logTime;
	}

	/**
	 * returns the stack trace for an exception
	 * 
	 * @param excp
	 *            exception to return the stack trace
	 * @return the stack trace for the exception
	 * 
	 */
	public static String getTraceInfo(Exception excp) {
		String trace = "";
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			PrintStream ps = new PrintStream(new BufferedOutputStream(os));
			excp.printStackTrace(ps);
			ps.flush();
			trace = os.toString();
			os.close();
			ps.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return trace;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -