SpringBoot整合logback日志框架
在resource下创建一个名称为 logback-spring.xml文件
<configuration> <!--日志文件夹存放的名称--> <contextName>demo-test</contextName> <!--各项目日志保存统一父目录->路径自己设置绝对路径和相对路径都可以--> <property name="LOG_DIR" value="./log/" /> <!--最大保存天数--> <property name="KEEP_MAX_DAY" value="180" /> <!--总最大保存文件大小--> <property name="KEEP_TOTAL_MAX_SIZE" value="5GB" /> <!--单文件最大保存文件大小--> <property name="KEEP_FILE_MAX_SIZE" value="100MB" /> <!--输出到控制台--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!--输出到ERROR事件记录文件--> <appender name="errorFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${LOG_DIR}/${CONTEXT_NAME}/error/${CONTEXT_NAME}-error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/error/${CONTEXT_NAME}-error-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize> <maxHistory>${KEEP_MAX_DAY}</maxHistory> <totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!--输出到WARN事件记录文件--> <appender name="warnFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${LOG_DIR}/${CONTEXT_NAME}/warn/${CONTEXT_NAME}-warn.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/warn/${CONTEXT_NAME}-warn-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize> <maxHistory>${KEEP_MAX_DAY}</maxHistory> <totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!--输出到INFO事件记录文件--> <appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${LOG_DIR}/${CONTEXT_NAME}/info/${CONTEXT_NAME}-info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_DIR}/${CONTEXT_NAME}/info/${CONTEXT_NAME}-info-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxFileSize>${KEEP_FILE_MAX_SIZE}</maxFileSize> <maxHistory>${KEEP_MAX_DAY}</maxHistory> <totalSizeCap>${KEEP_TOTAL_MAX_SIZE}</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework.jdbc.core" level="debug" additivity="false"> <appender-ref ref="console" /> </logger> <logger name="com.springboot.web.dynamic_debug" level="debug" additivity="false"> <appender-ref ref="console" /> </logger> <root level="info"> <appender-ref ref="console" /> <appender-ref ref="errorFile" /> <appender-ref ref="warnFile" /> <appender-ref ref="infoFile" /> </root> </configuration>
然后yml配置添加
logging:
config: classpath:logback-spring.xml
level:
com.example.demotest.*: debug
com.example.demotest.* 这个是项目的包名 根据自己实际的来
然后启动就可以了
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)