文章目录
  1. 1. Log的使用
    1. 1.1. commons-logging与Log4j
      1. 1.1.1. 为什么同时使用commons-logging和Log4j?为什么不仅使用其中之一?
      2. 1.1.2. Commons-logging能帮我们做什么?
      3. 1.1.3. Common-loggin说明
    2. 1.2. log4j和sfl4j和lobback到底有什么区别
    3. 1.3. Spring自带的日志(JCL)
      1. 1.3.0.1. JCL原理
      2. 1.3.0.2. spring使用JCL

Log的使用

slf4j与commons-logging只是一个日志门面,实际还是要依赖真正的日志库log4j,虽然slf4j和commons-loggins自带了日志库,但是毕竟log4j才是最强大的。

commons-logging与Log4j

为什么同时使用commons-logging和Log4j?为什么不仅使用其中之一?

​ Commons-loggin的目的是为“所有的Java日志实现”提供一个统一的接口,它自身的日志功能平常弱(只有一个简单的SimpleLog?),所以一般不会单独使用它。Log4j的功能非常全面强大,是目前的首选。我发现几乎所有的Java开源项目都会用到Log4j,但我同时发现,所有用到Log4j的项目一般也同时会用到commons-loggin。我想,大家都不希望自己的项目与Log4j绑定的太紧密吧。另外一个我能想到的“同时使用commons-logging和Log4j”的原因是,简化使用和配置。

Commons-logging能帮我们做什么?

​ 提供一个统一的日志接口,简单了操作,同时避免项目与某个日志实现系统紧密a耦合很贴心的帮我们自动选择适当的日志实现系统(这一点非常好!)它甚至不需要配置

这里看一下它怎么“‘很贴心的’帮我们‘自动选择’‘适当的’日志实现系统”:
1) 首先在classpath下寻找自己的配置文件commons-logging.properties,如果找到,则使用其中定义的Log实现类;

2)如果找不到commons-logging.properties文件,则在查找是否已定义系统环境变量org.apache.commons.logging.Log,找到则使用其定义的Log实现类;

1
2
建立一个叫 :CATALINA_OPTS 的环境变量 
给他的值 : - Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog - Dorg.apache.commons.logging.simplelog.defaultlog = warn

3) 否则,查看classpath中是否有Log4j的包,如果发现,则自动使用Log4j作为日志实现类;

4) 否则,使用JDK自身的日志实现类(JDK1.4以后才有日志实现类);

5) 否则,使用commons-logging自己提供的一个简单的日志实现类SimpleLog;

可见,commons-logging总是能找到一个日志实现类,并且尽可能找到一个“最合适”的日志实现类。我说它“很贴心”实际上是因为:

1、可以不需要配置文件;
2、自动判断有没有Log4j包,有则自动使用之;
3、最悲观的情况下也总能保证提供一个日志实现(SimpleLog)。

可以看到,commons-logging对编程者和Log4j都非常友好。

​ 为了简化配置commons-logging,一般不使用commons-logging的配置文件,也不设置与commons-logging相关的系统环境变量,而只需将Log4j的Jar包放置到classpash中就可以了。这样就很简单地完成了commons-logging与Log4j的融合。如果不想用Log4j了怎么办?只需将classpath中的Log4j的Jar包删除即可。就这么简单!

参考: https://www.cnblogs.com/tv151579/archive/2013/01/20/2868846.html

Common-loggin说明

​ 严格的说,commons-logging不是一个日志控件,没有日志功能,它只是统一了JDK Logging与Log4j的API,并把日志功能交给JDK Loggings或者是log4j。对于不能确定日志方式的系统,commons-logging是一个不错的选择,Spring,Hibernate,Struts等使用的都是commons-logging

log4j和sfl4j和lobback到底有什么区别

SLF4J:即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统(Log4j logback)。

在使用SLF4J的时候,不需要在代码中或配置文件中指定你打算使用那个具体的日志系统,SLF4J提供了统一的记录日志的接口,只要按照其提供的方法记录即可,最终日志的格式、记录级别、输出方式等通过具体日志系统的配置来实现,因此可以在应用中灵活切换日志系统。

log4j:没什么好说的,就是一个打日志,并且有日志系统

logback和log4j是非常相似的,Logback的内核重写了,在一些关键执行路径上性能提升10倍以上。而且logback不仅性能提升了,初始化内存加载也更小。说白了也是个日志系统,存储日志

原文链接:https://blog.csdn.net/u012954706/article/details/79572028

Spring自带的日志(JCL)

JCL原理

​ JCL,全称为”Jakarta Commons Logging”,也可称为”Apache Commons Logging”。

​ JCL这个日志框架跟Log4J,Java Logging API等日志框架不同。JCL采用了设计模式中的“适配器模式”,它对外提供统一的接口,然后在适配类中将对日志的操作委托给具体的日志框架,比如Log4J,Java Logging API等。

