原文java
切换日志级别为 DEBUGspring
使用命令行,能够在其后添加 --debugapache
application.properties 配置 debug=truespringboot
spring.output.ansi.enabled=DETECT 检测终端是否支持ANSI输出,支持则打印彩色日志。默认为 NEVER,不开启。还能够设在置为 ALWAYS。app
文件输出,在 application.properties 中设置 loggin.file 或 logging.path 属性。框架
包级别控制,配置 logging.level.*=LEVEL。好比 logging.level.com.didispace=DEBUG:com.didispace包下全部class以DEBUG级别输出。logging.level.root=WARN:root日志以WARN级别输出。maven
自定义日志配置(不使用自带的日志系统)spring-boot
命名规则spa
Log4j:log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml命令行
Log4j2:log4j2-spring.xml, log4j2.xml
除此以外,还能够 使用 logging.config=log4j2-spring.xml 指定文件的位置。
定义输出格式
logging.pattern.console:定义输出到控制台的样式
logging.pattern.file:定义输出到文件的样式
配置 log 显示行号:
logging: pattern: console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}.%M[%line] - %msg%n" file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}.%M[%line] - %msg%n"
=======================================================================================
说明:
关于采用 Logback
配置外化文件:在外化配置文件 application.properties 中配置
debug=true logging.config=classpath:logger/logback-spring.xml logging.file=target/logs/spring-boot-logging.log
自定义 logback-spring.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/console-appender.xml" /> <appender name="TIME_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.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern> <maxHistory>365</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="TIME_FILE" /> </root> </configuration>
关于引入 Log4j2
加入依赖:在使用 maven建立spring boot 工程时,引入了 spring-boot-starter,其中包含了 spring-boot-starter-logging,采用的是 spring boot 默认的日志框架 Logback。在使用 Log4j2 以前须要排除该包的依赖,并引入 log4j2 的依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
配置外化文件:
logging.config=classpath:logger/log4j2-spring.xml
自定义 log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="debug" strict="true" name="XMLConfigTest" packages="org.apache.logging.log4j.test"> <Properties> <Property name="filename">target/logs/spring-boot-logging.log</Property> </Properties> <Filter type="ThresholdFilter" level="trace"/> <Appenders> <Appender type="Console" name="STDOUT"> <Layout type="PatternLayout" pattern="%m MDC%X%n"/> <Filters> <Filter type="MarkerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/> <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"/> </Filters> </Appender> <Appender type="Console" name="FLOW"> <Layout type="PatternLayout" pattern="%C{1}.%M %m %ex%n"/><!-- class and line number --> <Filters> <Filter type="MarkerFilter" marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/> <Filter type="MarkerFilter" marker="EXCEPTION" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> </Appender> <Appender type="File" name="File" fileName="${filename}"> <Layout type="PatternLayout"> <Pattern>%d %p %C{1.} [%t] %m%n</Pattern> </Layout> </Appender> <Appender type="List" name="List"> </Appender> </Appenders> <Loggers> <Logger name="org.apache.logging.log4j.test1" level="debug" additivity="false"> <Filter type="ThreadContextMapFilter"> <KeyValuePair key="test" value="123"/> </Filter> <AppenderRef ref="STDOUT"/> </Logger> <Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false"> <AppenderRef ref="File"/> </Logger> <Root level="trace"> <AppenderRef ref="List"/> </Root> </Loggers> </Configuration>