Logging in Java Threads

Source: Internet
Author: User

Import Java.io.File; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.PrintWriter; Import Java.util.HashMap; public class Threadlog {private final static String _detaillogfile = "Log" +file.separator+ "detail"; private static Boolea n _logflag = true; private static hashmap<string, string> _threaddetailfile = new hashmap<string, string> (); private static int _fileindex = 1; private static void Generatelogfile (String fileName) {string absolutefilepath = Projectconfig.getnfscabsolutepath () + Fi Le.separator + "Log" + File.separator +filename; _threaddetailfile.put (String.valueof (Thread.CurrentThread (). GetId ()) + "_" + FileName, Absolutefilepath); File File = new file (Absolutefilepath); Create a new file if (!file.exists ()) {try {file.createnewfile ();} catch (IOException ex) {Exceptiondefaulthandler.han Dle (ex, "Threadlog.log"); }} private static void Generatelogfile () {String fileName = _detaillogfile + datetimeutil.getcurrentdateformatstring ("Yyyymmddhhmmss ") +" _ "+ string.valueof (_fileindex++) +". Log "; String Absolutefilepath = Projectconfig.getnfscabsolutepath () + File.separator + fileName; _threaddetailfile.put (String.valueof (Thread.CurrentThread (). GetId ()), Absolutefilepath); File File = new file (Absolutefilepath); If the file exist, we have to remove it. if (file.exists ()) {try {file.delete ();} catch (SecurityException ex) {Exceptiondefaulthandler.handle ex, " Threadlog.log "); }//create a new file try {file.createnewfile ();} catch (IOException ex) {Exceptiondefaulthandler.handle (ex, "Threadlo G.log "); The private static Boolean Hasthreadlogfile () {return _threaddetailfile.containskey (string.valueof) ( Thread.CurrentThread (). GetId ()); private static Boolean hasthreadlogfile (String fileName) {return _threaddetailfile.containskey string.valueof ( Thread.CurrentThread (). GetId ()) + "_" + fileName); public static void Makelog (string detail, string fileName) {if (!_logflag) {return;} if (!hasthreadlogfile (fileName)){generatelogfile (fileName);} File File = new file (_threaddetailfile.get string.valueof (Thread.CurrentThread (). GetId ()) + "_" + FileName)); if (File.canwrite ()) {Boolean appendflag = true; if (File.length () > 1000000) {appendflag = false;} PrintWriter StreamWriter = null; try {streamwriter = new PrintWriter (new FileOutputStream (file, Appendflag)); StreamWriter.Write ("-------------------- --------------------------------------------"); StreamWriter.Write (Datetimeutil.getcurrentdatelocalformatstring ()); StreamWriter.Write ("----------------------------------------------------------------"); StreamWriter.Write (Systemproperties.newinstance (). Getlineseparator ()); StreamWriter.Write (detail); StreamWriter.Write (Systemproperties.newinstance (). Getlineseparator ()); StreamWriter.Close (); catch (IOException ex) {Exceptiondefaulthandler.handle (ex, "Threadlog.log");} finally {streamwriter.close ();}} else {System.out.println ("does not have the permission to write into the" + _threaddetailfilE.get (String.valueof (Thread.CurrentThread (). GetId ()) + ""); } public static void Makelog (String detail) {if (!_logflag) {return;} if (!hasthreadlogfile ()) {generatelogfile ();} F ile file = new file (_threaddetailfile.get (string.valueof (Thread.CurrentThread (). GetId ())); if (File.canwrite ()) {Boolean appendflag = true; if (File.length () > 1000000) {appendflag = false;} PrintWriter StreamWriter = null; try {streamwriter = new PrintWriter (new FileOutputStream (file, Appendflag)); StreamWriter.Write ("-------------------- --------------------------------------------"); StreamWriter.Write (Datetimeutil.getcurrentdatelocalformatstring ()); StreamWriter.Write ("----------------------------------------------------------------"); StreamWriter.Write (Systemproperties.newinstance (). Getlineseparator ()); StreamWriter.Write (detail); StreamWriter.Write (Systemproperties.newinstance (). Getlineseparator ()); StreamWriter.Close (); catch (IOException ex) {Exceptiondefaulthandler.handle ex, "Threadlog.log"); finally {streamwriter.close ();}} else {System.out.println ("does not have the permission to write into the" + _threaddetailfile.get (string.valueof Rentthread (). GetId ())) + ""); }}}  

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.