在JCL中对外有两个统一的接口,分别是Log和LogFactory。

spring使用JCL

Logging是spring中唯一强制的外部依赖,spring中默认使用的日志是commons-logging,简称JCL,这里说的强制性,是因为在spring-core这个模块中引入了该依赖。不过,引入了该依赖,也无需做任何其他的配置,它是`日志门面.

spring-jcl-xxx.jar下的 org.apache.commons.logging包中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.logging;

import java.io.Serializable;
import java.util.logging.LogRecord;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.LoggerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;

/**
* A minimal incarnation of Apache Commons Logging's {@code LogFactory} API,
* providing just the common {@link Log} lookup methods. This is inspired
* by the JCL-over-SLF4J bridge and should be source as well as binary
* compatible with all common use of the Commons Logging API (in particular:
* with {@code LogFactory.getLog(Class/String)} field initializers).
*
* <p>This implementation does not support Commons Logging's original provider
* detection. It rather only checks for the presence of the Log4j 2.x API
* and the SLF4J 1.7 API in the Spring Framework classpath, falling back to
* {@code java.util.logging} if none of the two is available. In that sense,
* it works as a replacement for the Log4j 2 Commons Logging bridge as well as
* the JCL-over-SLF4J bridge, both of which become irrelevant for Spring-based
* setups as a consequence (with no need for manual excludes of the standard
* Commons Logging API jar anymore either). Furthermore, for simple setups
* without an external logging provider, Spring does not require any extra jar
* on the classpath anymore since this embedded log factory automatically
* delegates to {@code java.util.logging} in such a scenario.
*
* <p><b>Note that this Commons Logging variant is only meant to be used for
* infrastructure logging purposes in the core framework and in extensions.</b>
* It also serves as a common bridge for third-party libraries using the
* Commons Logging API, e.g. Apache HttpClient, Castor and HtmlUnit, bringing
* them into the same consistent arrangement without any extra bridge jars.
*
* <p><b>For logging need in application code, prefer direct use of Log4j 2.x
* or SLF4J or {@code java.util.logging}.</b> Simply put Log4j 2.x or Logback
* (or another SLF4J provider) onto your classpath, without any extra bridges,
* and let the framework auto-adapt to your choice.
*
* @author Juergen Hoeller (for the {@code spring-jcl} variant)
* @since 5.0
*/
public abstract class LogFactory {

private static LogApi logApi = LogApi.JUL;

static {
ClassLoader cl = LogFactory.class.getClassLoader();
try {
// Try Log4j 2.x API
cl.loadClass("org.apache.logging.log4j.spi.ExtendedLogger");
logApi = LogApi.LOG4J;
}
catch (ClassNotFoundException ex1) {
try {
// Try SLF4J 1.7 SPI
cl.loadClass("org.slf4j.spi.LocationAwareLogger");
logApi = LogApi.SLF4J_LAL;
}
catch (ClassNotFoundException ex2) {
try {
// Try SLF4J 1.7 API
cl.loadClass("org.slf4j.Logger");
logApi = LogApi.SLF4J;
}
catch (ClassNotFoundException ex3) {
// Keep java.util.logging as default
}
}
}
}


/**
* Convenience method to return a named logger.
* @param clazz containing Class from which a log name will be derived
*/
public static Log getLog(Class<?> clazz) {
return getLog(clazz.getName());
}

/**
* Convenience method to return a named logger.
* @param name logical name of the <code>Log</code> instance to be returned
*/
public static Log getLog(String name) {
switch (logApi) {
case LOG4J:
return Log4jDelegate.createLog(name);
case SLF4J_LAL:
return Slf4jDelegate.createLocationAwareLog(name);
case SLF4J:
return Slf4jDelegate.createLog(name);
default:
// Defensively use lazy-initializing delegate class here as well since the
// java.logging module is not present by default on JDK 9. We are requiring
// its presence if neither Log4j nor SLF4J is available; however, in the
// case of Log4j or SLF4J, we are trying to prevent early initialization
// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly
// trying to parse the bytecode for all the cases of this switch clause.
return JavaUtilDelegate.createLog(name);
}
}

........

通过尝试加载log4j,slf4j等日志来创建Log接口的实现类

文章目录
  1. 1. Log的使用
    1. 1.1. commons-logging与Log4j
      1. 1.1.1. 为什么同时使用commons-logging和Log4j?为什么不仅使用其中之一?
      2. 1.1.2. Commons-logging能帮我们做什么?
      3. 1.1.3. Common-loggin说明
    2. 1.2. log4j和sfl4j和lobback到底有什么区别
    3. 1.3. Spring自带的日志(JCL)
      1. 1.3.0.1. JCL原理
      2. 1.3.0.2. spring使用JCL