Automatic slf binding implementation process judgment

Source: Internet
Author: User

Automatic slf binding implementation process judgment
The binding implementation class is based on the agreed principle: the following steps are taken to determine the implementation class:

1. the LoggerFactory scan implementation class path has several implementation classes, that is, there are several StaticLoggerBinder under org/slf4j/impl. class2. if multiple implementation classes exist, report the path of multiple implementation classes to the developer.
3. If there are multiple implementation classes, report to the developer which implementation class is actually bound
4. What if no implementation class is available?

Code Implementation

// File path to be scanned
Private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder. class ";

// Scan all StaticLoggerBinder. class paths to the set
Private static Set findPossibleStaticLoggerBinderPathSet (){
// Use Set instead of list in order to deal with bug #138
// Define hashset appropriate here because it preserves insertion order during iteration
Set staticLoggerBinderPathSet = new LinkedHashSet ();
Try {
ClassLoader loggerFactoryClassLoader = LoggerFactory. class
. GetClassLoader ();
Enumeration paths;
If (loggerFactoryClassLoader = null ){
Paths = ClassLoader. getSystemResources (STATIC_LOGGER_BINDER_PATH );
} Else {
Paths = loggerFactoryClassLoader
. GetResources (STATIC_LOGGER_BINDER_PATH );
}
While (paths. hasMoreElements ()){
URL path = (URL) paths. nextElement ();
StaticLoggerBinderPathSet. add (path );
}
} Catch (IOException ioe ){
Util. report ("Error getting resources from path", ioe );
}
Return staticLoggerBinderPathSet;
}
// Report all implementation class paths
Private static void reportMultipleBindingAmbiguity (Set staticLoggerBinderPathSet ){
If (isAmbiguousStaticLoggerBinderPathSet (staticLoggerBinderPathSet )){
Util. report ("Class path contains multiple SLF4J bindings .");
Iterator iterator = staticLoggerBinderPathSet. iterator ();
While (iterator. hasNext ()){
URL path = (URL) iterator. next ();
Util. report ("Found binding in [" + path + "]");
}
Util. report ("See" + MULTIPLE_BINDINGS_URL + "for an explanation .");
}
}

// Name of the implementation class actually used in the Report
Private static void reportActualBinding (Set staticLoggerBinderPathSet ){
If (isAmbiguousStaticLoggerBinderPathSet (staticLoggerBinderPathSet )){
Util. report ("Actual binding is of type [" + StaticLoggerBinder. getSingleton (). getLoggerFactoryClassStr () + "]");
}
}


// Obtain the instance of the implementation class. Because the StaticLoggerBinder class does not exist, the NoClassDefFoundError exception must be caught.

Private final static void bind (){
Try {
Set staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet ();
ReportMultipleBindingAmbiguity (staticLoggerBinderPathSet );
// The next line does the binding
StaticLoggerBinder. getSingleton ();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
ReportActualBinding (staticLoggerBinderPathSet );
EmitSubstituteLoggerWarning ();
} Catch (NoClassDefFoundError ncde ){
String msg = ncde. getMessage ();
If (messageContainsOrgSlf4jImplStaticLoggerBinder (msg )){
INITIALIZATION_STATE = NOP_FALLBACK_INITIALIZATION;
Util. report ("Failed to load class \" org. slf4j. impl. StaticLoggerBinder \".");
Util. report ("Defaulting to no-operation (NOP) logger implementation ");
Util. report ("See" + NO_STATICLOGGERBINDER_URL
+ "For further details .");
} Else {
FailedBinding (ncde );
Throw ncde;
}
} Catch (java. lang. NoSuchMethodError nsme ){
String msg = nsme. getMessage ();
If (msg! = Null & msg. indexOf ("org. slf4j. impl. StaticLoggerBinder. getSingleton ()")! =-1 ){
INITIALIZATION_STATE = FAILED_INITIALIZATION;
Util. report ("The slf4j-api 1.6.x (or later) is incompatible with this binding .");
Util. report ("Your binding is version 1.5.5 or earlier .");
Util. report ("Upgrade your binding to version 1.6.x .");
}
Throw nsme;
} Catch (Exception e ){
FailedBinding (e );
Throw new IllegalStateException ("Unexpected initialization failure", e );
}
}
At this point, the instance of the implementation class is actually obtained.

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.