. Introduction:
《Log4jAdvantages over system. OutArticle)
Log4jIs an API class library that helps developers manage log output. Its most important feature is that the configuration file can be flexibly set daily.
The priority of log information, the output destination of log information, and the output format of log information.
Log4jBesidesProgramBesides running log information, an important function is to display debugging information.
Programmers often encounter debugging programs out of the Java IDE environment. At this time, most people choose to use system. Out. println.
Statement to output a variable value for debugging. This will cause a very troublesome problem: Once a programmer decides not to display this
Some system. Out. println items can only be commented out in one row. If you need to debug variable values one day, you can only
Remove the comments in another row and restore the system. Out. println statement.
UseLog4jIt can handle similar situations well.
2)Log4j-- Something that worries me for a while:
Fengy asked for configuration in FebruaryLog4jI haven't gotten it out for a week, and I don't know which environment it is.
An error occurred. At that time, I didn't know much about relative paths and common interfaces. After wangwm is configured, I still don't know why.
It cannot run.
Now let's look back and think about it. There has been no progress because (a) There is no java. Io knowledge; (B) there is no relative Java path.
Knowledge of paths; (c) what must be done to complete configuration.
At that time, I couldn't figure it out and kept STUCK IN THE. Java file "propertyconfigurator. Configure ".
(Myfile. getpath (); "this lineCodeAt that time, no matter how it was readLog4j. Properties file, think a lot of ways,
This includes how to read attribute files, and how to use relative paths to read attribute files. However, these are all in the fog, and the essence is not clear,
No more details can be found.
NowLog4jThe configuration has a bit of knowledge!
2. First, differentiate the configuration in each environmentLog4jWhat should be done:
1) Java engineering environment:
After jdk1.4, the log function is integrated. The D: \ Program Files \ Java \ jre1.5.0 _ 04 \ lib directory contains logging. properties.
File. If you want to use logs, you can directly use or modify the logging. properties attribute file!
2) in Tomcat, use log4j Configure the log system:
A)
Step 1: download log4j . jar, and set log4j . jar into $ catalina_home/common/lib.
Step 2: Write the configuration file log4j . properties:
# log4j . properties
Log4j. Rootlogger = warn, A1, warnlog
Log4j. Appender. A1 = org. Apache.Log4j. Leleappender
Log4j. Appender. a1.layout = org. Apache.Log4j. Patternlayout
# Print the date in iso8601 format
Log4j. Appender. a1.layout. conversionpattern = % d [% T] %-5 p % C-% m % N
Log4j . Appender. warnlog = org. Apache. Log4j . Fileappender
Log4j . Appender. warnlog. maxfilesize = 50000kb
Log4j . Appender. warnlog. maxbackupindex = 0
Log4j . Appender. warnlog. File = F:/userworkspace/jiangcm/logs/warn.html
Log4j . Appender. warnlog. layout = org. Apache. Log4j . Htmllayout
Log4j . Appender. warnlog. layout. conversionpattern = % d % x %-5 p % m % N
Log4j.Logger.org. Apache = warn
Step 3: Create a warn.html file in the directory "F:/userworkspace/jiangcm/logs;
Step 4:Log4jPut the. properties file in the $ catalina_home/common/classes/directory.
Step 5: restart tomcat.
OK. Now we have configured it in Tomcat.Log4j.
B) how to use it? -- Get a logger object in the. Java file and then output the log directly. Example:
// Bar. Java
Package com. Foo;
Import org. Apache.Log4j. Logger;
Public class bar {
Static logger = logger. getlogger (bar. Class );
Public void doit (){
Logger. debug ("there is a debug ...");
Logger.info ("there is a info ...");
Logger. Warn ("there is a warning ...");
}
}
OK! That is to say, to configure in Tomcat, you only needLog4j. Jar andLog4jPut. properties to $ catalina_home/common/
Under lib and classes! You can directly use it in. Java without additional settings.
3) configure it in Tomcat's webappLog4j: Configure a single webapp.
This is a little troublesome! Because it is configured in the entire tomcat, Tomcat will automatically readLog4j. Properties; however, webapp requires
Write a Servlet and initialize it.Log4jAnd set servlet to tomcat in Web. XML to start automatically.
This method has not been successfully tested, but the wangwm configuration is successful.
However, the general steps are the same. You can take the following steps:
Step 1: DownloadLog4j. Jar, andLog4j. Jar into webappname/WEB-INF/classes/lib.
Step 2: Write the configuration fileLog4j. Properties:
Note:
SolutionLog4jConfiguration of the location of the output Log File
Http://wangluwww.spaces.live.com/blog/
I have always hoped that the configuration of my web application can be simplified. The best way is to use it whenever a package is opened, even if I have to make some configuration.
You also want to configure the connection pool. This goal is almost achieved in the current project, except for the log
File Location.
InLog4jThe log output path in the configuration file always uses an absolute path, which will be required for deployment on different machines.
Redefines the output location of the log file. Although it takes a little time, it is always easier for me to have less than one thing.
I felt a little depressed.
It takes no effort to break the shoes. I saw a document and explained it.Log4jConfiguration File supportWindowsEnvironment Change
Volume. The format is similar to velocity :$ {env}. OK. solution:
In general, we will put some initialization information into web. XML to run it at system startup. Follow these steps
Location to any place under the webapp, such as webapp/WEB-INF/CONF/logs, you canLog4jIn the. properties file.
Definition:
Log4j. Appender. R. File =$ {webapphome}/WEB-INF/CONF/logs/sysout. Log
$ {Webapphome} is implemented through system. setproperty ("webapphome", webapphome );.
An initial servlet. The specific method is omitted.
Step 4:Log4jPut the. properties file in the WEB-INF/classes/directory.
Step 5: Write an initialized servlet;
Step 6: configure the servlet in Web. xml and set it to auto start;
Step 7: restart tomcat.
For details, refer to the following materials:
Fried pork with bamboo shootsLog4jStudy Notes (1) (2) (3)
Http://hedong.3322.org/archives/000193.html
Http://hedong.3322.org/archives/000195.html
Http://hedong.3322.org/archives/000197.html