/ ** * */ 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; private static boolean isInitialized = false; private static filewriter logwriter = null; private static boolean printlogwhenlog = true; private static boolean initlogger(){string logdirectorypath = system.getProperty( "user.dir") + java.io.file.separatorchar + "log"; if(!new file(logdirectorypath).exists()){new file(logdirectoryPath).mkdir(); } date logfiledate = new date(); SimpleDateFormat dateFormat = new SimpledateFormat( "yyyy-mm-dd-hh-mm-ss"); logfilepath = logdirectorypath + java.io.file.separatorchar + dateformat.format.format. + ".log"; try {logwriter = new filewriter(logfilepath、true); isInitialized = true; } catch(IOException e){// todo auto-enerated catch block system.err.println( "ログファイルを作成できません。"); system.err.println( "Intionization fail。"); e.printstacktrace(); falseを返します。 } trueを返します。 } public static void log(string message){if(!isInitialized){initlogger(); } date logfiledate = new date(); SimpleDateFormat dateFormat = new SimpledateFormat( "yyyy-mm-dd-hh-mm-ss"); string callingClassName = new Exception()。getStackTrace()[1] .getClassName();同期(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 auto-enerated catch block system.err.println( "file%sエラーへの記述ログを書き込みます。"); e.printstacktrace(); }}} public static void log(例外例外){if(!isInitialized){initlogger(); } date logfiledate = new date(); SimpleDateFormat dateFormat = new SimpledateFormat( "yyyy-mm-dd-hh-mm-ss"); string callingClassName = new Exception()。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"、log); } try {logwriter.write(log); logwriter.flush(); } catch(ioException e){// todo auto-enerated catch block system.err.println( "file%sエラーへの記述ログを書き込みます。"); e.printstacktrace(); }}}}