Original address: http://huanyue.iteye.com/blog/574598
log4j when you specify the location of the log file is to use absolute path, so when the deployment environment is very cumbersome to change, find a number of methods on the Internet, personal feeling the following methods are more applicable:
Because spring also loads log4j.properties, log information is sent to the console if it is not loaded. Spring provides a log4jconfiglistener that itself can be configured in Web.xml to specify the location to load the log4j configuration file and log output path, noting that the listener needs to be placed before spring's listener.
In fact, Log4jconfiglistener is more suitable for log4j to use in Web projects for the following reasons:
1. Dynamic changes to record levels and policies do not require the restart of Web applications, as described in "effective Enterprise Java."
2. Set the log file in/web-inf/logs/without the need to write an absolute path.
Because the system presses the path of the Web directory into a system variable called Webapp.root. This does not need to write the absolute path when writing the log file path.
Log4j.appender.logfile.file=${webapp.root}/web-inf/logs/settlement.log
3. Log4j.properties can be put together with other properties in/web-inf/, not class-path.
4. Log4jrefreshinterval 60000 means that a watchdog thread scans the profile changes every 60 seconds;
Add in Web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value> web-inf/log4j.properties</param-value>
</context-param>
<context-param>
< param-name>log4jrefreshinterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
</listener>
The log4j configuration file is as follows:
Log4j.appender.stdout=org.apache.log4j.consoleappender
log4j.appender.stdout.layout= Org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n
Log4j.rootlogger=info, Stdout,r
log4j.appender.r=org.apache.log4j.dailyrollingfileappender
Log4j.appender.r.file=${webapp.root}/web-inf/logs/settlement.log
Log4j.appender.r.datepattern = "." yyyy-mm
log4j.appender.r.layout=org.apache.log4j.patternlayout
log4j.appender.r.layout.conversionpattern=%d%p [%c]-%m%n
Other:
LOG4J offers the following kinds of Appender
o Org.apache.log4j.ConsoleAppender (console)
o Org.apache.log4j.FileAppender (file)
o org.apache.log4j.DailyRollingFileAppender (Generate a log file every day)
o Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size)
o Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)
We develop the B/s structure system, in the service side generally have daily running log records. When you save it in the form of a log file, you often encounter a problem: the log file is too large. Hundreds of megabytes of log files are also a problem for logging information. So I want to be able to generate a log file every day or every month so that the file is not too large. or according to the log file size to judge, over the specified size, the log automatically add new files.
The implementation of both approaches in log4j is simple, as long as it is set in the configuration file.
One, according to a certain amount of time to produce log files, configuration files are as follows:
# Set Root logger level to ERROR and it only appender to A1.
Log4j.rootlogger=error,r
# R is set to be a dailyrollingfileappender.
Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
Log4j.appender.r.file=backup.log
Log4j.appender.r.datepattern = "." Yyyy-mm-dd
Log4j.appender.r.layout=org.apache.log4j.patternlayout
Log4j.appender.r.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n
The above configuration is to generate a backup file every day. Where the backup file is named Backup.log.
The effect is this: the log information for the day is recorded in the Backup.log file, and the previous date was recorded in a file named Backup.log.yyyy-mm-dd.
Similarly, if you need to generate a file every month to modify the configuration above:
Will
Log4j.appender.r.datepattern = "." Yyyy-mm-dd
To
Log4j.appender.r.datepattern = "." yyyy-mm
Second, automatically generate new log files based on log file size
The contents of the configuration file are as follows:
# Set Root logger level to ERROR and it only appender to A1.
Log4j.rootlogger=error,r
# R is set to be a rollingfileappender.
Log4j.appender.r=org.apache.log4j.rollingfileappender
Log4j.appender.r.file=backup.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=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n
which
#日志文件的大小
log4j.appender.r.maxfilesize=100kb
# Save a backup file
Log4j.appender.r.maxbackupindex=1
Other:
Log display level:
They define the level of log display, which is divided into five levels: DEBUG, INFO, WARN, error, and fatal. These five levels are in order, DEBUG < INFO < WARN < ERROR < FATAL, it is important to understand this, here log4j has a rule: the assumption is set a level of P, if a level q is higher than P, it can be activated, otherwise shielding off.