• springboot项目logback配置文件示例


    logback-spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/defaults.xml" />
        <!-- 开启jmx功能来实现动态修改日志级别 -->
        <jmxConfigurator/><springProperty scope="context" name="springAppName" source="spring.application.name" />
        <!-- Example for logging into the build folder of your project -->
        <property name="LOG_FILE" value="/data/logs/crm/${springAppName}" /><!-- You can override this to have a custom pattern -->
        <property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
    
        <springProfile name="local,dev,test">
            <!-- Appender to log to console -->
            <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <!-- Minimum logging level to be presented in the console logs -->
                    <level>DEBUG</level>
                </filter>
                <encoder>
                    <pattern>${CONSOLE_LOG_PATTERN}</pattern>
                    <charset>utf8</charset>
                </encoder>
            </appender>
            <root level="INFO">
                <appender-ref ref="console" />
            </root>
            <!--打印sql语句-->
            <!-- <logger name="com.mili.boss.alarm.mapper" level="debug" additivity="false">
                <appender-ref ref="console" />
            </logger> -->
    
        </springProfile>
    
        <springProfile name="test,pre,prod">
            <!-- Appender to log to file -->
            <appender name="flatfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_FILE}.log</file>
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
                    <maxHistory>30</maxHistory>
                </rollingPolicy>
                <encoder>
                    <pattern>${CONSOLE_LOG_PATTERN}</pattern>
                    <charset>utf8</charset>
                </encoder>
            </appender>
    
            <appender name="logstash"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${LOG_FILE}.json</file>
                <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>${LOG_FILE}.json.%d{yyyy-MM-dd}.gz
                    </fileNamePattern>
                    <maxHistory>7</maxHistory>
                </rollingPolicy>
                <encoder
                        class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
                    <providers>
                        <timestamp>
                            <timeZone>UTC+8</timeZone>
                        </timestamp>
                        <pattern>
                            <pattern>
                                {
                                "severity": "%level",
                                "service": "${springAppName:-}",
                                "trace": "%X{X-B3-TraceId:-}",
                                "span": "%X{X-B3-SpanId:-}",
                                "parent": "%X{X-B3-ParentSpanId:-}",
                                "exportable": "%X{X-Span-Export:-}",
                                "pid": "${PID:-}",
                                "thread": "%thread",
                                "class": "%logger{40}",
                                "rest": "%message"
                                }
                            </pattern>
                        </pattern>
                    </providers>
                </encoder>
            </appender><root level="INFO">
                <appender-ref ref="flatfile" />
                <!-- uncomment this to have also JSON logs -->
                <appender-ref ref="logstash"/>
            </root>
        </springProfile>
    </configuration>

    如需开启异步日志,可以参考:https://www.cnblogs.com/yangzhilong/p/10577613.html

  • 相关阅读:
    spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
    SpringMVC静态资源拦截的问题
    初识SpringMVC
    Linux打tar包排除目录中的某个目录
    Angular的forEach无法通return跳出循环问题
    shell脚本编写保存
    Chrome浏览器偶尔提示错误net::ERR_EMPTY_RESPONSE的解决方法
    SVN右键菜单不显示
    JavaScript创建对象常用的两种方法
    JavaScript调试之alert和console.log()的区别
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/10577623.html
Copyright © 2020-2023  润新知