日志管理,springboot
2021-07-20 00:05
                         标签:oid   stdout   应该   实现类   pes   日志功能   EAP   项目   bst    1.市面上的日志框架; 日志门面: SLF4J; 3.SLF4j使用 理想使用情况: 3.2但是,当使用框架时,各框架有自己使用的日志框架,因此需要下述处理方式:  如何让系统中所有的日志都统一到slf4j; 案例1:slf4j+logback    案例二:slf4j+log4j    4.SpringBoot使用它来做日志功能    4.1底层依赖关系 4.2总结: 4)、如果我们要引入其他框架?一定要把这个框架的默认日志依赖移除掉? SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可;   5.使用日志  SpringBoot修改日志的默认配置(application.properties)  指定配置: 例如: 如果使用logback.xml作为日志配置文件,还要使用profile功能,会有以下错误   6.切换日志框架 方法 6.2切换为log4j2        日志管理,springboot 标签:oid   stdout   应该   实现类   pes   日志功能   EAP   项目   bst    原文地址:https://www.cnblogs.com/psy-code/p/9521031.html
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j.... 
2.
日志实现:Logback;
SpringBoot:底层是Spring框架,Spring框架默认是用JCL;‘
SpringBoot选用 SLF4j和logback; 
3.1如何在系统中使用SLF4j   https://www.slf4j.org
以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面的方法;
给系统里面导入slf4j的jar和 logback的实现jar 
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");
    }
}    

a、将系统中其他日志框架先排除出去;
b、用中间包来替换原有的日志框架;
c、我们导入slf4j其他的实现 



1)、SpringBoot底层也是使用slf4j+logback的方式进行日志记录
2)、SpringBoot也把其他的日志都替换成了slf4j;
3)、中间替换包? 
@SuppressWarnings("rawtypes")
public abstract class LogFactory {
    static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J =
    "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";
    static LogFactory logFactory = new SLF4JLogFactory();
}
以排除Spring默认日志框架为例,Spring框架用的是commons-logging; 
package com.zy.log;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorldLog {
    // 记录器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Test
    public void fn(){
        //日志的级别;
        //由低到高 trace
日志输出格式:
	%d表示日期时间,
	%thread表示线程名,
	%‐5level:级别从左显示5个字符宽度
	%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
	%msg:日志消息,
	%n是换行符
	%d{yyyy‐MM‐dd HH:mm:ss.SSS} [%thread] %‐5level %logger{50} ‐ %msg%n
logging.level.com.zy=trace
# 不指定路径在当前项目下生成springboot.log日志
#logging.path=
# 可以指定完整的路径;
#logging.file=F:/springboot.log
# 在当前磁盘的根路径下创建spring文件夹和里面的log文件夹;使用 spring.log 作为默认文件
logging.path=/spring/log
# 在控制台输出的日志的格式
logging.pattern.console=%d{yyyy‐MM‐dd} [%thread] %‐5level %logger{50} ‐ %msg%n
# 指定文件中日志输出的格式
logging.pattern.file=%d{yyyy‐MM‐dd} === [%thread] === %‐5level === %logger{50} ==== %msg%n


springProfile name="staging">
    ‐‐ configuration to be enabled when the "staging" profile is active ‐‐>
    可以指定某段配置只在某个环境下生效
springProfile>
appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    ‐‐
        日志输出格式:
        %d表示日期时间,
        %thread表示线程名,
        %‐5level:级别从左显示5个字符宽度
        %logger{50} 表示logger名字最长50个字符,否则按照句点分割。
        %msg:日志消息,
        %n是换行符
    ‐‐>
    layout class="ch.qos.logback.classic.PatternLayout">
        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>
    layout>
appender>
no applicable action for [springProfile] 
6.1可以按照slf4j的日志适配图,进行相关的切换;
slf4j+log4j的方式; 
dependency>
    groupId>org.springframework.bootgroupId>
    artifactId>spring‐boot‐starter‐webartifactId>
    exclusions>
        exclusion>
            artifactId>logback‐classicartifactId>
            groupId>ch.qos.logbackgroupId>
        exclusion>
        exclusion>
            artifactId>log4j‐over‐slf4jartifactId>
            groupId>org.slf4jgroupId>
        exclusion>
    exclusions>
dependency>
dependency>
    groupId>org.slf4jgroupId>
    artifactId>slf4j‐log4j12artifactId>
dependency>
dependency>
    groupId>org.springframework.bootgroupId>
    artifactId>spring‐boot‐starter‐webartifactId>
    exclusions>
        exclusion>
            artifactId>spring‐boot‐starter‐loggingartifactId>
            groupId>org.springframework.bootgroupId>
        exclusion>
    exclusions>
dependency>
dependency>
    groupId>org.springframework.bootgroupId>
    artifactId>spring‐boot‐starter‐log4j2artifactId>
dependency>