SpringBoot项⽬中控制台⽇志的保存配置操作
SpringBoot 控制台⽇志的保存配置
情景:SpringBoot项⽬中需要把控制台⽇志保存下来。
实现⽅法:
1.在配置⽂件l
配置如下:
logging:
config: l
l
这个⽂件是记录控制台打印⽇志保存的路径配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 分级别异步⽂件⽇志输出配置 -->
<configuration>
<!-- ⽇志级别 -->
<property name="logLevel" value="INFO"></property>
<!-- ⽇志地址 -->
<property name="logPath" value="/usr/server/shop/logs/"></property>
<property name="logPathDebug" value="/usr/server/shop/logs/debug/"></property>
<property name="logPathInfo" value="/usr/server/shop/logs/info/"></property>
<property name="logPathWarn" value="/usr/server/shop/logs/warn/"></property>
<property name="logPathError" value="/usr/server/shop/logs/error/"></property>
<!-- 最⼤保存时间 -->
<property name="maxHistory" value="30"/>
<!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 -->
<property name="queueSize" value="512"></property>
<!--⽇志⽂件名前缀-->
<property name="name" value="shop_prod"></property>
<appender name="STDOUT" class="ch.ConsoleAppender">
<encoder>
<pattern>%black(%contextName -) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{60}) - %gray(%msg%n)</pattern> </encoder>
</appender>
<appender name="FILE_DEBUG" class="ch.olling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logPathDebug}/${name}_debug.log</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${logPathDebug}/${name}_debug.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_INFO" class="ch.olling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logPathInfo}/${name}_info.log</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${logPathInfo}/${name}_info.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_WARN" class="ch.olling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logPathWarn}/${name}_warn.log</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${logPathWarn}/${name}_warn.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder>
</appender>
<appender name="FILE_ERROR" class="ch.olling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${logPathError}/${name}_error.log</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${logPathError}/${name}_error.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>${maxHistory}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder>
</appender>
<appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失⽇志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的⽇志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_DEBUG"/>
</appender>
<appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失⽇志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的⽇志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_INFO"/>
</appender>
<appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失⽇志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的⽇志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_WARN"/>
</appender>
<appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丢失⽇志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的⽇志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
<queueSize>${queueSize}</queueSize>
<appender-ref ref="FILE_ERROR"/>
</appender>
<root level="${logLevel}">
<!-- appender referenced after it is defined -->
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_LOG_DEBUG"/>
<appender-ref ref="ASYNC_LOG_INFO"/>
<appender-ref ref="ASYNC_LOG_WARN"/>
<appender-ref ref="ASYNC_LOG_ERROR"/>
</root>
</configuration>
注意:
<property name="logPath" value="/usr/server/shop/logs/"></property>
中⽇志存放的路径在/usr/server/shop/logs/下存放。
SpringBoot项⽬怎么保存服务的运⾏时⽇志
这个问题相对很简单,只需要完成两个步骤
配置log4j输出⽇志的⽂件
在pom中需要引⽤lombok依赖,版本跟随你的springboot夫版本即可
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds"
debug="false">
<contextName>StudentContext</contextName>
<property name="log.path" value="./logs" />
<!--输出到控制台 -->
<appender name="console" class="ch.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 全部⽇志 -->
<appender name="all" class="ch.olling.RollingFileAppender">
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/student/student.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 输出到warn -->
<appender name="warn" class="ch.olling.RollingFileAppender">
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/student/student.warn.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
log4j2 打印日志<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN⽇志 -->
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 输出到error -->
<appender name="error" class="ch.olling.RollingFileAppender">
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR⽇志 -->
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="all" />
<appender-ref ref="warn" />
<appender-ref ref="error" />
</root>
</configuration>
log4j输出⽇志的⽂件,将其放在resources下即可,注意fileNamePattern这个标签中的东西换成标识你微服务作⽤的,⽐如我写的学⽣管理系统,所以⽤的是student
在application指定此配置⽂件的位置
logging:
level:
comflix.solver.aws.ConfigClusterResolver: WARN
config: l
完毕,当你将项⽬部署到linux服务器。⽇志就会产⽣,每天的⽇志是⼀个⽂件,⾮常⽅便。以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤
家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论