我的个人网站:
http://riun.xyz
在本机和服务器上向控制台和文件输出不同级别的日志。
版本:spring-boot-starter-parent:2.2.6.RELEASE
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置:这里分为application-dev.yml
和application-test.yml
两个配置文件
意为 本机的环境,和在云服务器上的测试环境 的配置文件,不懂的话就只需要一个application.yml
就行。
其中application-dev.yml
如下配置:
server:
port: 8080
logging:
file:
path: E:/temp/xxx # 日志输出目录
name: E:/temp/xxx/info.log # 日志输出文件
添加logback-spring.xml
日志配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<!-- <include resource="org/springframework/boot/logging/logback/base.xml"/> -->
<conversionRule conversionWord="clr"
converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex"
converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
<conversionRule conversionWord="wEx"
converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} [TxId : %X{PtxId} , SpanId : %X{PspanId}] - %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} %-40.40logger{39} :[TxId : %X{PtxId} , SpanId : %X{PspanId}] - %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--默认的日志输出路径,会被配置文件中的覆盖-->
<property name="LOG_PATH" value="${LOG_PATH:-E:/temp/log}"/>
<property name="LOG_INFO_FILE" value="${LOG_FILE:-${LOG_PATH}/info.log}"/>
<property name="LOG_ERROR_FILE" value="${LOG_PATH}/error.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="INFO-FILE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_INFO_FILE}</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/info_%d{yyyy_MM_dd}.log</fileNamePattern>
<!--<maxHistory>180</maxHistory>-->
<!--<cleanHistoryOnStart>true</cleanHistoryOnStart>-->
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="ERROR-FILE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_ERROR_FILE}</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error_%d{yyyy_MM_dd}.log</fileNamePattern>
<!--<maxHistory>180</maxHistory>-->
<!--<cleanHistoryOnStart>true</cleanHistoryOnStart>-->
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<logger name="com.zaxxer" level="warn"/>
<logger name="org.mybatis" level="warn"/>
<logger name="javax.activation" level="WARN"/>
<logger name="javax.mail" level="WARN"/>
<logger name="javax.xml.bind" level="WARN"/>
<logger name="ch.qos.logback" level="WARN"/>
<logger name="com.codahale.metrics" level="WARN"/>
<logger name="com.netflix" level="WARN"/>
<logger name="com.netflix.discovery" level="INFO"/>
<logger name="com.ryantenney" level="WARN"/>
<logger name="com.sun" level="WARN"/>
<logger name="com.zaxxer" level="WARN"/>
<logger name="io.undertow" level="WARN"/>
<logger name="net.sf.ehcache" level="WARN"/>
<logger name="org.apache" level="WARN"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="OFF"/>
<logger name="org.bson" level="WARN"/>
<logger name="org.hibernate.validator" level="WARN"/>
<logger name="org.hibernate" level="WARN"/>
<logger name="org.hibernate.ejb.HibernatePersistence" level="OFF"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springframework.security" level="INFO"/>
<logger name="org.springframework.cache" level="WARN"/>
<logger name="org.thymeleaf" level="WARN"/>
<logger name="org.xnio" level="WARN"/>
<logger name="springfox" level="WARN"/>
<logger name="sun.rmi" level="WARN"/>
<logger name="sun.rmi.transport" level="WARN"/>
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="INFO-FILE-APPENDER"/>
<appender-ref ref="ERROR-FILE-APPENDER"/>
</root>
<!--开发环境 -->
<springProfile name="dev">
<!--这里控制日志级别,TRACE < DEBUG < INFO < WARN < ERROR < FATAL 若日志级别配置为 INFO 则 DEBUG和TRACE级别的日志都看不到-->
<logger name="root" level="DEBUG"/>
</springProfile>
<springProfile name="test">
<logger name="root" level="DEBUG"/><!--记录DEBUG级别的日志,同时输出到控制台和文件里-->
</springProfile>
</configuration>
做了以上配置后,即可在代码中输出日志:
定义一个HelloController.java
:
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@GetMapping("hello")
public String hello() {
logger.info("hello");
return "hello";
}
@GetMapping("errorhx")
public String errorhx() {
logger.error("errorhx");
return "errorhx";
}
}
然后我们在application.yml
中默认启用dev配置文件:
spring:
# 指定默认生效的配置文件
profiles:
active: dev
配置文件目录:
启动项目,访问 http://localhost:8080/hello , 观察控制台:
观察 E:\temp\xxx 目录:
查看info.log:
访问:http://localhost:8080/errorhx , 观察控制台:
查看 E:\temp\xxx 目录的error.log :
日志已经被正确输出到控制台和文件。
如果发布到云服务器上后,只需启动时,在java -jar
命令中添加 --spring.profiles.active=test
参数就会使用application-test.yml
配置文件:
server:
port: 8080
# 覆盖配置文件中的(不配置的话,配置文件有默认的)
logging:
file:
path: /home/log # 输出到云服务器上的目录
name: /home/log/info.log