About the configuration of log4j log files

The log4j log system of tomcat is successfully configured, the format: HTML + daily log file named yyyy-mm-dd.log
1. Introduction: The
    internship unit asks the log4j to configure the log system of the webapp, and the log file required to be generated is in html format and is Daily log file named in yyyy-mm-dd date format.
    This has made me troubled for a while, and it took two days recently to finally configure it successfully! However, the successful configuration is tomcat, not a single webapp. Now record the steps and configure the webapp at night. Write the step-by-step operation first, and then explain some key knowledge points of log4j.

2. Configure tomcat log4j logging system

step 1: Download log4j.jar, and put log4j.jar in $CATALINA_HOME/common/lib.
Step 2: Create a new log4j.properties file and put it in $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


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

log4j.appender.R.File=F:/UserWorkSpace/jiangcm/ Tomcat 5.0/logs/tomcat_log_ Change

the path to the logs directory of your Tomcat. If your system environment is configured with the CATALINA_HOME environment variable, you can use ${catalina.home}/logs/tomcat_log_ to configure it.

2) The above log4j configuration only records the more important logs (warning warning, error error, fatal error fatal) to the tomcat_log_ file in the logs directory of Tomcat. The first message after 0:00 every night will trigger the renaming of tomcat_log_, Appends the current date to the tomcat_log_ filename and produces a new tomcat_log_ file.

3) Use FileAppender to output log information to a file, but it is inconvenient to read if the file is too large. At this point you can use the DailyRollingAppender. DailyRollingAppender can output Log information to files distinguished by date. For example, the above log4j.properties configuration file will generate an html file every day, and each html file only records the log information of the day.

4) After the DailyRollingAppender is set, if the maximum value of the file is set in the configuration item, it should be removed, otherwise the log in days may not be generated.

5) Log4j's org.apache.log4j.DailyRollingFileAppender source code line 223
scheduledFilename = fileName+sdf.format(new Date(file.lastModified()));

that is to say, the backup date file is the file name followed by your Matched DatePattern formatted string. Unless you write an appender yourself.

6) The log file generated by the above log4j.properties has the following name format: tomcat_log_2007-02-24.html;
Note: The format of the first log file generated is as follows: tomcat_log_, which is just the name set in A1.file; The file name after the two log files will be added with the DatePattern formatted string.

7) Configure the log system in the DailyRollingFileAppender format. If you want to verify the result of the generated log file, you should pay attention to one point. You need to close tomcat, modify the system date to the date of the next day, and then restart tomcat, so that a new log file can be automatically generated; Because the date needs to be modified to trigger the generation of a new log file.


Step 4: Restart tomcat.

ok, log4j has now been configured in tomcat.

Step 5: Verify

Verify the log4j logging system configured above:

    use the web application jsp-examples that comes with tomcat, and now the logging system is configured, then if the log is used in the .java file of the web application jsp-examples , then when IE accesses the jsp file of this web app, it should be able to automatically generate log files to the configured path.

    step:
    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 to this file, as follows Show:
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 = 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:
Code added There are two parts, one is to construct a logger object, the other is to output the logger information, as shown below, other codes are not changed:
static Logger 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 2007-02-26;
    e) Restart tomcat;
    f) Enter "http://localhost:8086/jsp-examples/dates/date.jsp" in IE, and run the jsp file;
    g) Now in the directory "F:/UserWorkSpace/jiangcm/Tomcat 5.0/logs" I saw the newly generated log file: tomcat_log_2007-02-25.html.
   
    ok! The log system configuration is successful, la~! !

3. Remaining Problems
3.1 The above configuration is the log system of tomcat, which should work on all webapps under tomcat. Now I have only verified the webapp of jsp-examples, 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, etc. I feel that the log information of webapp should be separated from the system log information of tomcat, but how should it be separated? If the log system is configured separately for each webapp, will these log contents be output to the system log of tomcat?

3.3 No configuration is required. The default log directory of tomcat5.0 "F:/UserWorkSpace/jiangcm/Tomcat 5.0/logs" has log files generated by date. The file naming format is as follows: localhost_log.2007-02-26.txt , and this file is automatically generated. The above configuration generates a tomcat_log_2007-02-25.html file in the directory "F:/UserWorkSpace/jiangcm/Tomcat 5.0/logs", and tomcat itself also automatically generates a localhost_log.2007- 02-26.txt file, which is equivalent to automatically generating two log files.
Of course, the contents of the two log files are different. The contents of tomcat_log_2007-02-25.html are as follows:
To be posted and the contents

