log4j simple configuration explanation

log4j configuration explanation 1:

 ###Output log information to console###
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{yyyy-MM-dd HH:mm:ss}%m%n

###Output log information to file: test.log###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=test.log
log4j.appender.file.layout=org. apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%m%n

###Set priority and output source###
log4j.rootLogger=debug,stdout,file

 

Configuration explanation:
log4j allows log requests to be output to multiple output sources, one output source is called an appender. There are two appenders above, the first one is named stdout, which uses ConsoleAppender.
By configuring the Target property, the log information is written to the console, and the effect is equivalent to directly printing the information to System.out. The second appender is named file and uses
FileAppender to write log information to the specified file (test.log) by configuring the File property.
layout specifies the style of log information output. The above two outputs both use PatternLayout, which can flexibly specify the layout mode. But you need to configure the layout.ConversionPattern property,
where: %d{yyyy-MM-dd HH:mm:ss} is used to set the time to output the log, and the output format is similar to 2009-05-13 09:00:00; %m uses to output the message specified in the code; %n is used to output a carriage return;
%l is used to output the location of the log event, including the class name, the thread where it occurred, and the number of lines in the code. For example: if the output is java.ch04.TestLog4j.min(TestLog4j.java:12), it
means that the log event occurs in the main thread of the TestLog4j class, and the number of lines in the code is line 12.
The log information has an output priority. The priority from high to low is FATA, error, warn, info, and debug. The last sentence above sets the priority to debug, and sets the log to be output to the console and the
test.log file respectively.

 

-------------------------------------------------------------------------------------------------------------------------------------------------

 

log4j configuration explanation 2:

#log4j.rootLogger=DEBUG,A1,R
### Set priority (fata, error, warn, debug, info), and output source (console, file) ###
log4j.rootLogger=INFO,A1,R
# ## Output log information to console ###
### A1, R output source name ###
### ConsoleAppender: write log information to console ###
log4j.appender.A1=org.apache.log4j .ConsoleAppender
### Output style layout pattern ###
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
### %c:packagename.filename. %p: output level, %m: output the message specified in the code, %n: output a carriage return and line feed, %l: output the location where the log information occurs, including the class name, the thread that occurred, the number of lines, %t output a tab ###
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

### RollingFileAppender: Write the log information to the file cyclically, FileAppender: Write the log information to the file ###
log4j.appender.R=org.apache.log4j.RollingFileAppender
### Set the log information file name to bbscs7log.txt ###
log4j.appender.R.File=bbscs7log.txt
### Set the log information file size to 500KB ###
log4j.appender.R.MaxFileSize=500KB
### Set the maximum number of log information files to 10## #
log4j.appender.R.MaxBackupIndex=10
### Output style layout pattern ###
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d%d% d%d%d%d%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n

#log4j.logger.org.springframework.transaction.interceptor=DEBUG

 

-------------------------------------------------------------------------------------------------------------------------------------------------

log4j without annotations:

File name: log4j.properties

 

#log4j.rootLogger=DEBUG,A1,R
log4j.rootLogger=INFO,A1,R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=bbscs7log.txt
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n

#log4j.logger.org.springframework.transaction.interceptor=DEBUG

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345395&siteId=291194637