spring-boot-learning 日志相关

sprint-boot 日志

 

 

市面上的日志框架;

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....

 

 

SpringBoot:底层是Spring框架,Spring框架默认是用JCL;‘

==SpringBoot选用 SLF4j和logback;==

 

日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法;

给系统里面导入slf4j的jar和 logback的实现jar

使用手册:http://www.slf4j.org/manual.html

复制代码
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}
复制代码

 

 

 

如果我们的应用当中存在多个框架,而且我们的每个框架使用的日志实现都不一样,

所以我们需要将日志进行统一的转换到一个实现框架里面:

 统一日志记录,即使是别的框架和我一起统一使用slf4j抽象日志框架进行输出

 

 

 

springBoot日志关系
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

SpringBoot使用spring-boot-starter-logging来做日志功能

 

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

 

 

 

1)、SpringBoot底层也是使用slf4j+logback的方式进行日志记录

​2)、SpringBoot也把其他的日志都替换成了slf4j;
中间替换包如下:

 

 

==SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,

引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;

排除的格式;

复制代码
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
复制代码

 

使用日志

  • ERROR 错误信息
  • WARN 警告信息
  • INFO 一般信息
  • DEBUG 调试信息
  • TRACE 跟踪信息

默认配置

复制代码
@SpringBootTest
class SpLoggingApplicationTests {
    //记录器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Test
    void contextLoads() {
        //日志的级别,由低到高trace debug  info  warn  error
        //调整输出日志级别,日志只会在这个级别或者以后的高级别打印
        // //上线只能只想输出warn,和error级别的日志
        logger.trace("只是日志");
        logger.debug("这是debug日志");
        //springboot 默认给我使用的使info级别,
        //没有指定级别的就用springboot 默认规定的级别,root级别
        logger.info("这是info信息");
        logger.warn("这是warn日志");
        logger.error("error rizhi ");


    }

}
复制代码

结果 :

 

因为设置的默认的logging.level是info所以debug和trance都没有显示出来
在配置文件里面设置就可以了,logging.level.com = debug ,必须在level后面还要加上路径

修改结果:

 

可以在application.properties里面修改日志配置

复制代码
logging.level.com.quan.splogging=trace#指定那个包或者哪个类的输出日志级别
#不指定路径就在当前项目下生成springbot.log日志
#可以指定完整的路径名称
#logging.file.name=quan.log
#一般指定目录
#不指定磁盘,默认在所在磁盘的根路径下创建spring文件夹里面的log文件夹
#使用spring.log为文件名字
logging.file.path=E:/spring/log
#在控制台输出的日志格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
#指定文件中的日志输出格式
logging.pattern.file==
复制代码

列子:

logging:
  level:
    com: trace
  pattern:
    console: 时间:%d{yyyy~MM~dd=HH:mm:ss} 线程名字:[[%thread]] 详情:%msg%n

热:

时间:2020~08~06=15:02:22 线程名字:[[main]] 详情:Running with Spring Boot v2.3.2.RELEASE, Spring v5.2.8.RELEASE
时间:2020~08~06=15:02:22 线程名字:[[main]] 详情:No active profile set, falling back to default profiles: default
时间:2020~08~06=15:02:23 线程名字:[[main]] 详情:Initializing ExecutorService 'applicationTaskExecutor'
时间:2020~08~06=15:02:23 线程名字:[[main]] 详情:Started HuolalaApplicationTests in 16.355 seconds (JVM running for 17.283)

时间:2020~08~06=15:02:23 线程名字:[[main]] 详情:djdijdi
时间:2020~08~06=15:02:23 线程名字:[[main]] 详情:debugdebug
时间:2020~08~06=15:02:23 线程名字:[[main]] 详情:errorerror

 

logging.file.name和logging.file.path的区别:

 日志格式的内容修改含义:

复制代码
    日志输出格式:
        %d表示日期时间,
        %thread表示线程名,
        %-5level:级别从左显示5个字符宽度
        %logger{50} 表示logger名字最长50个字符,否则按照句点分割。 
        %msg:日志消息,
        %n是换行符
复制代码

 

指定配置:

给类路径下放上每个日志框架自己的配置文件即可:SpringBoot就不适用默认配置了,

 

logback.xml:直接就被日志框架识别了;

logback-spring.xml日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能 

没使用Profile的:

<configuration>
<!--      设置日志输出的格式,以便后面使用root引用   -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>logbackyml %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

<!-- logger的值可以不区分大小写   用于设置那个包的或者具体类的日志打印级别       -->
    <logger name="com.quan.learning" level="INFO"/>

    <!-- Strictly speaking, the level attribute is not necessary since -->
    <!-- the level of the root level is set to DEBUG by default.       -->
<!--    root节点必须在appender下面,主要的作用就是,对appdender进行管理
需要哪个就加入哪个,
而且这里的日志级别是总级别,最低能够打出的日志级别,如果appender比他高就取高的
-->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

 

 

复制代码
            <springProfile name="dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n</pattern>
            </springProfile>
            <springProfile name="!dev">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n</pattern>
            </springProfile>
复制代码

如果使用logback.xml作为日志配置文件,还要使用springProfile那就会报错

 

切换日志框架:
可以按照slf4j的日志适配图,进行相关的切换;

排除所需要排除的,

添加所需要的东西

例子:SLF4j+log4j2

直接

直接exclusion spring-boot-starter-logging,然后加上要实现的

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--引入新的log实现框架达到SLF4j+log4j,不过一般都不需要,因为有bug-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>

配置:

logging:
  level:
    com: trace
  pattern:
    console: log4j2的日志===时间:%d{yyyy~MM~dd=HH:mm:ss} 线程名字:[[%thread]] 详情:%msg%n

结果:

log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[main]] 详情:Initializing ExecutorService 'applicationTaskExecutor'
log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[main]] 详情:Started HuolalaApplicationTests in 21.406 seconds (JVM running for 27.56)

log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[main]] 详情:djdijdi
log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[main]] 详情:debugdebug
log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[main]] 详情:errorerror

log4j2的日志===时间:2020~08~06=16:22:47 线程名字:[[SpringContextShutdownHook]] 详情:Shutting down ExecutorService 'applicationTaskExecutor'

 

 

posted @ 2020-08-23 20:45  小丑quan  阅读(256)  评论(0)    收藏  举报