spring-boot-starter-log4j集成
一、引入依赖
SpringBoot默认使用slf4j面板日志,所以只需要导入依赖包,它自动会找到实现类。
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency>
|
二、配置
在resource下的application.properties中加入配置
1 2
| logging.config=classpath:log4j.xml
|
log4j.xml文件,下面的配置是每10M分一个新文件。info类和error类日志分开
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <param name="Threshold" value="DEBUG" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" /> </layout> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="new a seq no spend" /> <param name="AcceptOnMatch" value="false" /> </filter> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="new hi[type=" /> <param name="AcceptOnMatch" value="false" /> </filter> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="FileTransfer.execCommand()" /> </filter> </appender>
<appender name="InfoFileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="Encoding" value="UTF-8" /> <param name="Append" value="true" /> <param name="Threshold" value="INFO" /> <param name="File" value="logs/info.log" /> <param name="MaxFileSize" value="10MB" /> <param name="MaxBackupIndex" value="15" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" /> </layout> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="new a seq no spend" /> <param name="AcceptOnMatch" value="false" /> </filter> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="new hi[type=" /> <param name="AcceptOnMatch" value="false" /> </filter> <filter class="org.apache.log4j.varia.StringMatchFilter"> <param name="StringToMatch" value="FileTransfer.execCommand()" /> <param name="AcceptOnMatch" value="false" /> </filter> </appender>
<appender name="errorFileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="Encoding" value="UTF-8" /> <param name="Append" value="true" /> <param name="Threshold" value="ERROR" /> <param name="File" value="logs/error.log" /> <param name="MaxFileSize" value="10MB" /> <param name="MaxBackupIndex" value="4" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n %m%n" /> </layout> </appender> <root> <priority value="INFO" /> <appender-ref ref="CONSOLE" /> <appender-ref ref="InfoFileAppender" /> <appender-ref ref="errorFileAppender" /> </root> </log4j:configuration>
|