Java Log Design & Practice (2)-Design article

Source: Internet
Author: User

Several problems are mainly considered in the design stage

    1. Split the log by app or function, but to grasp the degree, don't overdo it
    2. Log configuration supports dynamic tuning
Dynamically adjust log levels using Spring's Log4jconfiglistener

Add the following content to the Web. XML location

<context-param>  <param-name>log4jConfigLocation</param-name>  <param-value>/ web-inf/log4j.properties</param-value> </context-param>  <context-param>  < param-name>log4jrefreshinterval</param-name>  <param-value>10000</param-value> </ context-param>  <listener>  <listener-class>   Org.springframework.web.util.Log4jConfigListener  </listener-class> </listener>

  

Note

1. do not designate log4jconfiglocation as /web-inf/classes/log4j.properties< /c16>

When Log4jconfiglocation is specified as/web-inf/classes/log4j.properties, it may cause the class to reload if the log configuration needs to be dynamically adjusted. But dynamically adjusting the/web-inf/log4j.properties does not cause the class to reload

2. log4jrefreshinterval units are in milliseconds

3. /web-inf/log4j.properties and /web-inf/classes/log4j.properties cannot exist simultaneously, Otherwise it will report an exception .

4. problems with JUnit unit test classes

The following error is reported when you run the unit test

Log4j:warn No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext) .

Log4j:warn Initialize the log4j system properly.

Log4j:warn See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

The reason is that the default log4j configuration file cannot be found and can be specified manually, such as:

Log4jconfigurer.initlogging ("Classpath:log4j.test.properties");

Log4j.test.properties is a configuration file that is used specifically for the execution of Unit test classes

Specify a separate log file for each project

/web-inf/log4j.properties configured as follows

Log4j.rootlogger=info, filelog4j.appender.file= org.apache.log4j.dailyrollingfileappenderlog4j.appender.file.file=${catalina.home}/logs/ Projectnamelog4j.appender.file.datepattern= '. ' Yyyy-mm-dd '. Log ' log4j.appender.file.encoding=utf-8log4j.appender.file.layout= org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversionpattern=%p%d%m [%l]%n

  

Log4j.appender.file.file=${catalina.home}/logs/projectname

The projectname can be a string that uniquely identifies a project, such as a project name

${catalina.home} represents the current Tomcat engineering root directory, which is set when Tomcat starts

With the above configuration, you can reach a directory that specifies a separate log file for each project

Summary

Here are some questions to answer

"Q" A tomcat loads multiple projects, how can I determine what works in the log?

"A" specifies a separate log file for each project, avoiding the issue of outputting multiple project log files to a single file

"Ask" multiple tomcat to load the same project at the same time, you can use log4j Dailyrollingfileappender and other file class Appender?

Yes, when you specify a file name, you can use a similar

Log4j.appender.file.file=${catalina.home}/logs/projectname

Configuration so that each tomcat log is saved separately

"Q" multiple tomcat load the same project at the same time, can you specify the log output directory separately?

"Answer" can be the answer to a question

"Q" Can I dynamically adjust the log level of a class?

"Answer" can provide a way of thinking in this article

Java Log Design & Practice (2)-Design article

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.