Logging in Java, file not create -
i want perform logging. created class, class not create file.
this class logging:
public class log { private static file logfile = new file("log.txt"); public log() { if (!logfile.exists()) { try { logfile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } } } public static void write(string msg) { writeinfile(createstring(msg)); } private static string createstring(string msg) { stringbuilder sb = new stringbuilder(100); sb.append(mydate.currentdate()).append("\t| ").append(msg); return sb.append('\n').tostring(); } private static void writeinfile(string msg) { try (bufferedwriter out = new bufferedwriter(new filewriter(logfile, true))) { out.write(msg); out.flush(); } catch (ioexception e) { e.printstacktrace(); } } static class mydate { public static string currentdate() { dateformat formatter = new simpledateformat("hh:mm.ss"); date date = new date(); return formatter.format(date); } }}
and add log.write("some text log");
in diferent methods
check canonical path location file. code have depends on current working directory.
Comments
Post a Comment