1. log4j Log Configuration
①log4j instead of Tomcat's own log
If you want to use log4j to print out the Tomcat log in detail, you can do it in the following way
First, put the common-logging and log4j packages in Tomat/common/lib
Then put the log4j.properties file into the tomcat/common/classes
② Configuration output According to the different levels of the log
Configuration file:
# # # set log levels # # #
Log4j.rootlogger = Debug, stdout, D, E
# # # Output to console # # #
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{absolute}%5p%c{1}:%l-%m%n
# # # Output to log file # # #
LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender
Log4j.appender.d.file = Logs/log.log
Log4j.appender.d.append = True
Log4j.appender.d.threshold = Debug # # Output debug-level logs
Log4j.appender.d.layout = Org.apache.log4j.PatternLayout
Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
# # # Save exception information to a separate file # # #
LOG4J.APPENDER.E = Org.apache.log4j.DailyRollingFileAppender
# # Exception Log file name
Log4j.appender.e.file = Logs/error.log
Log4j.appender.e.append = True
# # Only output logs above the error level!!!
Log4j.appender.e.threshold = ERROR
Log4j.appender.e.layout = Org.apache.log4j.PatternLayout
Log4j.appender.e.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n
Used in code
public class Testlog4j {
public static void Main (string[] args) {
Propertyconfigurator.configure ("D:/code/conf/log4j.properties");
Logger Logger = Logger.getlogger (testlog4j. Class);
Logger.debug ("Debug");
Logger.error ("error");
}
}
③, log4j Configuration Xiang Solution
The first step: Join Log4j-1.2.8.jar to Lib.
Step two: Establish log4j.properties under the classpath. The contents are as follows:
1 Log4j.rootcategory=info, stdout, R
2
3 Log4j.appender.stdout=org.apache.log4j.consoleappender
4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
5 LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%c.%m (%l) | %m%n
6
7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
8 Log4j.appender.r.file=d:/tomcat 5.5/logs/qc.log
9 Log4j.appender.r.layout=org.apache.log4j.patternlayout
Ten Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n
11
Log4j.logger.com.neusoft=debug
Log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error
Log4j.logger.org.apache.commons=error
Log4j.logger.org.apache.struts=warn
Log4j.logger.org.displaytag=error
Log4j.logger.org.springframework=debug
Log4j.logger.com.ibatis.db=warn
Log4j.logger.org.apache.velocity=fatal
21st
Log4j.logger.com.canoo.webtest=warn
23
Log4j.logger.org.hibernate.ps.preparedstatementcache=warn
Log4j.logger.org.hibernate=debug
Log4j.logger.org.logicalcobwebs=warn
The third step: the corresponding modification of the properties, before you have to know what these are, in the second part of the explanation.
Fourth step: Add the relevant statement in the class to output the log:
Definition attribute: Protected final Log log = Logfactory.getlog (GetClass ());
In the appropriate method:
if (log.isdebugenabled ())
{
Log.debug ("System .....");
}
Ii. Description of Log4j
1 Log4j.rootcategory=info, stdout, R
This sentence is the output of the log information of level info to stdout and R, the definition of stdout and r in the following code, can be arbitrarily named. Level can be divided into off, FATAL, error, WARN, INFO, DEBUG, all, if the configuration off does not play any information, if configured as info so only display info, WARN, error log information, and DEBUG information will not be displayed, Refer to the third section to define the logger in the configuration file.
3 Log4j.appender.stdout=org.apache.log4j.consoleappender
This sentence defines what type of output is named stdout, which can be
Org.apache.log4j.ConsoleAppender(console),
Org.apache.log4j.FileAppender(file),
Org.apache.log4j.DailyRollingFileAppender(A log file is generated every day),
Org.apache.log4j.RollingFileAppender(A new file is generated when the file size reaches the specified size)
Org.apache.log4j.WriterAppender(Send log information to any specified location in stream format)
Refer to the third section to define the Appender in the configuration file.
4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
This sentence is the type of layout that defines the output named StdOut, which can be
Org.apache.log4j.HTMLLayout(Layout in HTML table Form),
Org.apache.log4j.PatternLayout(You have the flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout(contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout(Contains information about the time, thread, category, etc.) of the log
Refer to the third section to define layout in the configuration file.
5 log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n
If you use the pattern layout to specify the exact format of the printed information Conversionpattern, print the parameters as follows:
%mThe message specified in the output code
%pOutput priority, i.e. Debug,info,warn,error,fatal
%rThe number of milliseconds to output the log information from the app boot to output
%cThe output belongs to the class, which is usually the full name of the class
%tOutput the name of the thread that generated the log event
%nOutput a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%dThe date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%lThe location where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.
[QC]Is the beginning of the log information, which can be any character, generally referred to as the project abbreviation.
The output information
[TS] DEBUG [main] Abstractbeanfactory.getbean (189) | Returning cached instance of singleton Bean ' myautoproxy '
Refer to the third section to define the formatting log information in the configuration file.
7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender
This sentence is the same as the 3rd line. The type that defines the output that is named R produces a log file every day.
8 Log4j.appender.r.file=d:/tomcat 5.5/logs/qc.log
This is a file named D:/tomcat that defines the output named R 5.5/logs/qc.log
You can modify it yourself.
9 Log4j.appender.r.layout=org.apache.log4j.patternlayout
Same as Line 4th.
Ten Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n
Same as Line 5th.
Log4j.logger.com. Neusoft =debug
Specifies that all classes under the Com.neusoft package are rated as Debug.
You can change the Com.neusoft to the package name that you use for your project.
Log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error
These two sentences are the error level of the two packages, and if Ehcache is not configured in the project, these two sentences are not required.
Log4j.logger.org.apache.commons=error
Log4j.logger.org.apache.struts=warn
These two sentences are struts's packages.
Log4j.logger.org.displaytag=error
This sentence is Displaytag's bag. (used by the QC Problem List page)
Log4j.logger.org.springframework=debug
This sentence is spring's package.
Log4j.logger.org.hibernate.ps.preparedstatementcache=warn
Log4j.logger.org.hibernate=debug
These two sentences are Hibernate's package.
The above package settings can be customized according to the actual situation of the project.
Three, log4j detailed
1, defining the configuration file
Log4jSupport two configuration file formats, one is XML format file, one is Java attribute file Log4j.properties (key = value). The following describes how to use the Log4j.properties file as a configuration file:
①, configure Root Logger
LoggerMost of the operations that are responsible for processing the log records.
Its syntax is:
Log4j.rootlogger = [level], Appendername, Appendername, ...
Level is the priority of logging, which is divided into off, FATAL, ERROR, WARN, INFO, DEBUG, all, or custom levels. LOG4J recommends using only four levels, with the priority from high to low being error, WARN, INFO, DEBUG. By defining the level here, you can control the switch to the appropriate level of log information in your application. For example, if the info level is defined here, only if it is equal to or above that level, the log information for all debug levels in the application will not be printed. All: Print all logs, off: Turns off all log output. Appendername is where you specify where the log information is exported. Multiple output destinations can be specified at the same time.
Ii, configure log information output destination Appender
AppenderThe output that is responsible for controlling the logging operation.
Its syntax is:
Log4j.appender.appenderName = Fully.qualified.name.of.appender.class
Log4j.appender.appenderName.option1 = value1
...
Log4j.appender.appenderName.optionN = Valuen
Here the appendername is defined in the ①, can be arbitrarily named.
Among them, LOG4J provides the following types of Appender:
Org.apache.log4j.ConsoleAppender(console),
Org.apache.log4j.FileAppender(file),
Org.apache.log4j.DailyRollingFileAppender(A log file is generated every day),
Org.apache.log4j.RollingFileAppender(When the file size reaches the specified size, a new file is created), the file size can be set by LOG4J.APPENDER.R.MAXFILESIZE=100KB and the log4j.appender.r.maxbackupindex= 1 set to save a backup file.
Org.apache.log4j.WriterAppender(Send log information to any specified location in stream format)
Example: Log4j.appender.stdout=org.apache.log4j.consoleappender
Define an output destination named stdout, Consoleappender as the console.
③, configuration log Information Format (layout) layout
LayoutResponsible for formatting the output of the Appender.
Its syntax is:
Log4j.appender.appenderName.layout = Fully.qualified.name.of.layout.class
Log4j.appender.appenderName.layout.option1 = value1
...
Log4j.appender.appenderName.layout.optionN = Valuen
Among them, log4j offers the following types of layout:
Org.apache.log4j.HTMLLayout(Layout in HTML table Form),
Org.apache.log4j.PatternLayout(You have the flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout(contains the level of log information and the information string),
Org.apache.log4j.TTCCLayout(Contains information about the time, thread, category, etc.) of the log
2, formatting log information
Log4jFormat the log information in a print format similar to the printf function in C, with the following printing parameters:
%mThe message specified in the output code
%pOutput priority, i.e. Debug,info,warn,error,fatal
%rThe number of milliseconds to output the log information from the app boot to output
%cThe output belongs to the class, which is usually the full name of the class
%tOutput the name of the thread that generated the log event
%nOutput a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%dThe date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%lThe location where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.
3, using log4j in your code
We do the following three jobs in classes that require output log information:
1
Private Final log log = Logfactory.getlog (GetClass ());
Logfactory.getlog ()
3
if (log.isdebugenabled ())
{
Log.debug ("111");
}
if (log.isinfoenabled ())
{
Log.info ("222");
}
if (log.iswarnenabled ())
{
Log.warn ("333");
}
if (log.iserrorenabled ())
{
Log.error ("444");
}
if (log.isfatalenabled ())
{
Log.fatal ("555")
}
Tomcat Log Configuration