首页 > 其他分享 >Spring 日志文档翻译

Spring 日志文档翻译

时间:2022-11-04 14:36:26浏览次数:53  
标签:logging Spring commons SLF4J 文档 JCL 日志


原文:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#overview-logging

2.3.2 日志

日志对于​​spring​​来说非常重要,因为 

a)它是唯一强制的外部依赖,

b)每个人都希望在使用某个工具时可以看到一些友好地输出,

c)​​Spring​​集成了很多其他的工具,它们也都有自己的日志依赖。

应用开发者的一个目标通常是:对于整个应用来说(包括所有的外部组件),集中创建一个统一的日志配置。由于现在有如此多的日志框架,这个选择看起来会变得更难。

Logging is a very important dependency for Spring because a) it is the only mandatory external dependency, b) everyone likes to see some output from the tools they are using, and c) Spring integrates with lots of other tools all of which have also made a choice of logging dependency. One of the goals of an application developer is often to have unified logging configured in a central place for the whole application, including all external components. This is more difficult than it might have been since there are so many choices of logging framework.

​Spring​​中托管的日志依赖是​​Jakarta Commons Logging API (JCL)​​。我们反编译​​JCL​​并且让​​JCL​​日志对象对于所有继承​​Spring​​框架的类可见。由于​​Spring​​的所有版本都使用相同的日志库,所以迁移变的很简单。因为保证了向后兼容,甚至对于继承了​​Spring​​的应用来说也一样。我们处理的方法是让​​Spring​​的其中一个模块明确地依赖于​​commons-logging​​(​​JCL​​的标准实现),然后在编译时让其他的模块都依赖于这个模块。如果你使用​​Maven​​,并且想知道在哪里引入了​​commons-logging​​的依赖,你会发现它是从​​Spring​​的核心模块​​spring-core​​中引入的。

The mandatory logging dependency in Spring is the Jakarta Commons Logging API (JCL). We compile against JCL and we also make JCL Log objects visible for classes that extend the Spring Framework. It’s important to users that all versions of Spring use the same logging library: migration is easy because backwards compatibility is preserved even with applications that extend Spring. The way we do this is to make one of the modules in Spring depend explicitly on commons-logging (the canonical implementation of JCL), and then make all the other modules depend on that at compile time. If you are using Maven for example, and wondering where you picked up the dependency on commons-logging, then it is from Spring and specifically from the central module called spring-core.

​commons-logging​​的好处是让你不需要任何别的东西就可以让你的应用工作。它有一个运行时的发现​​​算法​​​,会在​​classpath​​下寻找另外的日志框架,并且使用其中一个它认为合理的(或者你可以告诉它你需要哪一个)。如果在​​classpath​​下没有可用的日志框架,那么你将会使用​​JDK​​自带的日志(​​Java.util.logging​​缩写​​JUI​​)。你会发现在大多数情况下你的​​Spring​​应用可以正常工作并且可以很轻松地将日志打印到控制台,这很重要。

The nice thing about commons-logging is that you don’t need anything else to make your application work. It has a runtime discovery algorithm that looks for other logging frameworks in well known places on the classpath and uses one that it thinks is appropriate (or you can tell it which one if you need to). If nothing else is available you get pretty nice looking logs just from the JDK (java.util.logging or JUL for short). You should find that your Spring application works and logs happily to the console out of the box in most situations, and that’s important.


不使用commons logging

不幸地是,​​commons-logging​​的运行时发现机制虽然对于终端用户很方便,却是有问题的。如果我们可以让时间倒退重新开始Spring项目,我们可能会使用一个不同的日志依赖。第一选择很可能就是​​Simple Logging Facade for Java​​ ( ​​SLF4J​​),很多工具中都使用到了​​SLF4J​​,并且这些工具在应用中通常是和​​Spring​​集成使用。

Unfortunately, the runtime discovery algorithm in commons-logging, while convenient for the end-user, is problematic. If we could turn back the clock and start Spring now as a new project it would use a different logging dependency. The first choice would probably be the Simple Logging Facade for Java ( SLF4J), which is also used by a lot of other tools that people use with Spring inside their applications.

基本上现在有两种关闭​​commons-logging​​的方法:

There are basically two ways to switch off commons-logging:

​spring-core​

  1. 模块中排除

