需要把logback.xml文件放在类路径下,如果是spring boot项目可以用 logging.config=classpath:log/xxxxxx.xml来指定配置文件
logback layout可以参考: https://logback.qos.ch/manual/layouts.html
以下是配置文件示例:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="false" scanPeriod="120 seconds" debug="false"> <contextName>myLog</contextName> <property name="LOG_ROOT" value="C:\Users\cy\Desktop\ssm1\duo" /> <!-- 控制台STDOUT --> <appender name="STDOUT-APPENDER" class="ch.qos.logback.core.ConsoleAppender"> <param name="target" value="System.out"/> <encoder charset="UTF-8"> <pattern>%d [%t] %-5p %c - [%msg]%n</pattern> </encoder> </appender> <!-- 控制台STDERR --> <appender name="STDERR-APPENDER" class="ch.qos.logback.core.ConsoleAppender"> <param name="target" value="System.err"/> <encoder charset="UTF-8"> <pattern>%d [%t] %-5p %c - [%msg]%n</pattern> </encoder> </appender> <!--默认记录--> <appender name="DEFAULT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_ROOT}/log/default.log</file> <append>true</append> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_ROOT}/log/backup/default.%d{yyyyMMdd}.log</fileNamePattern> <maxHistory>3</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5p %c - [%msg]%n</pattern> </encoder> </appender> <!--分类文件记录,如果日志太多默认记录不满足需求,可以分类存储--> <appender name="ES-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_ROOT}/log/es.log</file> <append>true</append> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_ROOT}/log/backup/es.%d{yyyyMMdd}.log</fileNamePattern> <maxHistory>3</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5p %c - [%msg]%n</pattern> </encoder> </appender> <!--文件异常记录--> <appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_ROOT}/log/error.log</file> <append>true</append> <!--过滤掉ERROR级别以下的日志信息--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_ROOT}/log/backup/error.%d{yyyyMMdd}.log</fileNamePattern> <maxHistory>3</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5p %C - [%msg]%n</pattern> </encoder> </appender> <!--控制台标准输出--> <logger name="STDOUT" level="INFO" additivity="false"> <appender-ref ref="STDOUT-APPENDER"/> </logger> <!--控制台错误输出--> <logger name="STDERR" level="ERROR" additivity="false"> <appender-ref ref="STDERR-APPENDER"/> </logger> <!--分类INFO输出,到文件--> <logger name="ES" level="INFO" additivity="false"> <appender-ref ref="ES-APPENDER"/> </logger> <!--所有ERROR输出--> <logger name="ERROR" level="ERROR" additivity="false"> <appender-ref ref="ERROR-APPENDER"/> </logger> <root level="INFO"> <!--默认记录输出到DEFAULT-APPENDER--> <appender-ref ref="DEFAULT-APPENDER"/> <!--<appender-ref ref="STDERR-APPENDER"/>--> <!--<appender-ref ref="STDOUT-APPENDER"/>--> <!--<appender-ref ref="ERROR-APPENDER"/>--> </root> </configuration>