Tag:log4j随笔
Article From:https://www.cnblogs.com/Danial7777777/p/9136849.html

###
# We’ve already seen the way to configure the log4j environment in the code, whether it’s more troublesome, let’s try using a configuration file to make your application more flexible.
# Log4jIt supports two kinds of configuration file formats, one is XML format file, the other is Java feature file (key = value).
# Now let’s try to use the Java feature file as the way to configure files.
#
# The following settings parameters have been mentioned before, and they will not be repeated here.
#
###

###
# The configuration of the root logger is set as follows: log4j.rootLogger = [level], appenderName1, appenderName2,… AppenderNameN
# levelTo set the priority of log records, there is a system custom 8 medium type, and can be customizable, and the official proposal is only using these four kinds of ERROR> WARN> INFO> DEBUG
# appenderNameNThe output fields for setting up logs can be set more than one, and you can customize the names.
###
log4j.rootLogger = debug,stdout,D,E,F

###
# Set the output of the log record, whose syntax is:
#     log4j.appender.appenderName = fully.qualified.name.of.appender.class
#   log4j.appender.appenderName.option1 = value1
#   …
#  log4j.appender.appenderName.optionN = valueN
# fully.qualified.name.of.appender.class:There are many predefined definitions of the full path class name of the class output class. Here are four commonly used ones.
# optionN/valueN :They are the corresponding output destination attribute and the set attribute value respectively.
#
# If the corresponding attribute is still object level, the corresponding attribute can be set by continuing.
#
###

###
# The output information to the console can be configured as follows:
# 1)Threshold:Setting the level of log information output
# 2)ImmediateFlush:Whether log information is output immediately, defaults to TRUE, and output immediately.
# 3)Target:The target flow that sets the output of log information has two values System.out and System.err, and the default value is System.out (err is red, out is black).
# 4)layout:Setting the style of the log information output
# 5)Encoding:Set the encoding format of the file, which is consistent with the coding format of the system platform.
###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Encoding = Utf-8
log4j.appender.stdout.Threshold = debug
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n

###
# The commonly used optional configurations for exporting information to files are as follows:
# 1)Threshold:Setting the level of log information output
# 2)ImmediateFlush:Whether log information is output immediately, defaults to TRUE, and output immediately.
# 3)File:Set log information to output the full path, for example: E://logs/FileAppender.log
# 4)layout:Setting the style of the log information output
# 5)Encoding:Set the encoding format of the file, which is consistent with the coding format of the system platform.
# 6)Append:Set whether to append the log message to the specified file, the default value of true, adding the message to the specified file, and false means to overlay the message to the specified file content
###
log4j.appender.D = org.apache.log4j.FileAppender
log4j.appender.D.File = log/logfile.txt
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n

###
# The output information is to the file, but the file is controlled, and how long it can be configured to generate a new log information file, and its commonly used optional configurations are as follows:
# 1)Threshold:Setting the level of log information output
# 2)ImmediateFlush:Whether log information is output immediately, defaults to TRUE, and output immediately.
# 3)File:Set log information to output the full path, for example: E://logs/DailyRollingFileAppender.log
# 4)layout:Setting the style of the log information output
# 5)Encoding:Set the encoding format of the file, which is consistent with the coding format of the system platform.
# 6)Append:Set whether to append the log message to the specified file, the default value of true, adding the message to the specified file, and false means to overlay the message to the specified file content
# 7)DatePattern:Setting the time style of file rollback
#              ‘.’yyyy-MM monthly
#              ‘.’yyyy-ww weekly
#              ‘.’yyyy-MM-dd Every day
#              ‘.’yyyy-MM-dd-a Two times a day
#              ‘.’yyyy-MM-dd-HH Hourly
#              ‘.’yyyy-MM-dd-HH-mm per minute
###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =log/logdaily.txt
log4j.appender.E.Append = true
log4j.appender.E.Threshold = WARN
log4j.appender.E.DatePattern = ‘.’yyyy-MM-dd-HH-mm
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n

###
# The output information is to the file, but the file is controlled. When the size of the file reaches a certain threshold, the log file will automatically roll back, and the common optional configuration is as follows:
# 1)Threshold:Setting the level of log information output
# 2)ImmediateFlush:Whether log information is output immediately, defaults to TRUE, and output immediately.
# 3)File:Set log information to output the full path, for example: E://logs/RollingFileAppender.log
# 4)layout:Setting the style of the log information output
# 5)Encoding:Set the encoding format of the file, which is consistent with the coding format of the system platform.
# 6)Append:Set whether to append the log message to the specified file, the default value of true, adding the message to the specified file, and false means to overlay the message to the specified file content
# 7)MaxFileSize:Set the file automatically to roll back when the file reaches this threshold. The unit can be KB, MB, GB, and the default unit is KB.
# 8)MaxBackupIndex:Setting up the maximum number of backups for the rollback log
###
log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.File =log/logrol.txt
log4j.appender.F.Append = true
log4j.appender.F.Threshold = INFO
log4j.appender.F.MaxFileSize = 1
log4j.appender.F.MaxBackupIndex = 5
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern =[%d{yyyy-MM-dd HH\:mm\:ss\:SSS}] [%-5p] [method\:%l]%n%m%n%n

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *