原则:
代码中尽量使用slf4j门面
尽量采用注解方式@Slf4j节省代码量
步骤:
1.排除springboot默认日志(logback)
<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.引入slf4j和log4j2的jar包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3.添加log4j2的配置文件log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!--http://blog.csdn.net/u013066244/article/details/72461105-->
<!--https://github.com/alibaba/druid/wiki/Druid中使用log4j2进行日志输出-->
<Properties>
<!-- 配置日志文件输出目录 -->
<property name="LOG_HOME">/cwdata/logs</property>
<property name="SERVER_NAME">first</property>
</Properties>
<Appenders>
<!--这个输出控制台的配置-->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level [%class{36}.%M: %L] - %msg%n" />
</Console>
<!-- 按天第天备份一个日志 -->
<RollingFile name="RollingFile" fileName="${LOG_HOME}/xxx-web.log"
filePattern="${LOG_HOME}/xxx-web%d{yyyy-MM-dd}_%i.log">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level [%class{36}.%M: %L] - %msg%n" />
<Policies>
<!-- 每24小时更新一次 -->
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
<SizeBasedTriggeringPolicy size="300 MB"/>
</Policies>
<!-- 最多备份30个 -->
<DefaultRolloverStrategy fileIndex="max" max="60" />
</RollingFile>
<!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
<!--
<File name="Log_test" fileName="${LOG_HOME}/test.log" append="false">
<PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%class{36}.%M: %L] - %msg%n"/>
</File>
-->
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
<!--<AppenderRef ref="AsyncMail"/>-->
</Root>
<!--<logger name="com.cloudwave.skm" level="error" additivity="false">-->
<!--<AppenderRef ref="AsyncMail"/>-->
<!--</logger>-->
<logger name="org.springframework" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="org.mybatis" level="debug" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="org.mybatis.spring" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="java.sql.Connection" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="java.sql.Statement" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="java.sql.PreparedStatement" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="java.sql.ResultSet" level="info" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="com.cloudwave.skm.db.mapper" level="TRACE" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<logger name="org.codehaus.jackson" level="error" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</logger>
<!--记录druid-sql的记录-->
<logger name="druid.sql.Statement" level="debug" additivity="false">
<AppenderRef ref="Console" />
<appender-ref ref="RollingFile"/>
</logger>
<logger name="druid.sql.Statement" level="debug" additivity="false">
<AppenderRef ref="Console" />
<appender-ref ref="RollingFile"/>
</logger>
<!--log4j2 自带过滤日志-->
<Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
<Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
<Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
<logger name="org.apache.sshd.common.util.SecurityUtils" level="warn"/>
<Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
<Logger name="org.crsh.plugin" level="warn" />
<logger name="org.crsh.ssh" level="warn"/>
<Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" />
<Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
<logger name="org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration" level="warn"/>
<logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn"/>
<logger name="org.thymeleaf" level="warn"/>
</Loggers>
</Configuration>
4.在application.yml中配置日志属性
# 日志配置
logging:
# 指定日志配置文件位置
config: classpath:log4j2-spring.xml
# 设置具体打印策略
level:
com.xx: info
org.springframework: WARN
org.spring.springboot.dao: debug
5.代码中使用
maven添加lombok插件依赖,在任意类中加上@Slf4j注解,然后使用log.info()打印日志