Detailed use of log4j.properties

1. Detailed explanation of the use of log4j.properties

 

1. Types of output levels

ERROR, WARN, INFO, DEBUG
ERROR is a serious error, mainly a program error
WARN is a general warning, such as session loss
INFO is a general information to be displayed, such as login and logout
DEBUG is the debugging information of the program


2. Configure the log information output destination

log4j.appender.appenderName = fully.qualified.name.of.appender.class
1.org.apache.log4j.ConsoleAppender (console)
2.org.apache.log4j.FileAppender (file) 3.org.apache.log4j
. DailyRollingFileAppender (generates a log file every day)
4.org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size)
5.org.apache.log4j.WriterAppender (streams log information to any designated place)


3. Configure the format of the log information

log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
1.org.apache.log4j.HTMLLayout (layout in HTML form),
2.org.apache.log4j.PatternLayout (can be flexibly specified Layout mode) , 3.org.apache.log4j.SimpleLayout (contains
the level and information string of log information),
4.org.apache.log4j.TTCCLayout (contains log generation time, thread, category, etc. information)


4. Console Options

Threshold=DEBUG: Specifies the lowest level of output of log messages.
ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
Target=System.err: By default: System.out, specifies the output console
FileAppender option
Threshold=DEBUF: specifies the lowest level of output for log messages.
ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
File=mylog.txt: Specifies that messages are output to the mylog.txt file.
Append=false: The default value is true, which means adding the message to the specified file, and false means overwriting the content of the specified file with the message.
RollingFileAppender option
Threshold=DEBUG: Specifies the minimum level of output of log messages.
ImmediateFlush=true: The default value is true, which means that all messages will be output immediately.
File=mylog.txt: Specifies that messages are output to the mylog.txt file.
Append=false: The default value is true, which means adding the message to the specified file, and false means overwriting the content of the specified file with the message.
MaxFileSize=100KB: The suffix can be KB, MB or GB. When the log file reaches this size, it will be automatically rolled, that is, the original content will be moved to the mylog.log.1 file.
MaxBackupIndex=2: Specifies the maximum number of rolling files that can be generated.
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n


5. The meanings represented by several symbols in the log information format:

 -X number: left-aligned when X information is output;
 %p: output log information priority, namely DEBUG, INFO, WARN, ERROR, FATAL,
 %d: date or time of output log time point, the default format is ISO8601, or Specify the format after it, for example: %d{yyy MMM dd HH:mm:ss,SSS}, the output is similar to: 2002-10-18 22:10:28, 921
 %r: The output is from the application startup to the output of the log The number of milliseconds spent by the information
 %c: The category to which the log information belongs, usually the full name of the class in which it belongs
 %t: The thread name that generates the log event is
 output %l: The location where the log event occurs, equivalent to %C.% The combination of M(%F:%L), including the category name, the thread in which it occurs, and the number of lines in the code. Example: Testlog4.main (TestLog4.java:10)
 %x: Output the NDC (Nested Diagnostic Environment) associated with the current thread, especially for multi-client multi-threaded applications like java servlets.
 %%: output a "%" character
 %F: output the name of the file where the log message is generated
 %L: output the line number in the code
 %m: output the message specified in the code, the specific log information generated
 %n: output a Carriage return line feed, "/r/n" for Windows platform, "/n" for Unix platform to output log information line feed

 

Modifiers can be added between the % and the pattern character to control the minimum width, maximum width, and alignment of the text.

Such as:

 1)   %20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,默认的情况下右对齐。
 2)   %-20c:指定输出category的名称,最小的宽度是20,如果category的名称小于20的话,"-"号指定左对齐。
 3)   %.30c:指定输出category的名称,最大的宽度是30,如果category的名称大于30的话,就会将左边多出的字符截掉,但小于30的话也不会有空格。

 4)   %20.30c:如果category的名称小于20就补空格,并且右对齐,如果其名称长于30字符,就从左边较远输出的字符截掉。

 


 

二、log4j.properties的配置

 

1、配置步骤

 


  1)  在应用程序中使用log4j
  2)     把log4j-*.*jar放入CLASSPATH变量中
  3)     新建一个配置文件log4j.properties,放于bin文件下
    

 


 

2.新建一个配置文件log4j.properties,如:

 


  log4j.rootLogger=WARN, stdout, R
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  # Pattern to output the caller's file name and line number.
  #log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  # Print the date in ISO 8601 format
  log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=example.log
  log4j.appender.R.MaxFileSize=100KB
  # Keep one backup file
  log4j.appender.R.MaxBackupIndex=1
  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
  # Print only messages of level WARN or above in the package com.foo.
  log4j.logger.com.foo=WARN

  编译并运行TestLog4j会在目录下生成一个example.log的文件,屏幕也会输出信息,这证明已经你已经成功了第一步。

转:http://blog.csdn.net/edward0830ly/article/details/8250412

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326552263&siteId=291194637