​commons-logging​

  1. 依赖(因为它是唯一明确地依赖于

​commons-logging​

  1. 的模块)

Exclude the dependency from the spring-core module (as it is the only module that explicitly depends on commons-logging)

  1. 依赖于一个特殊的

​commons-logging​

  1. 依赖,它将会用一个空的jar来替换原有jar(更详细的信息可以查看

​SLF4J​

  1. 的FAQ)

Depend on a special commons-logging dependency that replaces the library with an empty jar (more details can be found in the SLF4J FAQ)

为了排除​​commons-logging​​,需要在​​dependencyManagement​​部分添加下列代码:

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.9.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

现在这个应用已经无法正常运行了,因为在​​classpath​​下没有​​JCL API​​的实现,为了解决这个问题,我们还是需要提供一个实现。在下面一节中我们将举例告诉你如何使用​​SLF4J​​来提供一个可替代的​​JCL​​实现。

Now this application is probably broken because there is no implementation of the JCL API on the classpath, so to fix it a new one has to be provided. In the next section we show you how to provide an alternative implementation of JCL using SLF4J as an example.


使用SLF4J

​SLF4J​​是一个更纯粹的依赖并且在运行时比​​commons-logging​​更有效率,因为它使用编译时绑定来代替了运行时发现机制来寻找其他集成的日志框架。这也意味着你需要更清楚在运行时你希望发生什么,然后通过相应的声明或配置来实现。​​SLF4J​​提供对很多常见的日志框架的绑定,所以你可以绑定一个你已经在用的日志框架来配置和管理。

SLF4J is a cleaner dependency and more efficient at runtime than commons-logging because it uses compile-time bindings instead of runtime discovery of the other logging frameworks it integrates. This also means that you have to be more explicit about what you want to happen at runtime, and declare it or configure it accordingly. SLF4J provides bindings to many common logging frameworks, so you can usually choose one that you already use, and bind to that for configuration and management.

​SLF4J​​提供对很多常见的日志框架的绑定,包括​​JCL​​,倒过来也成立:因为这是一条其他日志框架和​​SLF4J​​的桥梁。所以想要在​​Spring​​中使用​​SLF4J​​,你需要用​​SLF4J-JCL​​桥梁来替代​​commons-logging​​依赖。一旦你替换了,那么​​Spring​​中的日志的调用将都会被转换成对​​SLF4J API​​的调用,如果在你的应用中的其他lib也使用了这个​​API​​,那么你可以有一个集中的地方来配置和管理日志。

SLF4J provides bindings to many common logging frameworks, including JCL, and it also does the reverse: bridges between other logging frameworks and itself. So to use SLF4J with Spring you need to replace the commons-logging dependency with the SLF4J-JCL bridge. Once you have done that then logging calls from within Spring will be translated into logging calls to the SLF4J API, so if other libraries in your application use that API, then you have a single place to configure and manage logging.

一个常见的选择可能会是建立​​Spring​​和​​SLF4J​​的桥梁,然后提供明确的从​​SLF4J​​到​​Log4J​​的绑定。你需要提供4个依赖(排除已经存在的​​commons-logging​​):桥梁,​​SLF4J API​​,对​​Log4J​​的绑定,​​Log4J​​的实现。在​​Maven​​中你可以做如下配置:

A common choice might be to bridge Spring to SLF4J, and then provide explicit binding from SLF4J to Log4J. You need to supply 4 dependencies (and exclude the existing commons-logging): the bridge, the SLF4J API, the binding to Log4J, and the Log4J implementation itself. In Maven you would do that like this:

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.9.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>

为了得到一些日志,这看起来可能会引入很多依赖。,

That might seem like a lot of dependencies just to get some logging. Well it is, but it is optional, and it should behave better than the vanilla commons-logging with respect to classloader issues, notably if you are in a strict ​​Container​​ like an OSGi platform. Allegedly there is also a performance benefit because the bindings are at compile-time not runtime.

在​​SLF4J​​用户中,一个更常见的选择是直接绑定到​​Logback​​上,因为这样步骤更少且产生的依赖也更少。其中省去了额外的绑定这一步因为​​Logback​​直接实现了​​SLF4J API​​,你只需要依赖两个lib而不是四个(​​jcl-over-slf4j​​和​​logback​​)。如果你这么做,还需要从其他外部依赖(不是​​Spring​​)中排除​​slf4j-api​​,因为你只需要一个版本的​​SLF4J API​​在你的​​classpath​​下。

