log4j configuration in detail and log4j2

1 log4j configuration in detail and log4j2

1.1 Description of the concept

1.1.1 log4j Brief

Log4jIs Apachea Javalogging library by using Log4j, we can control the destination (console, file, database, etc.) log information conveyed; we can also control the output format of each log; by defining the level of each log information, we It can be more finely controlled generation process log.

1.1.2 Log4j2 Brief

Apache Log4j 2It is Log4jescalation, than its predecessor it Log4j 1.xoffers significant improvements, and offers Logbackmany improvements available, and fixes Logbacksome of the problems architecture.
Now the best Javalogging framework Log4j2, not one. According to the official tests have shown that, in a multi-threaded environment, Log4j2 asynchronous log performance even more outstanding. In the asynchronous log in, Log4j2use a separate thread to perform the I/Ooperation, can greatly enhance the performance of your application

1.2 pom.xml file

First is the need to exclude springbootdefault inside the logconfiguration reference

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-dependencies</artifactId>
     <version>2.1.9.RELEASE</version>
     <!-- 排除默认日志配置 -->
     <exclusions>
         <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
          </exclusion>
      </exclusions>
</dependency>
<!-- 引入log4j2的jar包-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-log4j2</artifactId>
     <version>RELEASE</version>
</dependency>

1.3 log4j.properties of reference

application.propertiesFollows the profile

#配置日志
logging.config=classpath:log4j.properties

1.4 log4j.properties description

1.4.1 rootLogger property

log4j.propertiesFile Description

log4j.rootLogger = level,appenderName1,appenderName2, .....

levelIt is a log priority, divided OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALLor customize the level of the
leveldefault priority: ALL < DEBUG < INFO <WARN < ERROR < FATAL < OFF
appenderNamea log information output position, a plurality of output destinations can be specified at the same time

1.4.2 output destination appender property

AppenderThe configurations:

log4j.appender.appenderName = Log4j提供的appender类
log4j.appender.appenderName.属性名 = 属性值
.....
log4j.appender.appenderName.属性名 = 属性值

Log4jProvided appenderthe following 5species, respectively, may be the log information output to 5different platforms

  • org.apache.log4j.ConsoleAppender (console)
  • org.apache.log4j.FileAppender (file)
  • org.apache.log4j.DailyRollingFileAppender (produce a daily log file)
  • org.apache.log4j.RollingFileAppender (file size reaches the specified size when create a new file)
  • Org.apache.log4j.WriterAppender (log stream format information is sent to any designated place)

1.4.2.1 ConsoleAppender

Threshold=WARN: Specified minimum level of output log messages.
ImmediateFlush=true: The default value is true, means that all messages will be immediately output.
Target=System.err: The default is: System.out, specify the output console

1.4.2.2 FileAppender

Threshold=WARN: Specified minimum level of output log messages.
ImmediateFlush=true: The default value is true, means that all messages will be immediately output.
File=mylog.txt: News output to a mylog.txtfile.
Append=false: The default value is true, will be designated to file, falseit refers to overwrite the contents of the message file specified.

1.4.2.3 DailyRollingFileAppender

Threshold=WARN: Specified minimum level of output log messages.
ImmediateFlush=true: The default value is true, means that all messages will be immediately output.
File=mylog.txt: News output to a mylog.txtfile.
Append=false: The default value is true, will be designated to file, falseit refers to overwrite the contents of the message file specified.
DatePattern=”.”yyyy-ww: Rolling a weekly papers, which produce a new file every week. Of course, you can be specified 月、周、天、时和分. I.e., corresponding to the following format:

  • yyyy-MM ".": Monthly
  • yyyy-ww ".": weekly
  • "." Yyyy-MM-dd: day
  • yyyy-MM-dd-a ".": twice a day
  • yyyy-MM-dd-HH ".": per hour
  • yyyy-MM-dd-HH-mm ".": min

1.4.2.4 RollingFileAppender

