springboot项目添加logback日志

2024-10-09 09:53:45 浏览数 (1)

1.application.yml

配置日志文件路径:

代码语言:javascript复制
logging:
    config: classpath:logback.xml
    file: /usr/local/log/projectName/projectName.log

2.logback.xml文件

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <property name="LOG_HOME" value="/usr/local/log/projectName"/>
    <property name="FILE_LOG_NAME" value="projectName"/>
    <!-- root日志级别 -->
    <property name="LOG_LEVEL" value="INFO"/>

	<!-- 控制台日志 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %-5level %class{36}#%M [line:%L] - %msg%xEx%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${LOG_LEVEL}</level>
        </filter>
    </appender>

	<!-- 文件日志 -->
    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/${FILE_LOG_NAME}.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${FILE_LOG_NAME}-%d{yyyy-MM-dd}-%i.log</FileNamePattern>
            <MaxHistory>100</MaxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>200MB</MaxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{X-B3-TraceId:-},%X{X-B3-SpanId:-}] %-5level %class{36}#%M [line:%L] - %msg%xEx%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${LOG_LEVEL}</level>
        </filter>
    </appender>

	
	<!-- 项目级别 根目录 -->
    <logger name="org.corp.core" level="INFO" additivity="false">
        <appender-ref ref="fileLog"/>
        <appender-ref ref="consoleLog"/>
    </logger>
    <root level="INFO">
        <appender-ref ref="fileLog"/>
        <appender-ref ref="consoleLog"/>
    </root>

</configuration>

0 人点赞