Place the log4j configuration file outside the project

Source: Internet
Author: User

Why the profile is placed outside the project
在项目开发阶段,我们使用的是测试区的数据库,FTP 等等 资源。在项目上线时,自然要切换到正式区。 如果配置文件写在项目里面,那么发布时就要变更代码,修改链接地址。所以讲配置文件放在项目之外,就可以解决这个问题了。
Why to dynamically configure the path of log4j.properties
因为目前我的项目要给多个外包厂用,部署到多个服务器上。 因外包厂服务器安装目录不同,各自的命令规范也无法统一。

Therefore, the client needs to make the log path location.

Basic ideas
* 在tomcat的context.xml中 新增一个变量,维护log4j的位置.* 在项目启动加载时,根据路径读取log4j的文件,重新定义log4j配置文件.
Configuring Tomcat's Context.xml
    • Context.xml New Line

<br /><Environment name="myLog4jPath" override="false" type="java.lang.String" value="/opt/conf/log4j.properties"/> <br />

Create a MAVEN project
    • Pom.xml

      Note: log4j if 1.2.17 or above, otherwise not supported
      <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <dependency>        <groupId>log4j</groupId>        <artifactId>log4j</artifactId>        <version>1.2.17</version>    </dependency>    <dependency>        <groupId>javax.servlet</groupId>        <artifactId>servlet-api</artifactId>        <version>2.5</version>        <scope>provided</scope>    </dependency></dependencies><build>    <finalName>MyLogger</finalName>  </build>
New servlet
Package Com;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.io.inputstream;import Java.io.printwriter;import Javax.naming.context;import Javax.naming.InitialContext; Import Javax.naming.namingexception;import Javax.servlet.servletexception;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.log4j.logger;import Org.apache.log4j.propertyconfigurator;public class Myloggerservlet extends HttpServlet {private static final long Serialversionuid = 1l;private Logger Logger = Logger.getlogger (myloggerservlet.cla    SS);/** * @see httpservlet#httpservlet () */public Myloggerservlet () {Context initctx = null;    String config = null;    InputStream is = null;    Context envctx = null;        try {initctx = new InitialContext ();        Envctx = (Context) initctx.lookup ("java:comp/env");        Config = (String) envctx.lookup ("Mylog4jpath"); is = new FileInputStream (The new File (config));        Propertyconfigurator.configure (IS);    Logger.info ("========================config file path=============" + config);    } catch (Exception ex) {Logger.error (ex.tostring ());            } finally {if (envctx! = null) {try {envctx.close ();            } catch (Namingexception e) {e.printstacktrace ();            }} if (Initctx! = null) {try {initctx.close ();            } catch (Namingexception e) {e.printstacktrace (); }}}}/** * @see httpservlet#doget (httpservletrequest request, HttpServletResponse * response) */protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {prin    Twriter pw = Response.getwriter ();    Logger.info ("Hello servlet, Hello log4j");    Pw.write ("Okok"); Pw.flush ();} /** * @see Httpservlet#dopost (httpservletrEquest request, HttpServletResponse * response) */protected void DoPost (HttpServletRequest request, Httpservle Tresponse response) throws Servletexception, IOException {doget (request, response);}

}

`

Xml
<servlet>    <servlet-name>MyLoggerServlet</servlet-name>    <display-name>MyLoggerServlet</display-name>    <description></description>    <servlet-class>com.MyLoggerServlet</servlet-class></servlet><servlet-mapping>    <servlet-name>MyLoggerServlet</servlet-name>    <url-pattern>/MyLoggerServlet</url-pattern></servlet-mapping>
Test

The servlet loads the/opt/conf/log4j.properties file at the first call and prints the log to the specified directory
Http://localhost:8080/MyLogger/MyLoggerServlet

Place the log4j configuration file outside the project

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.