Write your own simple Java log class share _java

Source: Internet
Author: User
Tags dateformat erro
/** * * * * */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;
  private static Boolean isinitialized = false;
  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 ()) {New file (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";
      try {logwriter = new FileWriter (LogFilePath, true);
    IsInitialized = true; catch (IOException e) {// TODO auto-generated Catch block System.err.println ("Unable to create log file.");
      System.err.println ("initilization fail.");
      E.printstacktrace ();
    return false;
  return true;
    public static void Log (String message) {if (!isinitialized) {Initlogger ();
    Date logfiledate = new Date ();
    SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss");
    String callingclassname = new Exception (). Getstacktrace () [1]. GetClassName (); Synchronized (logwriter) {String log = String.Format (' [%s] @ [%s]:%s\n ", Callingclassname, Dateformat.fo
      Rmat (logfiledate), message);
      if (printlogwhenlog) {System.out.printf ("[log]:%s", log);
        try {logwriter.write (log);
      Logwriter.flush (); catch (IOException e) {//TODO auto-generated catch block System.err.println (' Write log to file%s Erro
        R. ");
      E.printstacktrace ();
  }  } public static void Log (Exception Exception) {if (!isinitialized) {Initlogger ();
    Date logfiledate = new Date ();
    SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss");
    String callingclassname = new Exception (). Getstacktrace () [1]. GetClassName (); Synchronized (logwriter) {String log = String.Format (' [%s] @ [%s]:%s\n ", Callingclassname, Dateformat.fo
      Rmat (Logfiledate), exception.tostring ());
      if (printlogwhenlog) {System.out.printf ("[log]:%s", log);
        try {logwriter.write (log);
      Logwriter.flush (); catch (IOException e) {//TODO auto-generated catch block System.err.println (' Write log to file%s Erro
        R. ");
      E.printstacktrace (); }
    }    
  }
 
}

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.