1、导入pom依赖引入log4j
<!--log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2、log4j.properties
创建日志的配置文件 ,log4j.properties
是log4j默认的配置文件名
(1)将日志信息直接在控制台输出
log4j.rootLogger=DEBUG, stdout,ERROR, WARN, INFO, DEBUG, ALL
### 输出信息到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c:%L : %m%n
(2)自定义日志输出到文件,按时间划分日志,每天产生一个新文件
#输出ERROR 级别以上的日志到error.log
log4j.logger.ERROR=ERROR
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File =logs/error.log
log4j.appender.ERROR.DatePattern ='.'yyyy-MM-dd
#ERROR以上级别是输出
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出WARN 级别以上的日志到warn.log
log4j.logger.WARN=WARN
log4j.appender.WARN = org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN.File =logs/warn.log
log4j.appender.WARN.DatePattern ='.'yyyy-MM-dd
log4j.appender.WARN.Threshold = WARN
log4j.appender.WARN.Append = true
log4j.appender.WARN.layout = org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出INFO级别以上的内容到info.log中
log4j.logger.INFO=INFO
log4j.appender.INFO = org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFO.File = logs/info.log
log4j.appender.INFO.DatePattern ='.'yyyy-MM-dd
log4j.appender.INFO.Threshold = INFO
log4j.appender.INFO.Append = true
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出DEBUG 级别以上的日志到debugger.log
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.File = logs/debugger.log
log4j.appender.DEBUG.DatePattern ='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG
log4j.appender.DEBUG.Append = true
log4j.appender.DEBUG.layout = org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出ALL级别的日志到all.log
log4j.logger.ALL=ALL
log4j.appender.ALL = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ALL.File = logs/all.log
log4j.appender.ALL.DatePattern ='.'yyyy-MM-dd
log4j.appender.ALL.Threshold = ALL
log4j.appender.ALL.Append = true
log4j.appender.ALL.layout = org.apache.log4j.PatternLayout
log4j.appender.ALL.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
log4j.properties
文件类容
log4j.appender.encoding = UTF-8
# 大于1M后自定生成新文件
log4j.rootLogger=DEBUG, stdout,ERROR, WARN, INFO, DEBUG, ALL
### 输出信息到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c:%L : %m%n
#输出ERROR 级别以上的日志到error.log
log4j.logger.ERROR=ERROR
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File =logs/error.log
log4j.appender.ERROR.DatePattern ='.'yyyy-MM-dd
#ERROR以上级别是输出
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出WARN 级别以上的日志到warn.log
log4j.logger.WARN=WARN
log4j.appender.WARN = org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN.File =logs/warn.log
log4j.appender.WARN.DatePattern ='.'yyyy-MM-dd
log4j.appender.WARN.Threshold = WARN
log4j.appender.WARN.Append = true
log4j.appender.WARN.layout = org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出INFO级别以上的内容到info.log中
log4j.logger.INFO=INFO
log4j.appender.INFO = org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFO.File = logs/info.log
log4j.appender.INFO.DatePattern ='.'yyyy-MM-dd
log4j.appender.INFO.Threshold = INFO
log4j.appender.INFO.Append = true
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出DEBUG 级别以上的日志到debugger.log
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.File = logs/debugger.log
log4j.appender.DEBUG.DatePattern ='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG
log4j.appender.DEBUG.Append = true
log4j.appender.DEBUG.layout = org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
#-----------------------------------------------------------------------------------------------------
#输出ALL级别的日志到all.log
log4j.logger.ALL=ALL
log4j.appender.ALL = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ALL.File = logs/all.log
log4j.appender.ALL.DatePattern ='.'yyyy-MM-dd
log4j.appender.ALL.Threshold = ALL
log4j.appender.ALL.Append = true
log4j.appender.ALL.layout = org.apache.log4j.PatternLayout
log4j.appender.ALL.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID} --- [%15.15t] %c{10}:%L : %m%n
3、如何使用log4j
(1)java文件中
然后通过log.info("输出内容")
调用
(2)使用@log4j注解调用
添加@log4j注解,然后通过log.info("输出内容")
调用