Configure the Tomcat Log System

Source: Internet
Author: User

The log4j log system of Tomcat is successfully configured. Format: HTML + Log File named by yyyy-mm-dd.log every day
I. Introduction:
The practice organization requires log4j to be used to configure the webapp log system. The generated log file must be in HTML format and be a log file named every day in yyyy-mm-dd date format.
This once made me worry for a while, and it took the last two days to finally complete the configuration! However, the configuration is successful for Tomcat, not for a single webapp. Now we record the steps and configure the webapp at night. First, write down the operations step by step, and then explain some key log4j knowledge points.

2. Configure the Tomcat log4j log system step by step

Step 1: Download log4j. jar and put log4j. jar into $ catalina_home/common/lib.
Step 2: Create the log4j. properties file and put it in the $ catalina_home/common/classes/directory.
Step 3: Write the configuration file log4j. properties:
# Log4j. Properties

Log4j. rootlogger = warn, A1

Log4j. appender. A1 = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. a1.file = F:/userworkspace/jiangcm/tomcat 5.0/logs/tomcat_log _
Log4j.appender.a1.datepattern=yyyy-mm-dd'.html'
Log4j. appender. a1.layout = org. Apache. log4j. htmllayout

Log4j.logger.org. Apache = warn


Note:
1) You only need to modify this line:

Log4j. appender. R. File = F:/userworkspace/jiangcm/tomcat 5.0/logs/tomcat_log _

Modify the path to your Tomcat logs directory. If you have configured the catalina_home environment variable in your system environment, you can use $ {Catalina. Home}/logs/tomcat_log _ to configure it.

2) The above log4j configuration only records important logs (warning, error, fatal error fatal) to the tomcat_log _ file in the logs directory of Tomcat, the first message at every night will trigger the rename of atat_log _, append the current date to the tomcat_log _ file name, and generate a new tomcat_log _ file.

3) You can use fileappender to output log information to a file, but it is inconvenient to read the file if it is too large. Then you can use dailyrollingappender. Dailyrollingappender can output log information to files differentiated by date. For example, the preceding log4j. properties configuration file generates an HTML file every day. Each HTML file only records the log information of the current day.

4) After dailyrollingappender is set, if the maximum value of the file is set in the configuration item, remove it. Otherwise, logs in days may not be generated.

5) line 5 of the source code of org. Apache. log4j. dailyrollingfileappender of log4j
Scheduledfilename = filename + SDF. Format (new date (file. lastmodified ()));

That is to say, all the backup files are strings formatted with datepattern after the file name. Unless you write an appender yourself.

6) The above log4j. properties generated log file, name format: tomcat_log_2007-02-24.html;
Note: the format of the first log file generated is as follows: tomcat_log _, that is, the name set in a1.file; the file name after the second log file is generated will add the string formatted with datepattern.

7) configure the log system in the dailyrollingfileappender format. If you want to verify the generated log file results, note that you need to disable tomcat, change the system date to the next day, and restart tomcat, in this way, a new log file can be generated automatically, because a new log file can be generated only when the date needs to be modified.


Step 4: restart tomcat.

OK. Now you have configured log4j in Tomcat.

Step 5: Verify

Verify the log4j log system configured above:

Using the web application JSP-examples that comes with tomcat, now the log system has been configured, if the web application JSP-examples. if logs are used in the Java file, when ie accesses the JSP file of the web app, it should be able to automatically generate the log file to the configuration path.

Steps:
A) enter the directory "F: \ userworkspace \ jiangcm \ Tomcat 5.0 \ webapps \ JSP-examples \ WEB-INF \ Classes \ dates ";
B) modify the jspcalendar. Java file and add the Log Code in this file, as shown below:
Package dates;

Import java. Text. dateformat;
Import java. util .*;
Import org. Apache. log4j. Logger;
Import org. Apache. log4j. level;
Import org. Apache. log4j .*;


Public class jspcalendar {
Calendar calendar = NULL;
Static logger = logger. getlogger (jspcalendar. Class );


Public jspcalendar (){
Logger. debug ("this is Debug .");
Logger.info ("this is an info .");
Logger. Warn ("this is a warning .");
Logger. Error ("this is an error .");
Logger. Fatal ("this is a fatal error .");

Calendar = calendar. getinstance ();
Date trialtime = new date ();
Calendar. settime (trialtime );
}

Public int getyear (){

Return calendar. Get (calendar. year );

}
......
}

Note:
The added code consists of two parts: one is to construct a logger object and the other is to output the logger information, as shown below. Other codes are not dynamic:
Static logger = logger. getlogger (jspcalendar. Class );

Logger. debug ("this is Debug .");
Logger.info ("this is an info .");
Logger. Warn ("this is a warning .");
Logger. Error ("this is an error .");
Logger. Fatal ("this is a fatal error .");

C) recompile jspcalendar. java;
D) The modification date is;
E) Restart tomcat;
F) Input "http: // localhost: 8086/JSP-examples/dates/date. jsp" in IE to run the JSP file;
G) at this time in the directory "F: \ userworkspace \ jiangcm \ Tomcat 5.0 \ logs" to see the new log file: tomcat_log_2007-02-25.html.

OK! It indicates that the log system has been configured successfully ~!!

Iii. Legacy issues
3.1 The above configuration is the Tomcat log system, which should be used for all webapps under tomcat. Now I have only verified the JSP-examples webapp and have not verified other webapps.

