/ ** * */ importar java.io.file; importar java.io.filewriter; importar java.io.ioexception; importar java.text.simpledFormat; importar java.util.date; /** * @Author Magic282 * */public class Logger {private Static String LogFilePath; Private estático booleano isinitializado = falso; Private Static FileWriter LogWriter = NULL; private estático booleano printLog whenlog = true; Private estático booleano initLogger () {string logDirectoryPath = System.getProperty ("user.dir") + java.io.file.separatorchar + "log"; if (! novo arquivo (logDirectoryPath) .Exists ()) {new File (logDirectoryPath) .mkdir (); } Data LogFiledate = new Date (); SimpledateFormat dateFormat = new SimpleDateFormat ("AAAA-MM-DD-HH-MM-SS"); LogFilePath = LogDirectoryPath + java.io.file.separatorchar + dateFormat.Format (LogFiledate) + ".Log"; tente {LogWriter = new FileWriter (LogFilePath, true); isinitialized = true; } catch (ioexception e) {// TODO GENERATO AUTOMACIONADO SYSTEMING BLOT SYSTEM.ERR.PRINTLN ("Não é possível criar arquivo de log."); System.err.println ("Inicilização falha."); E.PrintStackTrace (); retornar falso; } retornar true; } public static void log (string message) {if (! isinitialized) {initLogger (); } Data LogFiledate = new Date (); SimpledateFormat dateFormat = new SimpleDateFormat ("AAAA-MM-DD-HH-MM-SS"); String CallingClassName = new Exception (). GetStackTrace () [1] .getClassName (); sincronizado (logWriter) {string log = string.format ("[%s] @ [%s]:%s/n", CallingClassName, DateFormat.Format (LogFiledate), mensagem); if (printLogwhenlog) {System.out.printf ("[log]:%s", log); } tente {LogWriter.Write (log); logWriter.flush (); } catch (ioexception e) {// TODO GENERATO AUTOMENTADO SYSTEMING BLOT SYSTEM.ERR.println ("Escreva log no arquivo %s error."); E.PrintStackTrace (); }}} public static void log (exceção de exceção) {if (! isinitialized) {initLogger (); } Data LogFiledate = new Date (); SimpledateFormat dateFormat = new SimpleDateFormat ("AAAA-MM-DD-HH-MM-SS"); String CallingClassName = new Exception (). GetStackTrace () [1] .getClassName (); sincronizado (logWriter) {string log = string.format ("[%s] @ [%s]:%s/n", CallingClassName, dateFormat.Format (LogFiledate), excepcion.tostring ()); if (printLogwhenlog) {System.out.printf ("[log]:%s", log); } tente {LogWriter.Write (log); logWriter.flush (); } catch (ioexception e) {// TODO GENERATO AUTOMENTADO SYSTEMING BLOT SYSTEM.ERR.println ("Escreva log no arquivo %s error."); E.PrintStackTrace (); }}}}