<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="3 seconds"> <!-- 定义变量 --> <if condition='p("os.name").contains("Windows")'> <then> <property name="LOG_PATH" value="c:/logs" /> </then> </if> <if condition='p("os.name").contains("Linux")'> <then> <property name="LOG_PATH" value="logs" /> </then> </if> <property name="APP_NAME" value="study" /> <property name="LOG_ROOT_LEVEL" value="debug" /> <property name="maxHistory" value="30" /> <contextName>${APP_NAME}</contextName> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> <!-- 设置日志输出格式 --> [%-5level] %d{yyyy-MM-dd HH:mm:ss} [%thread] %logger{36} - %m%n </pattern> </encoder> </appender> <!-- 按照每天生成日志文件 --> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/${APP_NAME}/ERROR.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] [%thread] %logger{36} - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_PATH}/${APP_NAME}/ERROR.%d.log </FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/${APP_NAME}/WARN.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] [%thread] %logger{36} - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_PATH}/${APP_NAME}/WARN.%d.log </FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/${APP_NAME}/INFO.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] [%thread] %logger{36} - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_PATH}/${APP_NAME}/INFO.%d.log </FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_PATH}/${APP_NAME}/DEBUG.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%level] [%thread] %logger{36} - %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${LOG_PATH}/${APP_NAME}/DEBUG.%d.log </FileNamePattern> <MaxHistory>${maxHistory}</MaxHistory> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <root level="${LOG_ROOT_LEVEL}"> <!-- 控制台输出 --> <appender-ref ref="STDOUT" /> <!-- 文件输出 --> <appender-ref ref="FILE_ERROR" /> <appender-ref ref="FILE_WARN" /> <appender-ref ref="FILE_INFO" /> <appender-ref ref="FILE_DEBUG" /> </root> </configuration>
中文文档链接:https://yunpan.360.cn/surl_ydCHABuegfQ (提取码:0c21)
注意事项:
1。使用if必须添加依赖
<dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.0.9</version> </dependency>