Spring boot官网
https://spring.io/
点击projects中的spring boot
点击learn中Reference Doc
点击Core Features
点击logging
配置
spring boot 默认是使用Logback日志记录。
输出日志:配置方式、输出位置、输出格式、级别控制
配置方式
1.使用spring boot 的配置文件 如application.properties application.yml
2.指定logback的配置文件
方式一
指定文件名或文件路径,文件名>文件路径,两属性应该不可以一起使用。
1
| logging.file.name=my.log
|
1
| logging.file.path=/Users/qichuanhan/logs/
|
滚动:
logging.logback.rollingpolicy.file-name-pattern |
用于创建日志存档的文件名模式。 |
logging.logback.rollingpolicy.clean-history-on-start |
如果应在应用程序启动时进行日志归档清理。 |
logging.logback.rollingpolicy.max-file-size |
归档前日志文件的最大大小。 |
logging.logback.rollingpolicy.total-size-cap |
在被删除之前可以占用的最大大小的日志档案。 |
logging.logback.rollingpolicy.max-history |
要保留的存档日志文件的最大数量(默认为 7)。 |
方式二
创建logback.xml在classpath
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| <?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="log.path" value="/home/trans/logs" /> <property name="log.pattern" value="%d{HH:mm:ss.SSS} -[%X{traceId}]- [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}/sys-info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}/sys-error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}/sys-user.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender>
<appender name = "file_sql" class = "ch.qos.logback.core.rolling.RollingFileAppender"> <file>${log.path}/sys-sql.log</file> <encoder> <pattern>${log.pattern}</pattern> </encoder> <rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/sys-sql.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>2048MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>5</maxHistory> <totalSizeCap>15GB</totalSizeCap> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender>
<logger name="com.trans" level="info" /> <logger name="org.springframework" level="warn" />
<logger name="sys-user" level="info"> <appender-ref ref="sys-user"/> </logger>
<springProfile name="!production"> </springProfile> <springProfile name="dev|test"> <logger name="com.trans.*.mapper" level="TRACE"> <appender-ref ref="file_sql"/> </logger>
<root level="info"> <appender-ref ref="console" /> <appender-ref ref="file_info" /> <appender-ref ref="file_error" /> <appender-ref ref="file_sql" /> </root> </springProfile> <springProfile name="prod"> <logger name="com.trans.*.mapper" level="DEBUG"> <appender-ref ref="file_sql"/> </logger> <root level="info"> <appender-ref ref="console" /> <appender-ref ref="file_info" /> <appender-ref ref="file_error" /> <appender-ref ref="file_sql" /> </root> </springProfile>
</configuration>
|