/** * * * * */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 (logfiledate) + ". Log";
try {logwriter = new FileWriter (LogFilePath, true);
IsInitialized = true; catch (IOException e) {// TODO auto-generated Catch block System.err.println ("Unable to create log file.");
System.err.println ("initilization fail.");
E.printstacktrace ();
return false;
return 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 (); Synchronized (logwriter) {String log = String.Format (' [%s] @ [%s]:%s\n ", Callingclassname, Dateformat.fo
Rmat (logfiledate), message);
if (printlogwhenlog) {System.out.printf ("[log]:%s", log);
try {logwriter.write (log);
Logwriter.flush (); catch (IOException e) {//TODO auto-generated catch block System.err.println (' Write log to file%s Erro
R. ");
E.printstacktrace ();
} } public static void Log (Exception Exception) {if (!isinitialized) {Initlogger ();
Date logfiledate = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss");
String callingclassname = new Exception (). Getstacktrace () [1]. GetClassName (); Synchronized (logwriter) {String log = String.Format (' [%s] @ [%s]:%s\n ", Callingclassname, Dateformat.fo
Rmat (Logfiledate), exception.tostring ());
if (printlogwhenlog) {System.out.printf ("[log]:%s", log);
try {logwriter.write (log);
Logwriter.flush (); catch (IOException e) {//TODO auto-generated catch block System.err.println (' Write log to file%s Erro
R. ");
E.printstacktrace (); }
}
}
}