Log output: log4j log output
After doing something, I suddenly found that it was not long before the syso output was synchronously output to a folder while the program was running, but the foundation was too bad, after studying for a while, I wrote this item.
1 private static void log () throws IOException {2 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd "); // This is the preparation for the log file name 3 SimpleDateFormat format2 = new SimpleDateFormat ("yyyy MM dd HH: mm: ss "); // This is to accurately record the operation time in the log. 4 Date date = new Date (); 5 File f = new File (". // log "); // use a relative path and create a folder 6 if (! F. exists ()&&! F. isDirectory () {// determine whether the log folder exists and is a folder 7 f. mkdir (); // If the folder does not exist, create the folder 8} 9 f = new File (". // log // "+ format. format (date) + ". txt "); // create the corresponding file 10 Writer writer = new FileWriter (f, true); // output string uses writer 11 writer. write (format2.format (date) + "---- log end ----"); 12 writer. flush (); 13 writer. close (); 14}
Here, you can change this method to parameter passing. Then, you only need to instantiate and call the class containing this method. When you call this method, you can upload the statement that syso wants to output to record the log.