springboot 默认会加载classpath:logback-spring.xml文件。
springProfile 中的name名字对应application-xx.properties 中的xx.
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 文件输出格式 --> <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" /> <!-- test文件路径 --> <property name="TEST_FILE_PATH" value="F:/logs/" /> <!-- produce文件路径 --> <property name="PRO_FILE_PATH" value="/opt/roncoo/logs" /> <!-- 开发环境 --> <springProfile name="dev"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <Prudent>true</Prudent> <encoder> <pattern>${PATTERN}</pattern> </encoder> </appender> <logger name="com.roncoo.education" level="debug"/> <root level="info"> <appender-ref ref="CONSOLE" /> </root> </springProfile> <!-- 测试环境 --> <springProfile name="test"> <!-- 每天产生一个文件 --> <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Prudent>true</Prudent> <!-- 文件路径 --> <file>${TEST_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件名称 --> <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="info"> <appender-ref ref="TEST-FILE" /> </root> </springProfile> <!-- 生产环境 --> <springProfile name="prod"> <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Prudent>true</Prudent> <file>${PRO_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="warn"> <appender-ref ref="PROD_FILE" /> </root> </springProfile> </configuration>
demo地址:https://github.com/qjm201000/microservice_log.git