1. log4j Print SQLto tune the log level to debug, the SQL will be displayedLog4j.rootlogger=info,console
Console
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m%n
Log4j.logger.java.sql.resultset=debug
#log4j. Logger.org.apache=info
Log4j.logger.java.sql.connection=debug
Log4j.logger.java.sql.statement=debug
Log4j.logger.java.sql.preparedstatement=debug
2. log4j generate files by date
the implementation of these two methods in log4j is simple, as long as it is set in the configuration file.
One, according to a certain time to generate log files, the configuration file is as follows:
# Set Root Logger level-to-ERROR and its-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 produce a backup file every day. The backup file is named Backup.log.
The specific effect is this: the log information of the day is recorded in the Backup.log file, the previous record in the name calledBackup.log.yyyy-mm-dd in the file.
Similarly, if you need to generate a file per month, you can modify the configuration above:
Will
Log4j.appender.r.datepattern = '. ' Yyyy-mm-dd
Switch
Log4j.appender.r.datepattern = '. ' yyyy-mm
II. 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 its-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
3. log4j log File Save location
Take Dailyrollingfileappender as an example: Suppose a log file per day
the following settings are available:
Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a1.file=app.log
log4j.appender.a1.datepattern= '. ' YYYY-MM-DD
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%d%5p-%c-%-4r [%t]-%m%n
The build log file will be located in the Tomcat Bin directory, if you want the log file to be persisted: under Root/web-info/logs/, there are 4 solutions for the individual:
1 Absolute Path
log4j.appender.a1.file=d:\apache-tomcat-6.0.18/webapps/Project/web-inf/logs/app.log
But the flexibility is poor .
The same setting principle is used in the following 3: Environment variables of the JVM
2:spring's Log4jconfiglistener
with the following configuration:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
...
Log4j.appender.logfile.file=${webapp.root}/web-inf/logs/app.log
...
to solve
2: Use an existing JVM variable:
For example:
Log4j.appender.logfile.file=${user.home}/logs/app.log
The log will be located in: For example windows:c:\documents and Settings\joe\logs\app.log
3 Set the directory yourself, that is, when the project starts with System.setproperty settings
by implementing Servletcontextlistener to solve: for example
public class Log4jlistener implements Servletcontextlistener {
public static final String Log4jdirkey = "Log4jdir";
public void contextdestroyed (Servletcontextevent servletcontextevent) {
System.getproperties (). Remove (Log4jdirkey);
}
public void contextinitialized (Servletcontextevent servletcontextevent) {
String Log4jdir = Servletcontextevent.getservletcontext (). Getrealpath ("/");
System.out.println ("Log4jdir:" +log4jdir);
System.setproperty (Log4jdirkey, Log4jdir);
}
}
Web. XML configuration:
<listener>
<listener-class>com.log4j.log4jlistener</listener-class>
</listener>
log4j.prtperties Configuration:
Log4j.appender.a1.file=${log4jdir}/web-inf/logs/app1.log
log4j print SQL, generate files by date, generate file Locations