SpringBoot集成log4j

Posted 只有云知道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot集成log4j相关的知识,希望对你有一定的参考价值。

1、pom.xml中增加依赖

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
    <version>1.3.8.RELEASE</version>
</dependency>

说明:去除对默认日志的依赖,是为了去除和logback的冲突。

2、在resources目录下添加log4j.properties文件

log4j.rootLogger=info,R,R2,R3
log4j.logger.monitor = info,monitor
log4j.logger.time-consuming = info,time-consuming
##log4j.rootLogger=info,errorservice,CONSOLE,DEBUG
#
##print to console
#-----------------------   CONSOLE   -------------------------------------------------
log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.Target=System.out
log4j.appender.R.ImmediateFlush=true
log4j.appender.R.Threshold=info
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}:  %m%n


#--------------------------------   INFO   -------------------------------------------
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.DatePattern=‘.‘yyyy-MM-dd-HH
log4j.appender.R2.File=/data/mmmeditor/info.log
#log4j.appender.R2.File=D:/workspace/info.log
log4j.appender.R2.Threshold=info
log4j.appender.R2.ImmediateFlush=true
log4j.appender.R2.Append=true
log4j.appender.R2.layout=org.apache.log4j.PatternLayout
log4j.appender.R2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}:  %m%n
log4j.additivity.R2=false


#write to file on the level of error
#-----------------------   error  ---------------------------------
log4j.appender.R3=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R3.DatePattern=‘.‘yyyy-MM-dd-HH
log4j.appender.R3.File=/data/mmmeditor/error.log
#log4j.appender.R3.File=D:/workspace/error.log
log4j.appender.R3.Threshold=error
log4j.appender.R3.ImmediateFlush=true
log4j.appender.R3.Append=true
log4j.appender.R3.layout=org.apache.log4j.PatternLayout
log4j.appender.R3.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}:  %m%n
log4j.additivity.R3=false


#------------------ 修改 /data/mmmeditor/time-consuming.log ----------------------
log4j.appender.monitor=org.apache.log4j.DailyRollingFileAppender
log4j.appender.monitor.DatePattern=‘.‘yyyy-MM-dd
log4j.appender.monitor.layout.ConversionPattern={"level":"%p","timestamp":"%d{yyyy-MM-dd HH:mm:ss,SSS}","thread":"%t","file":"%F","number":"%L","class":"%C","method":"%M","message":%m}%n
log4j.appender.monitor.File=/data/mmmeditor/monitor.log
#log4j.appender.monitor.File=D:/mylog/monitor.log
log4j.appender.monitor.MaxFileSize=10000KB
log4j.appender.monitor.MaxBackupIndex=1
log4j.appender.monitor.layout=org.apache.log4j.PatternLayout
log4j.additivity.monitor=false


#------------------ 修改 /data/mmmeditor/time-consuming.log -----------------------
log4j.appender.time-consuming=org.apache.log4j.DailyRollingFileAppender
log4j.appender.time-consuming.DatePattern=‘.‘yyyy-MM-dd
log4j.appender.time-consuming.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%t]: %m%n
log4j.appender.time-consuming.File=/data/mmmeditor/time-consuming.log
#log4j.appender.time-consuming.File=D:/mylog/time-consuming.log
log4j.appender.time-consuming.MaxFileSize=10000KB
log4j.appender.time-consuming.MaxBackupIndex=1
log4j.appender.time-consuming.layout=org.apache.log4j.PatternLayout
#log4j.additivity.time-consuming=false

注意:修改/data/mmmeditor/time-consuming.log

以上是关于SpringBoot集成log4j的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成log4j

springboot集成log4j + sql打印日志

Springboot: log4j集成,配合lombok

springboot项目集成logf4j详细流程记录

springBoot log4j集成的mybatis 控制台打印sql

SpringBoot-集成Log4j2