A more common choice amongst SLF4J users, which uses fewer steps and generates fewer dependencies, is to bind directly to Logback. This removes the extra binding step because Logback implements SLF4J directly, so you only need to depend on two libraries not four ( jcl-over-slf4j and logback). If you do that you might also need to exclude the slf4j-api dependency from other external dependencies (not Spring), because you only want one version of that API on the classpath.


使用Log4J

很多人因为配置和管理的原因,选择​​Log4J​​作为日志框架。它是高效且完善的,并且在实际中当我们构建和​​​测试​​​Spring时,运行时使用的就是​​Log4J​​。​​Spring​​也提供一些工具类来配置和初始化​​Log4j​​,所以在一些模块中有些可选择的编译时依赖​​Log4j​​。

Many people use Log4j as a logging framework for configuration and management purposes. It’s efficient and well-established, and in fact it’s what we use at runtime when we build and test Spring. Spring also provides some utilities for configuring and initializing Log4j, so it has an optional compile-time dependency on Log4j in some modules.

为了让​​Log4j​​可以和默认的​​JCL​​依赖(​​commons-logging​​)一起工作,你需要做的是将​​Log4j​​放到​​classpath​​下,并且提供一个配置文件(在​​classpath​​提供一个​​log4j.properties​​或​​log4j.xml​​)。对于​​Maven​​用户来说,下面给出依赖的声明:

To make Log4j work with the default JCL dependency ( commons-logging) all you need to do is put Log4j on the classpath, and provide it with a configuration file ( log4j.properties or log4j.xml in the root of the classpath). So for Maven users this is your dependency declaration:

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>

下面是一个​​log4j.properties​​的示例配置告诉你如何将日志输出到控制台:

log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n

log4j.category.org.springframework.beans.factory=DEBUG

标签:logging,Spring,commons,SLF4J,文档,JCL,日志
From: https://blog.51cto.com/u_15861563/5823758

相关文章

  • 声明Spring Bean和注入Bean的几种常用注解和区别
    Spring声明Bean的注解: @Component:组件,没有明确的角色。 @Service:在业务逻辑层(Service层)使用。@Repository: 再数据访问层(Dao层)使用。@Controller:再展现层(MVC->Sprin......
  • 深入理解 Spring 事务原理
    Spring事务的基本原理Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的。对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤......
  • spring security 01 初始入门
    1.引入<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.3</versio......
  • Spring 源码(七)Spring 事务源码解析
    注册后置处理器开启对事务的支持@EnableTransactionManagement​​@EnableTransactionManagement​​注解的主要作用是开启对事务的支持,源码如下:@Target(ElementType.TYPE)@......
  • Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
    问题描述SpringCache提供的@Cacheable注解不支持配置过期时间,还有缓存的自动刷新。我们可以通过配置CacheManneg来配置默认的过期时间和针对每个缓存容器(value)单独配置过......
  • Spring Boot缓存实战 Caffeine
    Caffeine和SpringBoot集成Caffeine是使用Java8对Guava缓存的重写版本,在SpringBoot2.0中将取代Guava。如果出现Caffeine,CaffeineCacheManager将会自动配置。使用spring.ca......
  • Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存-2
    问题上一篇​​SpringBootCache+redis设置有效时间和自动刷新缓存,时间支持在配置文件中配置​​,说了一种时间方式,直接扩展注解的Value值,如:@Override@Cacheable(value=......
  • Spring Boot缓存实战 Redis + Caffeine 实现多级缓存
    在前文我们介绍了如何使用Redis或者Caffeine来做缓存。​​SpringBoot缓存实战Redis设置有效时间和自动刷新缓存-2​​​​SpringBoot缓存实战Caffeine​​问题描述:通......
  • Spring Bean 的注册和注入的几种常用方式和区别
    Spring注册Bean:包扫描+组件标注注解(@Controller、@Service、@Repository、@Component),一般项目里面使用。使用@Bean注解,一般导入第三方组件的时候使用。使用@Import注解,一......
  • python操作日志
    importtimeimportosimportloggingcurrrent_path=os.path.dirname(__file__).rsplit("\\",1)[0]log_path=os.path.join(currrent_path,'../logs')classLo......