Log4j 入门
Log4j 入门
中柠檬 发表于1年前
Log4j 入门
  • 发表于 1年前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

原文地址

简介

Log4j 是一个使用java编写的日志框架。

Log4j 主要由三部分组成:

  • loggers:负责采集日志信息
  • appenders:负责将日志发送到不同的地方
  • layouts : 负责以各种风格格式化日志信息

配置

log4j 的配置文件是 Log4j.properties。模板:

# Define the root logger with appender file
Log4j.rootLogger = DEBUG, FILE

# Define the file appender
Log4j.appender.FILE=org.apache.Log4j.FileAppender
Log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
Log4j.appender.FILE.layout=org.apache.Log4j.PatternLayout
Log4j.appender.FILE.layout.conversionPattern=%m%n

一、需要设置的属性:

  • Log4j.rootLogger:日志的级别

  • Log4j.appender.X:日志的输出目的地

  • Log4j.appender.X.layout:日志的布局

  • 其中: %m%n 表示 打印出来的日志信息末尾加入换行。

二、 appender

ConsoleAppender、 FileAppender、 JDBCAppende等

三、layout

PatternLayout、HTMLLayout、SimpleLayout、XMLLayout

四、 使用文件记录日志

log4j.properties

# Define the root logger with appender file
Log4j.rootLogger = DEBUG, FILE

# Define the file appender
Log4j.appender.FILE=org.apache.Log4j.FileAppender

# Set the name of the file
Log4j.appender.FILE.File=${log}/log.out

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

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

# Set the append to false, overwrite
Log4j.appender.FILE.Append=false

# Define the layout for file appender
Log4j.appender.FILE.layout=org.apache.Log4j.PatternLayout
Log4j.appender.FILE.layout.conversionPattern=%m%n

或者 log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Log4j:configuration SYSTEM "Log4j.dtd">
<Log4j:configuration>

<appender name="FILE" class="org.apache.Log4j.FileAppender">

   <param name="file" value="${log}/log.out"/>
   <param name="immediateFlush" value="true"/>
   <param name="threshold" value="debug"/>
   <param name="append" value="false"/>

   <layout class="org.apache.Log4j.PatternLayout">
      <param name="conversionPattern" value="%m%n"/>
   </layout>
</appender>

<logger name="Log4j.rootLogger" additivity="false">
   <level value="DEBUG"/>
   <appender-ref ref="FILE"/>
</logger>

</Log4j:configuration>

逐日生成日志文件

log4j.properties

# Define the root logger with appender file
Log4j.rootLogger = DEBUG, FILE

# Define the file appender
Log4j.appender.FILE=org.apache.Log4j.DailyRollingFileAppender

# Set the name of the file
Log4j.appender.FILE.File=${log}/log.out

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

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

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

# Set the DatePattern
Log4j.appender.FILE.DatePattern='.' yyyy-MM-dd-a

# Define the layout for file appender
Log4j.appender.FILE.layout=org.apache.Log4j.PatternLayout
Log4j.appender.FILE.layout.conversionPattern=%m%n

五、配置多个输出路径
log4j.perperties

log4j.rootLogger=DEBUG, stdout,logFile

# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

# 日志文件(logFile)
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.Threshold=Error
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=TRUE
log4j.appender.logFile.File=E:/logs/log.out
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
  • 点赞
  • 收藏
  • 分享
粉丝 7
博文 177
码字总数 83542