/ ** * */ impor java.io.file; import java.io.filewriter; import java.io.ioException; import java.text.simpledateFormat; impor java.util.date; /** * @author magic282 * */Logger kelas publik {private static string logfilePath; Private static boolean isInitialized = false; Logwriter FileWriter Privat = NULL; private static boolean printlogwhenlog = true; private static boolean initlogger () {string logDirectoryPath = system.getProperty ("user.dir") + java.io.file.separatorchar + "log"; if (! baru file (logDirectoryPath) .exists ()) {file baru (logDirectoryPath) .mkdir (); } Tanggal logfiledate = tanggal baru (); SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy-mm-dd-hh-mm-ss"); logfilePath = logDirectoryPath + java.io.file.separatchar + dateFormat.format (logfiledate) + ".log"; coba {logwriter = FileWriter baru (logFilePath, true); isInitialized = true; } catch (ioException e) {// TODO Auto-Eynerated Catch Block System.err.println ("Tidak dapat membuat file log."); System.err.println ("Inisiilisasi Gagal."); e.printstacktrace (); mengembalikan false; } return true; } public static void log (pesan string) {if (! isInitialized) {initlogger (); } Tanggal logfiledate = tanggal baru (); SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy-mm-dd-hh-mm-ss"); String callingClassName = new Exception (). GetStackTrace () [1] .getClassName (); disinkronkan (logwriter) {string log = string.format ("[%s] @ [%s]:%s/n", callingClassName, dateFormat.format (logfiledate), pesan); if (printlogwhenlog) {System.out.printf ("[log]:%s", log); } coba {logwriter.write (log); logwriter.flush (); } catch (ioException e) {// TODO Auto-Eynerated Catch Block System.err.println ("Tulis Log ke File %S Kesalahan."); e.printstacktrace (); }}} public static void log (pengecualian pengecualian) {if (! isInitialized) {initlogger (); } Tanggal logfiledate = tanggal baru (); SimpleDateFormat DateFormat = new SimpleDateFormat ("yyyy-mm-dd-hh-mm-ss"); String callingClassName = new Exception (). GetStackTrace () [1] .getClassName (); disinkronkan (logwriter) {string log = string.format ("[%s] @ [%s]:%s/n", callingClassName, dateFormat.format (logfiledate), exception.toString ()); if (printlogwhenlog) {System.out.printf ("[log]:%s", log); } coba {logwriter.write (log); logwriter.flush (); } catch (ioException e) {// TODO Auto-Eynerated Catch Block System.err.println ("Tulis Log ke File %S Kesalahan."); e.printstacktrace (); }}}}