Threshold=WARN: Specified minimum level of output log messages.
ImmediateFlush=true: The default value is true, means that all messages will be immediately output.
File=mylog.txt: Mylog.txt news output to a file.
Append=false: The default value is true, the upcoming news 增加to the specified file, false means that the message 覆盖contents of the file specified.
MaxFileSize=100KB: Suffix can be KB, MBeither GBwhen the log file reaches this size, it will automatically scroll about to move to the original content. mylog.log.1Files.
MaxBackupIndex=2: Specifies the maximum number of files that can be generated scroll.

1.4.3 format layout log information

Log format configuration:

log4j.appender.appenderName.layout = Log4j提供的layout类
log4j.appender.appenderName.layout.属性 = 值
.....
log4j.appender.appenderName.layout.属性 = 值

Log4jlayout offers the following:

  • org.apache.log4j.HTMLLayout (in HTML table format layout),
  • org.apache.log4j.PatternLayout (the flexibility to specify the layout mode),
  • Org.apache.log4j.SimpleLayout (containing level information and log information string),
  • org.apache.log4j.TTCCLayout (including the time the log generated, thread, type information, etc.)

1.4.3.1 HTMLLayout

LocationInfo=true: The default value is false, java output file name and line number
Title=my app file: The default value is Log4J Log Messages

1.4.3.2 PatternLayout

ConversionPattern=%m%n: Specify how to format the specified message.

1.4.3.3 Layout log format in various types of symbols details

Log4JSimilar Clanguage printffunction log information formatted for printing, the printing parameters are as follows:

og4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n

Log information format meaning several symbols represent:

  • -X号: Left X when the information output;
  • %p: Priority output log information, i.e. DEBUG, INFO, WARN, ERROR, FATAL,
  • %d:Date or time output log point in time, the default format is ISO8601, you can specify the format thereafter, for example: %d{yyy MMM dd HH:mm:ss,SSS}the output is like: In 2002 at 22:10 on October 18: 28,921
  • %r: Output to the output since the start of the log information on the number of milliseconds spent
  • %c: Category output log information belongs, usually the full name of the class where
  • %t: The output generated log event thread name
  • %l:The occurrence location of the output log events, the equivalent of %C.%M(%F:%L)the portfolio, including categories were the threads, as well as the number of rows. For example:Testlog4.main(TestLog4.java:10)
  • %x:Output current thread and associated NDC, in particular, is used (nested diagnostic context) as java servletsa multi-client multi-threaded applications in the case.
  • %%:Output a ”%”character
  • %F: When the output file name where the log messages generated
  • %L: The output of the code line number
  • %m: DETAILED output log information specified in the code message, generated
  • %n:Output a carriage return line feed, Windowsplatform ”\r\n”, Unixplatform for ”\n”output log information newline

You may %add modifiers to control its mode between the character 最小宽度、最大宽度、和文本的对齐方式. Such as:

  • %20c:Specify the output categoryname, the minimum width 20, if categorythe name is less than 20, the right-aligned by default.
  • %-20c:Specify the output categoryname, the minimum width is 20, if the category name is less than 20, ”-“designated left-aligned.
  • %.30c:Specify the output categorynames, the maximum width is 30, if categorythe name is greater than 30, then, it will be left out of the multi-character amputated, but the words would not be less than 30 spaces.
  • %20.30c:If the categoryname of the meeting spaces is less than 20, and right-aligned, if the name is longer than 30 characters, it is exported from the left to pay-out character truncated

1.5 log4j.properties examples

# 设置日志根
log4j.rootLogger = debug , stdout , D , E
# 此处的addivity指是否追加写入日志
log4j.addivity.org.apache=true
# 输出到控制台
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
# 输出到日志文件
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.D.File = logs/log.log
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
# 保存异常信息到单独文件
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.D.File = logs/error.log ## 异常日志文件名
#log4j.appender.D.Append = true
#log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
#log4j.appender.D.layout = org.apache.log4j.PatternLayout
#log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/104150519