[问题记录]log4j使用记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yeweiouyang/article/details/53645458

步骤一:maven配置依赖

https://mvnrepository.com/artifact/log4j/log4j/1.2.16

步骤二:IDEA加入log4j.properties文件

File->Project Structure
idea_setting_1

log4j.properties文件放入resources文件夹中
idea_setting_2

步骤三:配置log4j.properties文件

  • 输出到控制台
  • 输出到../logs/task_node.log文件(每天生成一个新文件,旧文件自动被重命名为task_node.log_xxxx-xx-xx

log4j.rootLogger=DEBUG,console,FILE

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n

#log4j.appender.FILE=org.apache.log4j.RollingFileAppender
#log4j.appender.FILE.Append=true
#log4j.appender.FILE.File=../logs/TaskNode.log
#log4j.appender.FILE.Threshold=INFO
#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
#log4j.appender.FILE.MaxFileSize=10MB

# Direct log messages to a log file
# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

# Set the name of the file
log4j.appender.FILE.File=../logs/task_node.log

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=INFO

# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true

# Set the DatePattern
log4j.appender.FILE.DatePattern='_'yyyy-MM-dd

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n

Usage

private static final Logger logger = Logger.getLogger(GitUtil.class);

logger.info("xxx");
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页