/ ** * */ 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; booleano estático privado isinitializado = falso; 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 ()) {nuevo archivo (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"; intente {logwriter = new FileWriter (logFilePath, true); isinitialized = verdadero; } Catch (ioException e) {// TODO Auto Generado Bloque System.err.println ("No se puede crear el archivo de registro"); System.err.println ("Fallor de inicilización"); E.PrintStackTrace (); devolver falso; } return verdadero; } public static void log (mensaje de cadena) {if (! isInitialized) {initLogger (); } Date logFileDate = new Date (); SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-mm-dd-hh-mm-ss"); Cadena callClassName = new Exception (). GetStackTrace () [1] .getClassName (); sincronizado (logwriter) {string log = string.format ("[%s] @ [%s]:%s/n", callClassName, dateFormat.format (logFileDate), mensaje); if (printLogWhenLog) {System.out.printf ("[log]:%s", log); } try {logwriter.write (log); logwriter.flush (); } Catch (ioException e) {// TODO Generado Auto Generado Bloque System.err.println E.PrintStackTrace (); }}} public static void log (excepción de excepción) {if (! isinitialized) {initLogger (); } Date logFileDate = new Date (); SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-mm-dd-hh-mm-ss"); Cadena callClassName = new Exception (). GetStackTrace () [1] .getClassName (); sincronizado (logwriter) {string log = string.format ("[%s] @ [%s]:%s/n", callClassName, dateFormat.format (logFileDate), excepción.toString ()); if (printLogwhenlog) {System.out.printf ("[log]:%s", log); } try {logwriter.write (log); logwriter.flush (); } Catch (ioException e) {// TODO Generado Auto Generado Bloque System.err.println E.PrintStackTrace (); }}}}