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

📄 mylogger.java

📁 用JSP开发的考勤系统 编译器为myeclipse
💻 JAVA
字号:
package xtu.cie.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyLogger {
	private static String RUNTIME_STATE = "run";
	private static String OUTPUT_TYPE = "screen";
	private static String LOGFILE_PATH = "d:/kq.log";
	private static Date currentDate = null;
	private static PrintWriter writer = null;
	private static SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
	private static PrintWriter getWriter(){
		if(writer == null){
			try {
				currentDate = new Date();
				writer = new PrintWriter(new File(LOGFILE_PATH+"/"+formater.format(currentDate)+".log"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
			Date newDate = new Date();
			if(currentDate == null || formater.format(currentDate).equals(formater.format(newDate))){
				currentDate = newDate;
				try {
					writer = new PrintWriter(new File(LOGFILE_PATH+"/"+formater.format(currentDate)+".log"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return writer;
	}
	private MyLogger(){
		//load the config file
	}
	public static MyLogger getLogger(){
		//Load ConfigFile
		return new MyLogger();
	}
	private void setRuntimeState(String runtimeState){
		if(runtimeState != null &&
				(runtimeState.equalsIgnoreCase("run") || runtimeState.equalsIgnoreCase("debug")))
			RUNTIME_STATE = runtimeState;
	}
	private void setOutputType(String outputType){
		if(outputType != null &&
				(outputType.equalsIgnoreCase("file") || outputType.equalsIgnoreCase("screen")))
			RUNTIME_STATE = outputType;
	}
	
	private void writeLog(String msg){
		//format the log message
		
		if(OUTPUT_TYPE.equals("sreen")){
			//print message on screen
			System.out.println(msg);
		}
		else if(OUTPUT_TYPE.equals("file")){
			//write message to file
			MyLogger.getWriter().println(msg);
		}
	}
	/*
	 * output the logMessage on screen while debugging the program
	 */
	public void debug(String logMessage){
		if(RUNTIME_STATE.equalsIgnoreCase("debug")){
			writeLog(logMessage);
		}
	}
	/*
	 * output the logMessage to log file
	 */
	public void log(String logMessage){
		writeLog(logMessage);
	}
	public static String getRUNTIME_STATE() {
		return RUNTIME_STATE;
	}
	public static void setRUNTIME_STATE(String runtime_state) {
		RUNTIME_STATE = runtime_state;
	}
	public static String getOUTPUT_TYPE() {
		return OUTPUT_TYPE;
	}
	public static void setOUTPUT_TYPE(String output_type) {
		OUTPUT_TYPE = output_type;
	}
	public static String getLOGFILE_PATH() {
		return LOGFILE_PATH;
	}
	public static void setLOGFILE_PATH(String logfile_path) {
		LOGFILE_PATH = logfile_path;
	}
}

⌨️ 快捷键说明

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