of localhost_log.2007-02-26.txt are 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]]
2007-02-26 14:03:22 StandardContext[/jsp-examples]ContextListener: contextInitialized()
2007-02-26 14:03:22 StandardContext[/jsp-examples]SessionListener: contextInitialized()
2007-02-26 14:03:22 StandardContext[/servlets-examples]ContextListener: contextInitialized()
2007-02-26 14 :03:22 StandardContext[/servlets-examples]SessionListener: contextInitialized()

feels like localhost_log.2007-02-26.txt is the system log of tomcat, and omcat_log_2007-02-25.html is written by itself in .java logger's content.

So, is it that the system log in tomcat5 will be automatically generated on a daily basis without configuration; if developers need a log system, then they can configure one in their webapp, and do not need to care about the system log of tomcat? ? ?

3.4 The reason why the contents of tomcat_log_2007-02-25.html and localhost_log.2007-02-26.txt are different is it possible that it is because:
the level of apache set in your own log4j.properties is warn:? ?
log4j.logger.org.apache=warn

just verified it, it doesn't seem to matter, the sentence "log4j.logger.org.apache=warn" seems to not only have no effect on localhost_log.2007-02-26.txt, but also on tomcat_log_2007- The output of 02-25.html also has no effect.
So what is the purpose of the sentence "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? ? ?

4. References:
1. I want to generate a log file named "yyyy-mm-dd.log" every day. How should 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 Currently running, close Tomcat.
2. Download the Commons Logging distribution from the Apache website.
3. Unzip the commons-logging.jar in the compressed package to the common/lib directory of your Tomcat installation path.
4. Download the Log4j distribution from the Apache website.
5. Unzip the log4j-1.2.12.jar in the compressed package to the common/lib directory of your Tomcat installation path.
6. Create the log4j configuration file log4j.properties in common/classes of the Tomcat installation path (the configuration file is in the following section).
7. Restart Tomcat


3. Use log4j in web application
http://www.3doing.net/forums/printpage.asp?BoardID=11&ID=184

Note: The load-on-startup above should be set to 1 so that the servlet will be loaded when the web container starts. The log4j.properties file is placed in the properties subdirectory of the root, and it can be placed in other directories as well. The .properties file should be stored centrally for easy management.

4. Configuration of og4j log file pathhttp://wangluwww.spaces.live.com/blog/
Solve

the configuration problem of the location of the log file output by log4j

I always hope that the configuration of my web application can be simplified, the best way is After making a package, it can be used anywhere. Even if you have to do some configuration, I hope it is just some configuration information of the connection pool. This goal is almost achieved in the project at hand, except for the location of the log file.
In the log4j configuration file, the path of the log output has always been an absolute path, which leads to the need to redefine the output location of the log file when deploying on different machines. Although it doesn't take much time, it always feels a little depressing for someone like me who would rather do less than do more.
Stepping through the iron shoes, there is no need to find it, and it takes no effort to get it. I saw a document without comment, which said that the log4j configuration file supports windows environment variables, the format is similar to velocity: ${env}, ok, the problem is solved:
Generally speaking, we will put some initialization information into web.xml, to run at system startup. Following this idea, put the location of the log file anywhere under the webapp, such as under webapp/WEB-INF/conf/logs, you can make the following definitions in the log4j.properties file:
log4j.appender.R.File=${webappHome}/WEB-INF/conf/logs/sysout.log
where ${webappHome} is implemented by System.setProperty("webappHome", webappHome);, this operation can be 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 will add a judgment before each Debug/Info when using Log4j?

Many people may have said, what does it matter, I set the Log Level to Error when running.

Then look at the following code:

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

Look at this code again:

String result;
...
...
...
if (logger.isDebugEnabled())
{
       logger.debug("result is" + result );
}


Set the Log Level to ERROR, does the result differ? no! !
Is there a difference in the performance of the program? The difference is big!!!

Because of what?

Although the first piece of code does not generate Log output, the string variable in debug() is instantiated, understand?

This is why a lot of people complain that Log4j takes a lot of CPU time, especially when it comes to big loops.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324591734&siteId=291194637