Avoid initializing the log class in each class

Source: Internet
Author: User


When writing code, many classes need to log, the general wording is:

Logger log = Loggerfactory.getlogger (Xxx.class); Or the argument gives the class name string


Every class to write, so annoying, can be encapsulated, automatically get the use of the log class name, so do not have to write in each class:


A trial-and-error implementation (obtained via the exception stack):

public static Logger GetLogger () {Logger Logger = Logger.getlogger (Getinvokingclassname ()); Propertyconfigurator.configure (Getconfigfilename ()); return logger;} public static String Getinvokingclassname () {return new Throwable (). Getstacktrace () [2].getclassname ();}

Some people say that this way performance will be problematic, online someone has given the following ways to obtain the class name:

 // 1, through the SecurityManager protection Method Getclasscontext ()       String  Clazzname = new securitymanager ()  {           public string getclassname ()  {               return getclasscontext () [1].getname ();           }      }.getclassname ();       system.out.println (clazzname),        **** above this always feel where there is a limit *****     2, by parsing the anonymous class name ()       string clazzname3 = new  object ()  {          public String  GetClassName ()  {               String clazzname = this.getclasS (). GetName ();              return  Clazzname.substring (0, clazzname.lastindexof (' $ '));           }      }.getclassname ();       System.out.println (ClazzName3);       3, method of Thread getstacktrace ()          string clazzname4 = thread.currentthread (). GetStackTrace () [ 2].getclassname ();       system.out.println (clazzName4);              Note: Although the above 3 methods can get the class name, but, when writing the log, we are to see from the log       Log is from that class, and the above 3 can only display its own class information, but not the information of the      class that is writing the log.


In fact, the implementation of the text is also the JDK implementation mode:

The code given below is the source code of JDK1.4, and the source code of LOG4J. Explains its implementation principle.
Getting the calling class, and the method name, is the structure that needs to get the current run stack.
New Throwable (). Getstacktrace () returns the hierarchy of the current run stack.
With this principle, the call relationship of the entire running stack can be obtained.

The JDK1.4 java.util.logging package is implemented by the Throwable.getstacktrace () method.
Get the stack trace.
Stacktraceelement stack[] = (new Throwable ()). Getstacktrace ();


The implementation of LOG4J is also similar:

Org.apache.log4j.spi.LocationInfo class.
First use the Throwable.printstacktrace () method to print the exception information into a string.
The string is then parsed by rows. Extracts the calling class and method.


JDK1.5 introduces the Getstacktrace () and Getallstacktraces () Two methods inside the thread class. We don't have to (new Throwable ()). Getstacktrace (); You can call

Thread.getcurrentthread (). Getstacktrace () to get the running stack information for the current thread. Not only that, you can also get run stack information for other threads as long as permission is allowed. Unfortunately, although the stack information can be obtained, but the log printing is still showing the Thread.getcurrentthread (). Getstacktrace () information about the class in which the statement resides.



Avoid initializing the log class in each class

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.