有个任务停留在任务列表中好久了:使用Appenders 完成 loger4j 的日志推送
,始终没有成功实现。追其缘由,仍然是官方的文档没有认真看。在spring-boot
的项目中看到log4j
,就想固然的认为Spring-boot
使用的是log4j
,而后不假思索的去google
。最终致使的就是:功能没有实现,并且还浪费了不少没必要要的时间,最后:仍是老老实实的回来阅读spring-boot
的官方文档。html
本文主要对官方文档Logging
部分进行解读。
原文地址:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html.java
若是你使用的是否是最新版本,那么应该使用https://docs.spring.io/spring-boot/docs/版本号/reference/htmlsingle/#boot-features-logging web
如:https://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#boot-features-loggingspring
在web开中,咱们仅须要依赖于spring-boot-starter-web
便自动启用了日志系统Logback
。服务器
若是仅仅是想改变日志的等级,则能够直接使用logging.level
前缀在application.properties
中进行设置,好比:app
logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=ERROR
除了控制日志的等级外,还可使用logging.file
来定义日志输入到的文件位置。ide
若是咱们还想配置更多选项,则能够在classpath
(resourse)中定义logback.xml
或logback-spring.xml
。spring-boot
找到logback.xml
或logback-spring.xml
,复制如下基本内容:ui
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="DEBUG"/> </configuration>
使用idea
的ctrl+o
来打开spring-boot jar中的base.xml
,咱们会看到配置信息包含一些特殊的字符,解读以下:google
${PID}
当前的进程ID${LOG_FILE}
若是设置了logging.file
,则使用logging.file
作为日志输入文件。${LOG_PATH}
同上.指定日志输出路径。${LOG_EXCEPTION_CONVERSION_WORD}
..咱们本身定义日志输入的方式和字符串时,固然也可使用它们了。
若是咱们想禁用控制台的日志输出(生产环境中,咱们的确是要这么作的),而后把日志写入某个日志文件的话。那么须要新建logback-spring.xml
,并引入file-appender.xml
,好比:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="FILE" /> </root> </configuration>
而后:在application.properties
定义logging.file
来指定日志文件位置.
例:
logging.file=myapplication.log
再看看上面是怎么回事:打开org/springframework/boot/logging/logback/file-appender.xml
内容以下:
<?xml version="1.0" encoding="UTF-8"?> <!-- File appender logback configuration provided for import, equivalent to the programmatic initialization performed by Boot --> <included> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_FILE}.%i</fileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> </included>
注意:这里面有个<appender name="FILE"
,指定了appender
名称为FILE
,对应logback-spring.xml
的如下语句:
<root level="INFO"> <!--日志等级--> <appender-ref ref="FILE" /> <!--指定appender为FILE,则前面咱们刚刚找到的name值--> </root>
有了以上内容,咱们知道了以下知识点:
spring-boot
默认使用的是Logback
而非log4j
。logback-spring.xml
来细化Logback
的配置。Logback
中,是能够指定使用不一样的appender
来定义日志的输出的。appender
来达到将日志输出到咱们的日志服务器,从而达到系统监控的目的呢?