/ ** * */ import java.io.file; import java.io.filewriter; import java.io.io.ioexception; import java.text.simpledateformat; import java.util.date; /** * @author Magic282 * */public class logger {private static String logFilePath;私有静态布尔值isInitialized = false;私有静态文件作者logwriter = null;私有静态布尔值printLogWhenLog = true; private static boolean initlogger(){字符串logdirectorypath = system.getProperty(“ user.dir”) + java.io.file.file.separator.separatorchar +“ log”; if(!new file(logDirectoryPath).exists()){new file(logDirectoryPath).mkdir(); } date logfiledate = new Date(); SimpleDateFormat DateFormat =新的SimpleDateFormat(“ Yyyy-MM-DD-HH-MM-SS”); logFilePath = logDirectoryPath + java.io.file.file.separatorchar + dateformat.format(logfiledate) +“ .log”;尝试{logwriter = new filewriter(logfilepath,true); isInitialized = true; } catch(ioexception e){// todo自动生成的捕获块system.err.println(“无法创建日志文件。”); system.err.println(“ initirization Fail。”); e.printstacktrace();返回false; }返回true; } public static void log(字符串消息){if(!isInitialized){initlogger(); } date logfiledate = new Date(); SimpleDateFormat DateFormat =新的SimpleDateFormat(“ Yyyy-MM-DD-HH-MM-SS”);字符串calledClassName = new exception()。getStackTrace()[1] .getClassName();同步(logwriter){字符串log = string.format(“ [%s] @ [%s]:%s/n“,calleClassName,dateformat.format(logfiledate),message); if(printLogWhenLog){System.out.printf(“ [log]:%s”,log); }尝试{logwriter.write(log); logwriter.flush(); } catch(ioexception e){// todo自动生成的捕获块system.err.println(“将日志写入文件%s错误。”); e.printstacktrace(); }}}} public static void log(异常异常){if(!isInitialized){initlogger(); } date logfiledate = new Date(); SimpleDateFormat DateFormat =新的SimpleDateFormat(“ Yyyy-MM-DD-HH-MM-SS”);字符串calledClassName = new exception()。getStackTrace()[1] .getClassName();同步(logwriter){字符串log = string.format(“ [%s] @ [%s]:%s/n“,calleClassName,dateformat.format(logfiledate),exception.toString()); if(printLogWhenLog){System.out.printf(“ [log]:%s”,log); }尝试{logwriter.write(log); logwriter.flush(); } catch(ioexception e){// todo自动生成的捕获块system.err.println(“将日志写入文件%s错误。”); e.printstacktrace(); }}}}}