---
title: "Java  Logging Facade"
author: "dikar"
date: "2010-04-19T18:53:16Z"
tags: []
categories: ["编程语言"]
slug: "java-logging-facade"
draft: false
lastmod: "2010-04-19T18:53:16Z"
id: 647686
url: "https://dikar.iteye.com/blog/647686"
---

经常使用的java 日志门面选择日志框架的一个流程总结下：

 

首先是 Apache Commons Logging 摘录自 http://commons.apache.org/logging/guide.html

 

There are two base abstractions used by JCL: `Log` (the basic logger) and `LogFactory` (which knows how to create `Log` instances). Specifying a particular Log implementation is very useful (whether that is one provided by commons-logging or a user-defined one). Specifying a `LogFactory` implementation other than the default is a subject for advanced users only, so will not be addressed here. The default `LogFactory` implementation uses the following discovery process to determine what type of `Log` implementation it should use (the process terminates when the first positive match - in order - is found):

1. Look for a configuration attribute of this factory named `org.apache.commons.logging.Log` (for backwards compatibility to pre-1.0 versions of this API, an attribute `org.apache.commons.logging.log` is also consulted). Configuration attributes can be set explicitly by java code, but they are more commonly set by placing a file named commons-logging.properties in the classpath. When such a file exists, every entry in the properties file becomes an "attribute" of the LogFactory. When there is more than one such file in the classpath, releases of commons-logging prior to 1.1 simply use the first one found. From release 1.1, each file may define a `priority` key, and the file with the highest priority is used (no priority definition implies priority of zero). When multiple files have the same priority, the first one found is used. Defining this property in a commons-logging.properties file is the recommended way of explicitly selecting a Log implementation.
2. Look for a system property named `org.apache.commons.logging.Log` (for backwards compatibility to pre-1.0 versions of this API, a system property `org.apache.commons.logging.log` is also consulted).
3. If the Log4J logging system is available in the application class path, use the corresponding wrapper class ([Log4JLogger](http://commons.apache.org/logging/apidocs/org/apache/commons/logging/impl/Log4JLogger.html) ).
4. If the application is executing on a JDK 1.4 system, use the corresponding wrapper class ([Jdk14Logger](http://commons.apache.org/logging/apidocs/org/apache/commons/logging/impl/Jdk14Logger.html) ).
5. Fall back to the default simple logging wrapper ([SimpleLog](http://commons.apache.org/logging/apidocs/org/apache/commons/logging/impl/SimpleLog.html) ).

Consult the JCL javadocs for details of the various `Log` implementations that ship with the component. (The discovery process is also covered in more detail there.)

 

 

接着是 我经常使用的slf4J 摘录自 http://www.slf4j.org/manual.html

 

To switch logging frameworks, just replace slf4j bindings on your class path. For example, to switch from java.util.logging to log4j, just replace slf4j-jdk14-1.5.11.jar with slf4j-log4j12-1.5.11.jar.

 

 

SLF4J does not rely on any special class loader machinery. In fact, the each SLF4J binding is hardwired *at compile time* to use one and only one specific logging framework. For example, the slf4j-log12-1.5.11.jar binding is bound at compile time to use log4j. In your code, in addition to *slf4j-api-1.5.11.jar* , you simply drop **one and only one** binding of your choice onto the appropriate class path location. Do not place more than one binding on your class path. Here is a graphical illustration of the general idea.

 

![](http://www.slf4j.org/images/bindings.png)