/ ** * */ import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; /** * @author magic282 * */public class logger {private static string logfilepath; 개인 정적 부울 isinitialized = false; 개인 정적 FILEWRITER LOGWRITER = NULL; 개인 정적 부울 PrintLog whenLog = true; 개인 정적 부울 initlogger () {문자열 logdirectorypath = system.getProperty ( "user.dir") + java.io.file.separatorchar + "log"; if (! 새 파일 (logdirectoryPath) .exists ()) {새 파일 (logdirectoryPath) .mkdir (); } date logfiledate = 새 날짜 (); simpledateformat dateformat = new simpledateformat ( "yyyy-mm-dd-hh-mm-ss"); logfilepath = logdirectorypath + java.io.file.separatorchar + dateformat.format (logfiledate) + ".log"; try {logwriter = new filewriter (logfilepath, true); isinitialized = true; } catch (ioexception e) {// todo 자동 생성 캐치 블록 시스템.err.println ( "로그 파일을 만들 수 없음"); System.err.println ( "시작 실패"); e.printstacktrace (); 거짓을 반환합니다. } true를 반환합니다. } public static void log (문자열 메시지) {if (! isinitialized) {initlogger (); } date logfiledate = 새 날짜 (); simpledateformat dateformat = new simpledateformat ( "yyyy-mm-dd-hh-mm-ss"); 문자열 callingclassName = new Exception (). getStackTrace () [1] .getClassName (); Synchronized (logwriter) {String log = string.format ( "[%s] @ [%s] :%s/n", CallingClassName, dateformat.format (logfiledate), message); if (printlogwhenlog) {system.out.printf ( "[log] :%s", log); } try {logwriter.write (log); logwriter.flush (); } catch (ioexception e) {// todo 자동 생성 캐치 블록 시스템.err.println ( "File %S 오류에 로그를 쓰기"); e.printstacktrace (); }}} public static void log (예외 예외) {if (! isinitialized) {initlogger (); } date logfiledate = 새 날짜 (); simpledateformat dateformat = new simpledateformat ( "yyyy-mm-dd-hh-mm-ss"); 문자열 callingclassName = new Exception (). getStackTrace () [1] .getClassName (); synchronized (logwriter) {string log = string.format ( "[%s] @ [%s] :%s/n", callingclassName, dateformat.format (logfiledate), exception.toString ()); if (printlogwhenlog) {system.out.printf ( "[log] :%s", log); } try {logwriter.write (log); logwriter.flush (); } catch (ioexception e) {// todo 자동 생성 캐치 블록 시스템.err.println ( "File %S 오류에 로그를 쓰기"); e.printstacktrace (); }}}}