Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define variables in spring's xmls to use in log4j.properties

Kind of a puzzle here.

I have an application in WAR. There are web.xml and application context.xml in there, and also there is log4j.properties. This WAR runs in tomcat.

There is a possibility to use some variables in log4j.properties, e.g. log4j.appender.file.File=${catalina.base}/logs/app.log

I want to define a variable in web.xml or context.xml and use it in log4j.properties. For example, somehow set version=1.1 and use log4j.appender.file.File=${catalina.base}/logs/app-${version}.log. It should not be an environment variable.

Can I do it without recompiling the app?

ADD shouldn't affect anything, but just in case...

Its web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee">

<!-- Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:context.xml</param-value>
</context-param>

<!-- log4j -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>10000</param-value>
</context-param>
<context-param>
    <param-name>log4jExposeWebAppRoot</param-name>
    <param-value>false</param-value>
</context-param>
...
</web-app>
like image 757
Andrey Regentov Avatar asked Jul 28 '15 03:07

Andrey Regentov


People also ask

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

Where should log4j2 xml be placed?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.


1 Answers

Make a subclass of org.springframework.web.util.Log4jConfigListenerthat expose version value as System variable (and of course you can pass the value via standard System/Java environment variable).

public class TestListener extends Log4jConfigListener{

    @Override
    public void contextInitialized(ServletContextEvent pEvent) {
        String value = pEvent.getServletContext().getInitParameter("version");
        String version = System.getProperty("version");
        if (version == null || version.trim().length()==0) System.setProperty("version", value);

        super.contextInitialized(pEvent);
    }
}

And fix your web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee">

    <!-- Spring -->
    ...
    <!-- log4j -->
    <listener>
        <listener-class>com.TestListener</listener-class>
    </listener>
    <context-param>
        <param-name>version</param-name>
        <param-value>1.1</param-value>
    </context-param>
    ...
like image 138
FoxyBOA Avatar answered Sep 30 '22 02:09

FoxyBOA