/ ** * */ นำเข้า java.io.file; นำเข้า java.io.fileWriter; นำเข้า java.io.ioException; นำเข้า java.text.simpledateFormat; นำเข้า java.util.date; /** * @Author Magic282 * */Logger คลาสสาธารณะ {สตริงคงที่ส่วนตัว LogFilePath; บูลีนคงที่ส่วนตัว isinitialized = false; LogWriter FileWriter ส่วนตัว = NULL; Private Static Boolean printlogwhenlog = true; บูลีนแบบคงที่ส่วนตัว initlogger () {สตริง logdirectorypath = system.getProperty ("user.dir") + java.io.file.Separatorchar + "log"; if (! ไฟล์ใหม่ (LogDirectoryPath) .Exists ()) {ไฟล์ใหม่ (LogDirectoryPath) .MkDir (); } วันที่ logfiledate = วันที่ใหม่ (); SimpledateFormat DateFormat = new SimpledateFormat ("YYYY-MM-DD-HH-MM-SS"); logFilePath = logDirectoryPath + java.io.file.Separatorchar + dateformat.format (logfiledate) + ".log"; ลอง {logWriter = ใหม่ fileWriter (logFilePath, true); isInitialized = true; } catch (ioexception e) {// todo catch block catch ที่สร้างขึ้นอัตโนมัติ System.err.println ("ไม่สามารถสร้างไฟล์บันทึกได้"); System.err.println ("การเริ่มต้นล้มเหลว"); E.PrintStackTrace (); กลับเท็จ; } return true; } บันทึกโมฆะคงที่สาธารณะ (ข้อความสตริง) {ถ้า (! isInitialized) {initlogger (); } วันที่ logfiledate = วันที่ใหม่ (); SimpledateFormat DateFormat = new SimpledateFormat ("YYYY-MM-DD-HH-MM-SS"); String CallingClassName = ข้อยกเว้นใหม่ (). getStackTrace () [1] .getClassName (); ซิงโครไนซ์ (logWriter) {string log = string.format ("[%s] @ [%s]:%s/n", callingclassName, dateformat.format (logfiledate), ข้อความ); if (printlogwhenlog) {system.out.printf ("[log]:%s", บันทึก); } ลอง {logWriter.write (บันทึก); logWriter.flush (); } catch (ioexception e) {// todo catch block catch ที่สร้างขึ้นอัตโนมัติ System.err.println ("เขียนบันทึกไปยังข้อผิดพลาด %S File"); E.PrintStackTrace (); }}} บันทึกโมฆะคงที่สาธารณะ (ข้อยกเว้นข้อยกเว้น) {ถ้า (! isInitialized) {initlogger (); } วันที่ logfiledate = วันที่ใหม่ (); SimpledateFormat DateFormat = new SimpledateFormat ("YYYY-MM-DD-HH-MM-SS"); String CallingClassName = ข้อยกเว้นใหม่ (). getStackTrace () [1] .getClassName (); ซิงโครไนซ์ (logWriter) {string log = string.format ("[%s] @ [%s]:%s/n", callingclassname, dateformat.format (logfiledate), exception.toString ()); if (printlogwhenlog) {system.out.printf ("[log]:%s", บันทึก); } ลอง {logWriter.write (บันทึก); logWriter.flush (); } catch (ioexception e) {// todo catch block catch ที่สร้างขึ้นอัตโนมัติ System.err.println ("เขียนบันทึกไปยังข้อผิดพลาด %S File"); E.PrintStackTrace (); -