一、简介

支持日志框架:Java Util Logging, Log4J2 and Logback,默认是使用logback


对于日志的快捷方式:@Slf4j注解

    springBoot(4):日志配置-logback_Boot

注意:@Slf4j注解需要导入lombok包,pom如下:

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>


对于log的connot resolve symbol log的红叉提示,解决:

  1.用快捷键Ctrl+Alt+S打开:Settings→Plugins→Browse repositories

 2.输入lom后选择Install,安装插件

     springBoot(4):日志配置-logback_日志配置_02


logback配置方式

spring boot默认会加载classpath:logback-spring.xml或者classpath:logback-spring.groovy

 

使用自定义配置文件,配置方式为:

logging.config=classpath:logback-roncoo.xml

注意:不要使用logback这个来命名,否则spring boot将不能完全实例化

logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <!-- 文件输出格式 -->
    <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
    <!-- test文件路径 -->
    <property name="TEST_FILE_PATH" value="G:/workspace/logs_workspace/hystrix-dashboard/logs" />
    <!-- pro文件路径 -->
    <property name="PRO_FILE_PATH" value="/opt/hystrix-dashboard/logs" />

    <!-- 开发环境 -->
    <springProfile name="dev">
        <appender name="CONSOLE2" class="ch.qos.logback.classic.net.SMTPAppender">
            <smtpHost>smtp.163.com</smtpHost>
            <username>发送的@163.com</username>
            <password>密码</password>
            <to>a1754966750@qq.com</to>
            <from>发送的@163.com</from>
            <subject>hystrix-dashboard</subject>
            <layout class="ch.qos.logback.classic.html.HTMLLayout" >
                <pattern>${PATTERN}</pattern>
            </layout>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <!--满足结果就写入,不满足结果就拒绝写入 -->
                <level>ERROR</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </appender>
        <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${PATTERN}</pattern>
            </encoder>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <!--满足结果就写入,不满足结果就拒绝写入 -->
                <level>debug</level>
            </filter>
        </appender>

        <logger name="com.example.demo" level="info"/>
        <logger name="org.springframework" level="info"/>
        <logger name="org.mybatis" level="debug"/>
        <logger name="org.apache" level="info"/>
        <logger name="org.hibernate" level="info"/>
        <logger name="com.netflix" level="info"/>

        <root>
            <appender-ref ref="CONSOLE2" />
            <appender-ref ref="CONSOLE" />
        </root>
    </springProfile>

    <!-- 测试环境 -->
    <springProfile name="test">
        <!-- 每天产生一个文件 -->
        <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 文件路径 -->
            <file>${TEST_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 文件名称 -->
                <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
                <!-- 文件最大保存历史数量 -->
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>

            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>

        <root level="info">
            <appender-ref ref="TEST-FILE" />
        </root>
    </springProfile>

    <!-- 生产环境 -->
    <springProfile name="prod">
        <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${PRO_FILE_PATH}</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
                <MaxHistory>100</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>${PATTERN}</pattern>
            </layout>
        </appender>

        <root level="warn">
            <appender-ref ref="PROD_FILE" />
        </root>
    </springProfile>
</configuration>