• 11、Logback日志框架介绍和SpringBoot整合实战 2节课


    1、新日志框架LogBack介绍
         简介:日志介绍和新日志框架Logback讲解

        1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等
        
         2、logback介绍:基于Log4j基础上大量改良,不能单独使用,推荐配合日志框架SLF4J来使用
             logback当前分成三个模块:logback-core,logback-classic和logback-access;
             logback-core是其它两个模块的基础模块

        3、Logback的核心对象:
             Logger:日志记录器
             Appender:指定日志输出的目的地,目的地可以是控制台,文件
             Layout:日志布局 格式化日志信息的输出

           
         4、日志级别:DEBUG < INFO < WARN < ERROR

            ===========log4j示例===========       
              ### 设置###
             log4j.rootLogger = debug,stdout,D,E

            ### 输出信息到控制抬 ###
             log4j.appender.stdout = org.apache.log4j.ConsoleAppender
             log4j.appender.stdout.Target = System.out
             log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
             log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

            ### 输出DEBUG 级别以上的日志到=D://logs/error.log ###
             log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
             log4j.appender.D.File = D://logs/log.log
             log4j.appender.D.Append = true
             log4j.appender.D.Threshold = DEBUG
             log4j.appender.D.layout = org.apache.log4j.PatternLayout
             log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

            ### 输出ERROR 级别以上的日志到=D://logs/error.log ###
             log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
             log4j.appender.E.File =E://logs/error.log
             log4j.appender.E.Append = true
             log4j.appender.E.Threshold = ERROR
             log4j.appender.E.layout = org.apache.log4j.PatternLayout
             log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

            ===========logback============
        
         4、Log4j日志转换为logback在线工具(支持log4j.properties转换为logback.xml,不支持 log4j.xml转换为logback.xml)
          https://logback.qos.ch/translator/

    2、SpringBoot2.x日志讲解和自定义Logback配置实战
         简介:讲解SpringBoot2.x整合Logback配置实战

            1、官网介绍:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-logging

      日志节选:

      26.1 Log Format

    The default log output from Spring Boot resembles the following example:

    2014-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
    2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
    2014-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
    2014-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

    The following items are output:

    • Date and Time: Millisecond precision and easily sortable.
    • Log Level: ERRORWARNINFODEBUG, or TRACE.
    • Process ID.
    • --- separator to distinguish the start of actual log messages.
    • Thread name: Enclosed in square brackets (may be truncated for console output).
    • Logger name: This is usually the source class name (often abbreviated).
    • The log message.

               各个组件案例:https://logback.qos.ch/manual/index.html
            
             2、分析SpringBoot启动日志
                 1)默认情况下,Spring Boot将日志输出到控制台

            3、整合Logback实战
                 1)创建 日志文件logback-spring.xml,官方推荐 -spring.xml结尾
                     默认加载加载配置顺序 logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
                
                 注释:
                     <configuration> 子节点
                     <appender></appender>                      
                     <logger></logger>
                     <root></root>(要加在最后)    

      配置文件代码:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <configuration>
     3 
     4     <appender name="consoleApp" class="ch.qos.logback.core.ConsoleAppender">
     5         <layout class="ch.qos.logback.classic.PatternLayout">
     6             <pattern>
     7                 %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
     8             </pattern>
     9         </layout>
    10     </appender>
    11 
    12     <appender name="fileInfoApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
    13         <filter class="ch.qos.logback.classic.filter.LevelFilter">
    14             <level>ERROR</level>
    15             <onMatch>DENY</onMatch>
    16             <onMismatch>ACCEPT</onMismatch>
    17         </filter>
    18         <encoder>
    19             <pattern>
    20                 %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
    21             </pattern>
    22         </encoder>
    23         <!-- 滚动策略 -->
    24         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    25             <!-- 路径 -->
    26             <fileNamePattern>app_log/log/app.info.%d.log</fileNamePattern>
    27         </rollingPolicy>
    28     </appender>
    29 
    30     <appender name="fileErrorApp"
    31         class="ch.qos.logback.core.rolling.RollingFileAppender">
    32         <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    33             <level>ERROR</level>
    34         </filter>
    35         <encoder>
    36             <pattern>
    37                 %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
    38             </pattern>
    39         </encoder>
    40 
    41         <!-- 设置滚动策略 -->
    42         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    43             <!-- 路径 -->
    44             <fileNamePattern>app_log/log/app.err.%d.log</fileNamePattern>
    45 
    46             <!-- 控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动, 且<maxHistory> 是1,则只保存最近1个月的文件,删除之前的旧文件 -->
    47             <MaxHistory>1</MaxHistory>
    48 
    49         </rollingPolicy>
    50     </appender>
    51 
    52     <!-- 控制总的输出级别 -->
    53     <root level="INFO">
    54         <appender-ref ref="consoleApp" />
    55         <appender-ref ref="fileInfoApp" />
    56         <appender-ref ref="fileErrorApp" />
    57     </root>
    58 </configuration>

    设置完成后会自动创建文件夹

       

  • 相关阅读:
    MDaemon运行六年方法
    [转帖]如何将一张图片写入数据库SQL,并能正确在C#网页中显示
    【转】CSS 与 HTML5 响应式图片
    【原】设计稿中字体pt与px之间的转化
    【原】[webkit移动开发笔记]之使用chrome进行手机开发
    【原】[webkit移动开发笔记]之兼容iPhone4和iPhone5的方法
    【原】[webkit移动开发笔记]之空链接是使用javascript:void(0)还是使用#none
    CSS3Ps Photoshop图层特效转CSS3代码
    【原】[webkit移动开发笔记]之CSS3径向渐变
    【转】浏览器加载渲染网页过程解析总结(三)
  • 原文地址:https://www.cnblogs.com/116970u/p/10261909.html
Copyright © 2020-2023  润新知