3.2 The log system of Tomcat is configured, and the output should be only the standard input/output information of Tomcat. It is felt that the log information of webapp should be separated from the system log information of Tomcat, but how should we separate them? If the log system is configured separately for each webapp, will the log content be output to the Tomcat System log?

3.3 without any configuration, tomcat5.0 default log directory "F: \ userworkspace \ jiangcm \ Tomcat 5.0 \ logs" there is a log file generated by date, the file naming format is as follows: localhost_log.2007-02-26.txt, this file is automatically generated. The above configuration is in the directory "F: \ userworkspace \ jiangcm \ Tomcat 5.0 \ logs", which is equivalent to automatically generating two log files.
Of course these two log files have different content. The content in tomcat_log_2007-02-25.html is as follows:
Waiting for texture

The content of localhost_log.2007-02-26.txt is as follows:
2007-02-26 14:03:21 standardcontext [/balancer] Org. apache. webapp. balancer. balancerfilter: Init (): rulechain: [Org. apache. webapp. balancer. rulechain: [Org. apache. webapp. balancer. rules. urlstringmatchrule: Target string: News/redirect URL: http://www.cnn.com], [Org. apache. webapp. balancer. rules. requestparameterrule: Target Param name: paramname/Target Param value: paramvalue/redirect URL: http://www.yahoo.com], [Org. apache. webapp. balancer. rules. accepteverythingrule: Redirect URL: http://jakarta.apache.org]
14:03:22 standardcontext [/JSP-Examples] contextlistener: contextinitialized ()
2007-02-26 14:03:22 standardcontext [/JSP-Examples] sessionlistener: contextinitialized ()
14:03:22 standardcontext [/Servlets-Examples] contextlistener: contextinitialized ()
14:03:22 standardcontext [/Servlets-Examples] sessionlistener: contextinitialized ()

Localhost_log.2007-02-26.txt is the log of the Tomcat System, while omcat_log_2007-02-25.html is the content of the Self-written logger in. java.

In Tomcat 5, system logs are automatically generated on a daily basis without configuration. If developers need a log system, configure one in their own webapp, instead of managing Tomcat System logs ???

3.4 atat_log_2007-02-25.htmland localhost_log.2007-02-26.txt are different because:
Set Apache level in log4j. properties to warn :??
Log4j.logger.org. Apache = warn

The output of outputs is also unaffected.
So what is the role of "log4j.logger.org. Apache = warn ???

3.5 how to configure the log system in webapp and separate the log information of webapp from the system log of Tomcat itself ???

Iv. References:
1. I want to generate a log file named "yyyy-mm-dd.log" every day. How can I configure the file option? Thank you!
Http://community.csdn.net/Expert/TopicView3.asp? Id = 4788993

2. Use log4j to output logs for Tomcat
Http://www.chinawin.net/myblog/archives/314-log4jTomcataeoeOE.html

1. If Tomcat is currently running, disable tomcat.
2. Download the commons logging package from the Apache website.
3. decompress the commons-logging.jar in the package to the common/lib directory of your tomcat installation path.
4. Download The log4j release package from the Apache website.
5. decompress the log4j-1.2.12.jar in the package to the common/lib directory of your tomcat installation path.
6. Create the log4j configuration file log4j. properties in the common/classes in the tomcat installation path (the configuration file is in the subsequent section ).
7. Restart Tomcat


3. Use log4j in Web Applications
Http://www.3doing.net/forums/printpage.asp? Boardid = 11 & id = 184

Note: The above load-on-startup should be set to 1 to load the servlet when the Web Container starts. The log4j. properties file is placed in the root properties subdirectory, or in other directories. The. properties file should be stored in a centralized manner to facilitate management.

4. Configure the path of the og4j Log File
Http://wangluwww.spaces.live.com/blog/

Solve the Problem of location configuration of log files output by log4j

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 some configuration is required, you also want to configure some connection pool information. This goal is almost achieved in the current project, except for the location of log files.
The log output path in the log4j configuration file always uses an absolute path, which leads to the need to redefine the output location of the log file during deployment on different machines. Although it takes a little time, I always feel a little depressed for people like me who prefer less than one thing.
It takes no effort to break the shoes. Inadvertently, I saw a document describing how the log4j configuration file supports Windows environment variables in a format similar to velocity :$ {env}, OK. Problem Solving:
In general, we will put some initialization information into web. XML to run it at system startup. Following this idea, place the location of the log file to any place under the webapp, such as webapp/WEB-INF/CONF/logs, the following definitions can be made in the log4j. properties file:
Log4j. appender. R. File =$ {webapphome}/WEB-INF/CONF/logs/sysout. Log
$ {Webapphome} is implemented through system. setproperty ("webapphome", webapphome);. This operation can be performed through an initial servlet. The specific method is omitted.

5. log4j Optimization
Http://www.3doing.net/forums/printpage.asp? Boardid = 11 & id = 184

How many people use log4j to add a judgment before each debug/info?

As many people may have said, what is the relationship between this? I set the log level to error during running.

Please refer to the following code:

String result;
...
...
...
Logger. debug ("result is" + result );

Let's look at this Code:

String result;
...
...
...
If (logger. isdebugenabled ())
{
Logger. debug ("result is" + result );
}


Set log level to error. Is the running result different? No !!
Is the program performance different? Big difference !!!

Why?

Although the first code does not generate log output, the string variables in debug () are instantiated. Do you understand?

This is why many people complain that log4j takes a lot of CPU time, especially in large loops.

Add such judgment conditions to your program.

Configure the Tomcat Log System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.