最近 在搞一个0-1的项目 就想把 服务器日志配置 记录一下
我们使用的是 单体微服务项目
首先你需要一个xml
- "1.0" encoding="UTF-8"?>
- <configuration>
-
-
- <springProperty scope="context" name="logPath" source="log.path" defaultValue="logs"/>
- <springProperty scope="context" name="logMaxFileSize" source="log.maxFileSize" defaultValue="50MB"/>
- <springProperty scope="context" name="logMaxHistory" source="log.maxHistory" defaultValue="45"/>
- <springProperty scope="context" name="totalLogSizeCap" source="log.totalLogSizeCap" defaultValue="20GB"/>
- <springProperty scope="context" name="errorLogSizeCap" source="log.errorLogSizeCap" defaultValue="5GB"/>
-
-
-
-
- <springProfile name="dev | uat | ksh | ysh">
-
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
-
- layout>
- encoder>
- appender>
-
- <root level="info">
- <appender-ref ref="STDOUT"/>
- root>
-
- springProfile>
-
-
-
-
-
- <springProfile name="dev">
- <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
-
- <file>${logPath}/log_error.logfile>
-
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
- <fileNamePattern>${logPath}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
-
- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <maxFileSize>${logMaxFileSize}maxFileSize>
- timeBasedFileNamingAndTriggeringPolicy>
- <maxHistory>${logMaxHistory}maxHistory>
- <totalSizeCap>${errorLogSizeCap}totalSizeCap>
- <cleanHistoryOnStart>truecleanHistoryOnStart>
- rollingPolicy>
-
- <append>trueappend>
-
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
-
- layout>
- encoder>
-
- <filter class="ch.qos.logback.classic.filter.LevelFilter">
- <level>errorlevel>
- <onMatch>ACCEPTonMatch>
- <onMismatch>DENYonMismatch>
- filter>
- appender>
-
-
- <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
-
- <file>${logPath}/log_total.logfile>
-
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-
- <fileNamePattern>${logPath}/total/log-total-%d{yyyy-MM-dd}.%i.logfileNamePattern>
-
- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
- <maxFileSize>${logMaxFileSize}maxFileSize>
- timeBasedFileNamingAndTriggeringPolicy>
- <maxHistory>${logMaxHistory}maxHistory>
- <totalSizeCap>${totalLogSizeCap}totalSizeCap>
- <cleanHistoryOnStart>truecleanHistoryOnStart>
- rollingPolicy>
-
- <append>trueappend>
-
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
-
- layout>
- encoder>
- appender>
-
- <root level="info">
- <appender-ref ref="FILE_ERROR"/>
- <appender-ref ref="FILE_ALL"/>
- root>
- springProfile>
-
- configuration>
-
然后就是 pom文件里面添加上对应的依赖
- <exclusions>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-simpleartifactId>
- exclusion>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-apiartifactId>
- exclusion>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-nopartifactId>
- exclusion>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-log4j12artifactId>
- exclusion>
- <exclusion>
- <groupId>org.slf4jgroupId>
- <artifactId>slf4j-jdk14artifactId>
- exclusion>
- <exclusion>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- exclusion>
- <exclusion>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-loggingartifactId>
- exclusion>
- exclusions>
然后 yml 里面
logging.config=classpath:logback-spring.xml logging.level.root=info logging.level.com=debug logging.level.org.springframework.boot=ERROR log.path=/Users/meiyifan/code/logs log.maxFileSize=50MB log.maxHistory=45 log.totalLogSizeCap=20GB log.errorLogSizeCap=5GB systemLog.enable=true systemLogretentiondays=30
然后再服务器对应的创建 存储日志的文件 logs