Java log Design & Practice (2), java log
Several considerations during the design phase
Use Spring Log4jConfigListener to dynamically adjust the Log Level
Add the following content to the corresponding 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 setLog4jConfigLocationSpecified// WEB-INF/classes/log4j. properties
When log4jConfigLocation is specified as/WEB-INF/classes/log4j. properties, if you need to dynamically adjust the log configuration, it may cause the class to reload. But dynamic adjustments/WEB-INF/log4j. properties won't cause class reloading
2.Log4jRefreshIntervalThe Unit is milliseconds.
3.// WEB-INF/log4j. propertiesAnd// WEB-INF/classes/log4j. propertiesIt cannot exist at the same time; otherwise, an exception is reported.
4.JUnitUnit Test Problems
The following error is reported when you run the unit test.
Log4j: WARN No appenders cocould be found for logger (org. springframework. context. support. ClassPathXmlApplicationContext ).
Log4j: WARN Please 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 manually specified, for example:
Log4jConfigurer. initLogging ("classpath: log4j. test. properties ");
Log4j. test. properties is a configuration file used specifically for executing unit test classes.
Specify an independent log file for each project
// WEB-INF/log4j. propertiesThe configuration is 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
Project name can be a string that uniquely identifies a project, such as a project name.
$ {Catalina. home} indicates the current tomcat project root directory. This variable is set during Tomcat startup.
With the preceding configuration, you can create a directory that specifies an independent log file for each project.
Summary
Answer the following questions:
[Q] How do I determine the project in the log when a Tomcat loads multiple projects?
[Answer] specify an independent log file for each project to avoid the problem of outputting multiple project log files to one file.
[Q] Can I use log4j DailyRollingFileAppender and other file class Appender when multiple Tomcat servers load the same project at the same time?
[Answer] Yes. You can use a file name similar
Log4j. appender. file. File =$ {catalina. home}/logs/projectName
So that each Tomcat log is saved separately
[Q] Can I specify the log output directory when multiple Tomcat servers load the same project at the same time?
[Answer] You can answer the same question.
[Q] Can I dynamically adjust the log level of a class?
[Answer] This article provides an idea.