This article introduces the simple configuration and use of log4j.properties in detail. I will share it with you. The details are as follows:
Simple log4j.properties configuration example
### set log levels ### log4j.rootLogger = INFO , console , debug , error ### console ### log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH/:mm/:ss} [%p]-[%c] %m%n ### log file ### log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender log4j.appender.debug.File = ../logs/springmvc-demo.log log4j.appender.debug.Append = true log4j.appender.debug.Threshold = INFO log4j.appender.debug.layout = org.apache.log4j.PatternLayout log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH/:mm/:ss} [%p]-[%c] %m%n ### exception ### log4j.appender.error = org.apache.log4j.DailyRollingFileAppender log4j.appender.error.File = ../logs/springmvc-demo_error.loglog4j.appender.error.Append = true log4j.appender.error.Threshold = ERROR log4j.appender.error.layout = org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH/:mm/:ss} [%p]-[%c] %m%n #### You need to declare it, and then you can make druid sql output below, otherwise log4j.error.key not found 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{ISO8601} %l %c%n%p: %m%n ### druid sql ### log4j.logger.druid.sql=warn,stdout log4j.logger.druid.sql.DataSource=warn,stdout log4j.logger.druid.sql.Connection=warn,stdout log4j.logger.druid.sql.Statement=warn,stdout log4j.logger.druid.sql.ResultSet=warn,stdout JAVA Code Part
public Test{ Logger log = Logger.getLogger(Test.class)//log.info() call}Requires log4j JAR package
Log4j supports configuration files in two formats: xml and properties; depend on commons-logging package
1. Configure the root logger
log4j.rootLogger=[level],appenderName,appenderName,...
Levels can be divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, and ALL. If OFF is configured, no information will be displayed. The priority is: error>warn>info>debug, which is case-sensitive.
Subclasses can be added, for example:
log4j.category.org.nutz=INFO, console, nutz
log4j.category.com.gtz=DEBUG, console, gtz
2. Configure the log information output location Appender
log4j.appender.appenderName = fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = value1…log4j.appender.appenderName.optionN = valueN
There are 4 output forms:
org.apache.log4j.ConsoleAppender (Console)
org.apache.log4j.FileAppender (file)
org.apache.log4j.DailyRollingFileAppender (generate one log file every day)
org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches the specified size)
File size can be set through log4j.appender.appenderName.MaxFileSize=100KB
You can also save a backup file through log4j.appender.appenderName.MaxBackupIndex=1.
org.apache.log4j.WriterAppender (send log information in stream format to any specified place)
3. Configure the format (layout) of log information Layout
Layout is responsible for formatting the output of Appender, and the syntax is:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.classlog4j.appender.appenderName.layout.option1 = value1…log4j.appender.appenderName.layout.optionN = valueN
Among them, the layout provided by Log4j is as follows:
org.apache.log4j.HTMLLayout (layout in HTML table form)
org.apache.log4j.PatternLayout (the layout mode can be flexibly specified)
org.apache.log4j.SimpleLayout (contains the level and information string of log information)
org.apache.log4j.TTCCLayout (including time, thread, category, etc. of log generation)
4. Format log information
log4j.appender.appenderName.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
%m The message specified in the output code
%p output priority, i.e. DEBUG, INFO, WARN, ERROR, FATAL
%r Output the number of milliseconds taken from the start of the application to output the log information
%c outputs the category, usually the full name of the class it is located
%t outputs the thread name that generates the log event
%n outputs a carriage return line break, Windows platform is "rn", Unix platform is "n"
%d outputs the date or time of the log time point. The default format is ISO8601. You can also specify the format afterwards, such as: %d{yyyy MMM dd HH:mm:ss, SSS}, the output is similar: October 18, 2002 22:10:28, 921
%l Output the location where the log event occurs, including the class name, the thread that occurred, and the number of lines in the code.
Logger class: Complete logging and set log information level
Appender class: determines the destination of the log, terminal, DB, hard disk
Layout class: determines the style of log output, such as including the current thread, line number, and time
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.