Set the directory by yourself, that is, set through system. setproperty at project startup to implement servletcontextlistener:
Public class log4jlistener implements servletcontextlistener {
Public static final string log4jdirkey = "log4jdir ";
Public void contextdestroyed (servletcontextevent ){
System. getproperties (). Remove (log4jdirkey );
}
Public void contextinitialized (servletcontextevent ){
String log4jdir = servletcontextevent. getservletcontext (). getrealpath ("/");
// System. Out. println ("log4jdir:" + log4jdir );
System. setproperty (log4jdirkey, log4jdir );
}
}
Web. xml configuration:
<Listener>
<Listener-class> com. Haier. Framework. util. log4jlistener </listener-class>
</Listener>
Log4j. prtperties Configuration:
# Set root logger level to warn and append to stdout
Log4j. rootlogger = info, console, file
Log4j. appender. Console = org. Apache. log4j. leleappender
Log4j. appender. Console. layout = org. Apache. log4j. patternlayout \ t
# Pattern to output the caller's file name and line number.
# Log4j. appender. Console. layout. conversionpattern = [% P] % l --- % m % N
Log4j. appender. File = org. Apache. log4j. dailyrollingfileappender
# Name the daily log file name by date,
Log4j. appender. file. File =$ {log4jdir}/logs/log. Log
Log4j. appender. file. datepattern = yyyymmdd '. Log'
Log4j. appender. file. layout = org. Apache. log4j. patternlayout
Log4j. appender. file. layout. conversionpattern = [%-5 p] % L-% d {yyyy-mm-dd hh: mm: SS, SSS} method: % L-% m % N
During the test, how can I seek the similar "log. log20140518.log, there is only one log file. log File, the log generated on the 7th day (today) is "log. logs are stored in the form of logs generated on the 7th day by the 8th day (tomorrow. save the log file to log. log201400707.log, and then create an 8-day log. log files, and so on.
Therefore, you must note that you must modify the system time to view the generated logs during the test.