关键词:
slf4j简介和技术选型
市面上的日志框架:
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....
日志实现 | |
---|---|
Log4j JUL(java.util.logging) Log4j2 |
左边选一个门面(抽象层)、右边来选一个实现;
日志门面: SLF4J;
日志实现:Logback;
SpringBoot:底层是Spring框架,Spring框架默认是用JCL;‘
SpringBoot选用 SLF4j和logback;
一、slf4j简介
slf4j(Simple logging facade for Java)是对所有日志框架制定的一种规范、标准、接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体的日志框架实现配合使用
slf4j是门面模式的典型应用,外部与一个子系统的通信必须通过一个统一的外观对象进行,使得子系统更易于使用。
日志实现(log4j、logback、log4j2)
- log4j是apache实现的一个开源日志组件
- logback同样是由log4j的作者设计完成的,拥有更好的特性,用来取代log4j的一个日志框架,是slf4j的原生实现
- log4j2是log4j 1.x和logback的改进版,据说采用了一些新技术(无锁异步、等等),使得日志的吞吐量、性能比log4j 1.x提高10倍,并解决了一些死锁的bug,而且配置更加简单灵活。
为什么要使用slf4j
- SLF4J提供了统一的记录日志的接口,对不同日志系统的具体实现进行了抽象化,只要按照其提供的方法记录即可,最终日志的格式、记录级别、输出方式等通过绑定具体的日志系统来实现。在项目中使用了SLF4J记录日志,并且绑定了log4j,则日志会以log4j的风格输出;后期需要改为以logback的风格输出日志,只需要将jar包log4j替换成logback即可,不用修改项目中的代码。
- SLF4J支持
作为占位符,等价于C语言中的
%s
,而不必再进行字符串的拼接,效率有显著的提升
maven jar包
springBoot版本:
<!--boot版本:--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--使用的log版本是(boot这一个就可以了):以下内容会自动导入--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <version>2.2.13.RELEASE</version> </dependency> <!--logback-classic和logback-core版本是:--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.30</version> <scope>compile</scope> </dependency>
二、实现原理
slf4j只是一个日志标准,并不是日志系统的具体实现。slf4j只提做两件事情:Logger类用来打日志,LoggerFactory类用来获取Logger;
- 提供日志接口 (Logger.class)
- 提供获取具体日志对象的方法 (LoggerFactory.class)
Logger.class
/** * Copyright (c) 2004-2011 QOS.ch * All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ package org.slf4j; /** * The org.slf4j.Logger interface is the main user entry point of SLF4J API. * It is expected that logging takes place through concrete implementations * of this interface. * <p/> * <h3>Typical usage pattern:</h3> * <pre> * import org.slf4j.Logger; * import org.slf4j.LoggerFactory; * * public class Wombat * * <span style="color:green">final static Logger logger = LoggerFactory.getLogger(Wombat.class);</span> * Integer t; * Integer oldT; * * public void setTemperature(Integer temperature) * oldT = t; * t = temperature; * <span style="color:green">logger.debug("Temperature set to . Old temperature was .", t, oldT);</span> * if(temperature.intValue() > 50) * <span style="color:green">logger.info("Temperature has risen above 50 degrees.");</span> * * * * </pre> * * Be sure to read the FAQ entry relating to <a href="../../../faq.html#logging_performance">parameterized * logging</a>. Note that logging statements can be parameterized in * <a href="../../../faq.html#paramException">presence of an exception/throwable</a>. * * <p>Once you are comfortable using loggers, i.e. instances of this interface, consider using * <a href="MDC.html">MDC</a> as well as <a href="Marker.html">Markers</a>.</p> * * @author Ceki Gülcü */ public interface Logger /** * Case insensitive String constant used to retrieve the name of the root logger. * * @since 1.3 */ final public String ROOT_LOGGER_NAME = "ROOT"; /** * Return the name of this <code>Logger</code> instance. * @return name of this logger instance */ public String getName(); /** * Is the logger instance enabled for the TRACE level? * * @return True if this Logger is enabled for the TRACE level, * false otherwise. * @since 1.4 */ public boolean isTraceEnabled(); /** * Log a message at the TRACE level. * * @param msg the message string to be logged * @since 1.4 */ public void trace(String msg); /** * Log a message at the TRACE level according to the specified format * and argument. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the TRACE level. </p> * * @param format the format string * @param arg the argument * @since 1.4 */ public void trace(String format, Object arg); /** * Log a message at the TRACE level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the TRACE level. </p> * * @param format the format string * @param arg1 the first argument * @param arg2 the second argument * @since 1.4 */ public void trace(String format, Object arg1, Object arg2); /** * Log a message at the TRACE level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous string concatenation when the logger * is disabled for the TRACE level. However, this variant incurs the hidden * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method, * even if this logger is disabled for TRACE. The variants taking @link #trace(String, Object) one and * @link #trace(String, Object, Object) two arguments exist solely in order to avoid this hidden cost.</p> * * @param format the format string * @param arguments a list of 3 or more arguments * @since 1.4 */ public void trace(String format, Object... arguments); /** * Log an exception (throwable) at the TRACE level with an * accompanying message. * * @param msg the message accompanying the exception * @param t the exception (throwable) to log * @since 1.4 */ public void trace(String msg, Throwable t); /** * Similar to @link #isTraceEnabled() method except that the * marker data is also taken into account. * * @param marker The marker data to take into consideration * @return True if this Logger is enabled for the TRACE level, * false otherwise. * * @since 1.4 */ public boolean isTraceEnabled(Marker marker); /** * Log a message with the specific Marker at the TRACE level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged * @since 1.4 */ public void trace(Marker marker, String msg); /** * This method is similar to @link #trace(String, Object) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg the argument * @since 1.4 */ public void trace(Marker marker, String format, Object arg); /** * This method is similar to @link #trace(String, Object, Object) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg1 the first argument * @param arg2 the second argument * @since 1.4 */ public void trace(Marker marker, String format, Object arg1, Object arg2); /** * This method is similar to @link #trace(String, Object...) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param argArray an array of arguments * @since 1.4 */ public void trace(Marker marker, String format, Object... argArray); /** * This method is similar to @link #trace(String, Throwable) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param msg the message accompanying the exception * @param t the exception (throwable) to log * @since 1.4 */ public void trace(Marker marker, String msg, Throwable t); /** * Is the logger instance enabled for the DEBUG level? * * @return True if this Logger is enabled for the DEBUG level, * false otherwise. */ public boolean isDebugEnabled(); /** * Log a message at the DEBUG level. * * @param msg the message string to be logged */ public void debug(String msg); /** * Log a message at the DEBUG level according to the specified format * and argument. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the DEBUG level. </p> * * @param format the format string * @param arg the argument */ public void debug(String format, Object arg); /** * Log a message at the DEBUG level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the DEBUG level. </p> * * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void debug(String format, Object arg1, Object arg2); /** * Log a message at the DEBUG level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous string concatenation when the logger * is disabled for the DEBUG level. However, this variant incurs the hidden * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method, * even if this logger is disabled for DEBUG. The variants taking * @link #debug(String, Object) one and @link #debug(String, Object, Object) two * arguments exist solely in order to avoid this hidden cost.</p> * * @param format the format string * @param arguments a list of 3 or more arguments */ public void debug(String format, Object... arguments); /** * Log an exception (throwable) at the DEBUG level with an * accompanying message. * * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void debug(String msg, Throwable t); /** * Similar to @link #isDebugEnabled() method except that the * marker data is also taken into account. * * @param marker The marker data to take into consideration * @return True if this Logger is enabled for the DEBUG level, * false otherwise. */ public boolean isDebugEnabled(Marker marker); /** * Log a message with the specific Marker at the DEBUG level. * * @param marker the marker data specific to this log statement * @param msg the message string to be logged */ public void debug(Marker marker, String msg); /** * This method is similar to @link #debug(String, Object) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg the argument */ public void debug(Marker marker, String format, Object arg); /** * This method is similar to @link #debug(String, Object, Object) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void debug(Marker marker, String format, Object arg1, Object arg2); /** * This method is similar to @link #debug(String, Object...) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arguments a list of 3 or more arguments */ public void debug(Marker marker, String format, Object... arguments); /** * This method is similar to @link #debug(String, Throwable) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void debug(Marker marker, String msg, Throwable t); /** * Is the logger instance enabled for the INFO level? * * @return True if this Logger is enabled for the INFO level, * false otherwise. */ public boolean isInfoEnabled(); /** * Log a message at the INFO level. * * @param msg the message string to be logged */ public void info(String msg); /** * Log a message at the INFO level according to the specified format * and argument. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the INFO level. </p> * * @param format the format string * @param arg the argument */ public void info(String format, Object arg); /** * Log a message at the INFO level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the INFO level. </p> * * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void info(String format, Object arg1, Object arg2); /** * Log a message at the INFO level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous string concatenation when the logger * is disabled for the INFO level. However, this variant incurs the hidden * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method, * even if this logger is disabled for INFO. The variants taking * @link #info(String, Object) one and @link #info(String, Object, Object) two * arguments exist solely in order to avoid this hidden cost.</p> * * @param format the format string * @param arguments a list of 3 or more arguments */ public void info(String format, Object... arguments); /** * Log an exception (throwable) at the INFO level with an * accompanying message. * * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void info(String msg, Throwable t); /** * Similar to @link #isInfoEnabled() method except that the marker * data is also taken into consideration. * * @param marker The marker data to take into consideration * @return true if this logger is warn enabled, false otherwise */ public boolean isInfoEnabled(Marker marker); /** * Log a message with the specific Marker at the INFO level. * * @param marker The marker specific to this log statement * @param msg the message string to be logged */ public void info(Marker marker, String msg); /** * This method is similar to @link #info(String, Object) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg the argument */ public void info(Marker marker, String format, Object arg); /** * This method is similar to @link #info(String, Object, Object) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void info(Marker marker, String format, Object arg1, Object arg2); /** * This method is similar to @link #info(String, Object...) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arguments a list of 3 or more arguments */ public void info(Marker marker, String format, Object... arguments); /** * This method is similar to @link #info(String, Throwable) method * except that the marker data is also taken into consideration. * * @param marker the marker data for this log statement * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void info(Marker marker, String msg, Throwable t); /** * Is the logger instance enabled for the WARN level? * * @return True if this Logger is enabled for the WARN level, * false otherwise. */ public boolean isWarnEnabled(); /** * Log a message at the WARN level. * * @param msg the message string to be logged */ public void warn(String msg); /** * Log a message at the WARN level according to the specified format * and argument. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the WARN level. </p> * * @param format the format string * @param arg the argument */ public void warn(String format, Object arg); /** * Log a message at the WARN level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous string concatenation when the logger * is disabled for the WARN level. However, this variant incurs the hidden * (and relatively small) cost of creating an <code>Object[]</code> before invoking the method, * even if this logger is disabled for WARN. The variants taking * @link #warn(String, Object) one and @link #warn(String, Object, Object) two * arguments exist solely in order to avoid this hidden cost.</p> * * @param format the format string * @param arguments a list of 3 or more arguments */ public void warn(String format, Object... arguments); /** * Log a message at the WARN level according to the specified format * and arguments. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the WARN level. </p> * * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void warn(String format, Object arg1, Object arg2); /** * Log an exception (throwable) at the WARN level with an * accompanying message. * * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void warn(String msg, Throwable t); /** * Similar to @link #isWarnEnabled() method except that the marker * data is also taken into consideration. * * @param marker The marker data to take into consideration * @return True if this Logger is enabled for the WARN level, * false otherwise. */ public boolean isWarnEnabled(Marker marker); /** * Log a message with the specific Marker at the WARN level. * * @param marker The marker specific to this log statement * @param msg the message string to be logged */ public void warn(Marker marker, String msg); /** * This method is similar to @link #warn(String, Object) method except that the * marker data is also taken into consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg the argument */ public void warn(Marker marker, String format, Object arg); /** * This method is similar to @link #warn(String, Object, Object) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arg1 the first argument * @param arg2 the second argument */ public void warn(Marker marker, String format, Object arg1, Object arg2); /** * This method is similar to @link #warn(String, Object...) * method except that the marker data is also taken into * consideration. * * @param marker the marker data specific to this log statement * @param format the format string * @param arguments a list of 3 or more arguments */ public void warn(Marker marker, String format, Object... arguments); /** * This method is similar to @link #warn(String, Throwable) method * except that the marker data is also taken into consideration. * * @param marker the marker data for this log statement * @param msg the message accompanying the exception * @param t the exception (throwable) to log */ public void warn(Marker marker, String msg, Throwable t); /** * Is the logger instance enabled for the ERROR level? * * @return True if this Logger is enabled for the ERROR level, * false otherwise. */ public boolean isErrorEnabled(); /** * Log a message at the ERROR level. * * @param msg the message string to be logged */ public void error(String msg); /** * Log a message at the ERROR level according to the specified format * and argument. * <p/> * <p>This form avoids superfluous object creation when the logger * is disabled for the ERROspringboot系列一:默认日志logback配置解析
前言今天来介绍下SpringBoot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢如何引入日志?日志输出格式以及输出方式如何配置?代码中如何使用?正文SpringBoot在所有内部日志中使用CommonsLogging,但是... 查看详情
springboot实践:logback日志配置
...、logback是什么?工作中一直用的是log4j日志框架,接触了SpringBoot后,因为logback是默认框架,才开始进行了解。来看下官网的相关介绍。 可以看到,logback是log4j的进化版,是为了替代log4j的。logback分三个模块:logback-core:核... 查看详情
springboot整合+logback日志配置
本次演示的代码结构如下,基于maven,整合SpringBoot、Spring、Mybaits的SSM框架。同时测试logback日志框架的使用及配置。 1.创建maven工程,修改pom.xml文件<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema 查看详情
最全的springboot+logback日志配置教程
项目中日志系统是必不可少的,目前比较流行的日志框架有log4j、logback等,可能大家还不知道,这两个框架的作者是同一个人,Logback旨在作为流行的log4j项目的后续版本,从而恢复log4j离开的位置。另外slf4j(SimpleLoggingFacadeforJava)... 查看详情
springboot之logback日志最佳实践
一、SpringBoot日志介绍 SpringBoot对所有内部日志记录使用了CommonsLogging,但是底层日志实现是开放的。为JavaUtil日志记录、Log4J2和Logback提供了缺省配置。在每种情况下,日志记录器都预先配置为使用控制台输出和可选的文件输... 查看详情
springboot:日志配置-logback
一、简介支持日志框架:JavaUtilLogging,Log4J2andLogback,默认是使用logbacklogback配置方式springboot默认会加载classpath:logback-spring.xml或者classpath:logback-spring.groovy 使用自定义配置文件,配置方式为:logging.config=classpath:logback-ro 查看详情
springboot整合+logback日志配置(代码片段)
本次演示的代码结构如下,基于maven,整合SpringBoot、Spring、Mybaits的SSM框架。同时测试logback日志框架的使用及配置。1.创建maven工程,修改pom.xml文件<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi= 查看详情
springboot配置保存日志文件
参考技术Aspringboot日志配置:名称只要是一logback开头就行,测试使用log.xml并不会生成日志。合法名称:logback.xml、logback-spring.xml备注:要配置logback-spring.xml,springboot会默认加载此文件,为什么不配置logback.xml,因为logback.xml会先app... 查看详情
springboot日志logback配置
1<?xmlversion="1.0"encoding="UTF-8"?>2<!--3scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。4scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒;当sca... 查看详情
springboot2.x整合slf4j+logback日志框架(代码片段)
目录1、Slf4j及Logback简介2、日志级别3、maven依赖3.1springboot2maven配置文件3.2普通maven项目的配置文件4、logback、springboot配置说明4.1 logback.xml 和logback-spring.xml的区别4.2logback.xml配置文件的加载机制4.3SpringBoot结合logback配置说明4.3.1logb... 查看详情
springboot日志logback配置
...考技术A在前一个项目simple-boot-demo项目中配置日志相关。SpringBoot日志依赖为:spring-boot-starter-logging,不过对于web项目,已经引入了spring-boot-starter-web,默认已经自带了日志的spring-boot-starter-logging,已经默认引入了logback实现,只需... 查看详情
springboot日志配置
SpringBoot日志的配置如果不配置日志,默认配置是base.xml配置日志在resource目录下新建logback.xml<?xmlversion="1.0"encoding="UTF-8"?><configurationdebug="false"><!--定义日志文件的存储地址勿在LogBack的配置中使用相对路径--><propertyname 查看详情
springboot项目使用logback把日志输出到控制台或输出到文件
...初始化,可从控制台输出信息中查看加载的配置文件。在Springboot项目中可以自定义logback配置文件名及文件位置要想让Springboot项目识别到该logback配置文件,只需要在Springboot配置文件中定义好配置文件的加载路径即可如下所示:... 查看详情
13.7springboot集成日志系统logback的几个问题
...properties配置文件中value后面有空格。让人感到疑惑的是,SpringBoot居然没有对application.properties配置文件value末端作空格trim处理。配置内容:报错日志:涉及报错的源码在org.springframework.util.ResourceUtils通过源码,我们可以看出spring... 查看详情
springboot日志管理配置logback-spring.xml
几种常见的日志Log4j:是最早的日志框架,是apach旗下的,可以单独使用,也可配合日志框架JCL使用;Log4j2:apach旗下的关于log4j的升级版;Logback:是基于slf4j接口实现的一套日志框架组件;(Logback是由log4j创始人设计的又一个开... 查看详情
springboot整合logback集成elk实现日志的汇总分析统计和检索功能(代码片段)
在SpringBoot当中,默认使用logback进行log操作。logback支持将日志数据通过提供IP地址、端口号,以Socket的方式远程发送。在SpringBoot中,通常使用logback-spring.xml来进行logback配置。首先、创建一个elk的springboot项目,然后先对logback... 查看详情
springboot使用默认的logback配置logback-spring.xml每天一个日志文件(代码片段)
...ring.xml内容部署启动(2)application.yml放在resources下使用logbackspringboot配置每天一个日志文件logback-spring.xmlspringboot工程配置文件可以放在多个地方,可以直接放在resources目录下,也可以放在根目录下的con 查看详情
springboot2.x整合slf4j+logback日志框架(代码片段)
目录1、Slf4j及Logback简介2、日志级别3、maven依赖3.1springboot2maven配置文件3.2普通maven项目的配置文件4、logback、springboot配置说明4.1 logback.xml 和logback-spring.xml的区别4.2logback.xml配置文件的加载机制4.3SpringBoot结合logback配置说明4.3.1logb... 查看详情