Relative path method for configuring log files in log4j

Source: Internet
Author: User
Tags mongodb parent directory postgresql redis log4j


Summed up the above three methods, it is advisable to approach three, but still cumbersome, to write a servlet, write a lot of code



The following method is more practical I think we can try:




Log4j.appender.afile=org.apache.log4j.dailyrollingfileappender
Log4j.appender.afile.datepattern= '. ' Yyyy-mm-dd '. html '
Log4j.appender.afile.file=.. webapps//Project//logs//d1cm_log.html
Log4j.appender.afile.layout=org.apache.log4j.htmllayout
Log4j.appender.afile.layout.conversionpattern=%d{yyyy-mm-dd HH:mm:ss}%p%c%m%n




The default generated log file is in the server's Bin directory (I am using tomcat) so that the relative path is passed. webapps//project name//log directory//log file (or. /webapps/the project name/log directory/log file, you can automatically create log directories and log files in your project.






method One: replace absolute path with relative path



The main is to expand the Log4j Rollingfileappender class, the other fileappender the same reason. The extension method is to use a subclass to overwrite the Setfile method, which is called when the log4j reads the configuration file to generate Appender, and the path in the configuration file is passed in so that I can add the root path to the path as I think. This method allows you to determine the location of the generated log relative to the Web application root by configuring the Log4j.appender.A1.File property in log4j.properties with a relative path freely.



Example: Zxmdailyrollingfileappender.java



Log4j.properties file




Log4j.appender.afile=com.common.config.zxmdailyrollingfileappender
#log4j. Appender.afile=org.apache.log4j.dailyrollingfileappender public class Zxmdailyrollingfileappender extends Dailyrollingfileappender {
public void Setfile (String filetype, Boolean arg2, Boolean arg3, int arg4) throws IOException {
String path = Getservletconfig (). Getservletcontext (). Getrealpath ("/logs/");
String Fuhao = File.separator;
String filepath = path + Fuhao + "log.html";
Super.setfile (filepath, arg2, ARG3, ARG4);
}
}







method Two: Using server environment variables



is to set the log path relative to ${catalina.home} or ${catalina.base}, using environment variables such as ${catalina.home} or ${catalina.base that already exist in the server VM. The log can only be placed in the server subdirectory, and if the other server is used, change the corresponding environment variable. This method platform porting is not convenient.



Example: Log4j.appender.file.file=${catalina.base}/logs/psp_error.log



The catalina.home point to the public information is the parent directory of Bin and Lib.
The catalina.base points to the private information for each Tomcat directory, which is the parent directory of the Conf, logs, temp, webapps, and work.
When you run only one Tomcat instance, the two properties point to the same location.






method Three: load the File property implementation relative path through the servlet initialization init () method



is to extend the Actionservlet class, override its init () method, load the log4j.properties position parameters in the new method, and freely configure the log4j configuration file name and location. You can also freely configure the path of the log4j log file relative to the current application.



Example:



Xml




<!--system initialization configuration information servlet-->
<servlet>
<servlet-name>configServlet</servlet-name>
<servlet-class>com.common.config.ConfigServlet</servlet-class>
<init-param>
<param-name>Log4jFile</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</init-param>
<init-param>
<param-name>Log4jFileSavePath</param-name>
<param-value>/logs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>







Configservlet.java file



public class Configservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
public void init (ServletConfig config) throws servletexception {
Super.init (config);
Initializing the log4j log component
Initlogconfig (config);

}


private void Initlogconfig (ServletConfig config) {
String Prifix = Getservletcontext (). Getrealpath ("/");
System.out.println (Prifix);

Get log4j configuration file address
String log4jfile = Config.getinitparameter ("Log4jfile");
System.out.println (Log4jfile);
String FilePath = Prifix + log4jfile;
System.out.println (FilePath);
Propertyconfigurator.configure (FilePath);
Properties Props = new properties ();
try {
String Log4jfilesavepath = Config.getinitparameter ("Log4jfilesavepath");
System.out.println (Log4jfilesavepath);
FileInputStream Log4jstream = new FileInputStream (FilePath);
Props.load (Log4jstream);
Log4jstream.close ();
Set Log Save Address
String logFile = prifix + log4jfilesavepath + file.separator + "d1cm_log.html";
System.out.println (LogFile);
Props.setproperty ("Log4j.appender.AFile.File", logFile);
Propertyconfigurator.configure (props); Mount log4j configuration information
catch (IOException e) {
E.printstacktrace ();
}
SYSTEM.ERR.PRINTLN ("Initialize log4j log Component");
}
}


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.