Configure the relative path of the log file in Log4j [sequent]
This article is based on a complete solution to the problem of "Configuring the relative path of log files in log4j" circulating on the Internet. This blog post has been reproduced several times and has been published to the present. This is a reprinted Article []. We recommend that you read this article to understand the problems solved in this article.
This article finally mentions some exceptions and warnings during log4j configuration file initialization. In this case, I have some understanding in actual production and believe that the relative path of log4j configuration log files is necessary. Using system environment variables will depend on the system for migration, it is obviously not advisable to use the absolute path, so we will continue to explore the relative path of the log4j configuration file.
Notify ServletContextListener about context initialization before initializing all filters and servlets in the Web application. Then you can try to complete the path setting and configuration file initialization in log4j configuration in the contextInitialized () method.
1. The created Log4jListenter:
Public class Log4jListener implements ServletContextListener {
@ Override
Public void contextInitialized (ServletContextEvent arg0 ){
ServletContext context = arg0.getServletContext ();
String prefix = context. getRealPath ("/");
String file = context. getInitParameter ("log4j ");
String filePath = prefix + file;
Properties props = new Properties ();
Try {
FileInputStream istream = new FileInputStream (filePath );
Props. load (istream );
Istream. close ();
String logFile = prefix
+ Props. getProperty ("log4j. appender. FILE. File ");
// Set the path
Props. setProperty ("log4j. appender. FILE. File", logFile );
// Load log4j configuration information
PropertyConfigurator. configure (props );
} Catch (IOException e ){
System. out. println ("cocould not read configuration file [" + filePath
+ "].");
System. out. println ("Ignoring configuration file [" + filePath
+ "].");
}
}
@ Override
Public void contextDestroyed (ServletContextEvent arg0 ){
}
}
By obtaining the project path, the absolute path of the configuration file is obtained by combining the log4j configuration file with the project path; the configuration information is read; and then "log4j" is reset. appender. file. file value, which is the path of the log input, which is more flexible.
Here, the "log4j. appender. File. File" is more specific to the actual configuration content of log4j. properties. The following is the log configuration file used:
# Logs
Log4j. rootLogger = INFO, FILE
# FILE
Log4j. appender. FILE = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. FILE. Threshold = INFO
Log4j. appender. FILE. ImmediateFlush = true
Log4j. appender. FILE. File = brs. log
Log4j. appender. FILE. DatePattern = '. 'yyyy-MM-dd
Log4j. appender. FILE. layout = org. apache. log4j. PatternLayout
Log4j. appender. FILE. layout. conversionPattern = [% d {yyyy-MM-dd HH: mm: ss \} %-5 p] [% t] {% c: % L}-% m % n
According to the Code in 1, the output log file path is: project directory/brs. log
2. Configure in web. xml
<Context-param>
<Param-name> log4j </param-name>
<Param-value> WEB-INF/classes/log4j. properties </param-value>
</Context-param>
<Listener>
<Listener-class> com. test. Log4jListener </listener-class>
</Listener>
The path of the project corresponding to the log4j configuration file and the implementation class of ServletContextListener are configured above. Of course, whether context-param needs to be set here depends on the usage in the Log4jListener class.
3. Starting a web application does not involve the last issue mentioned in the article "configure the relative path of log files in log4j.
I can't help but lament that the author of Spring has come up with it, and it is easier to use Spring in Web applications. Configure the log file output path in the log configuration file as follows:
Log4j. properties file:
Log4j. appender. FILE. File =$ {webapp. root}/WEB-INF/logs/brs. lo
Web. xml file:
<Listener>
<Listener-class> org. springframework. web. util. Log4jConfigListener </listener-class>
</Listener
Yes, that's right! Spring listens to log configuration in this way!
However, the "webapp. root" variable used above is the root directory of the Web application. For the ins and outs of "webapp. root", you can view the source code: org. springframework. web. util. WebUtils class.
Log4j getting started
Configure Log4j in Hibernate to display SQL Parameters
Log4j learning notes (1) _ Log4j basics & configuration item Parsing
Log4j learning notes (2) _ Log4j configuration example & integrate Log4j with Spring
This article permanently updates the link address: