1.过滤掉自带的spring-boot-starter-logging:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2.添加spring-boot-starter-log4j依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
3.获得logger实例:
Logger log = Logger.getLogger(this.getClass());
4.添加log4j配置文件:
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- Note that this file is refreshed by the server every 60seconds, as specified in web.xml -->
<log4j:configuration debug="true">
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<!-- The active file to log to -->
<param name="file" value="/applogs/myportal/portal.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<!-- The file to roll to, this is a fairly intelligent parameter, if the file
ends in .gz, it gzips it, based on the date stamp it rolls at that time,
default is yyyy-MM-dd, (rolls at midnight)
See: http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html -->
<param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<!-- The log message pattern -->
<param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
</layout>
</appender>
<!-- Loggers to filter out various class paths -->
<logger name="org.hibernate.engine.loading.LoadContexts" additivity="false">
<level value="error"/>
<appender-ref ref="ROLL" />
</logger>
<!-- Debugging loggers -->
<!-- Uncomment to enable debug on calpoly code only -->
<!--
<logger name="edu.calpoly">
<level value="debug"/>
<appender-ref ref="ROLL" />
</logger>
-->
<root>
<priority value="info" />
<appender-ref ref="ROLL" />
</root>
</log4j:configuration>