Log output: log4j log output

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.