Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Init log4j2 via Spring Log4jConfigurer

Tags:

spring

log4j2

Making migration from log4j 1.2 to new log4j 2. Add to pom:

<!-- Add log4j version 2 with 1.2 API -->
    <dependency>
<!--             <groupId>log4j</groupId> -->
<!--             <artifactId>log4j</artifactId> -->
<!--             <version>1.2.9</version> -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-beta6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.0-beta6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-beta6</version>
    </dependency>

And try to init it as log4j via Spring context (that works perfectly on log4j 1.2). Use for this such config:

<!-- Init log4j with appropriate config according to enviroment -->
<bean id="log4jConfigurer-bean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>classpath:config/env/log4j-${env.variable}.xml</value>
        </list>
    </property>
</bean>

But it seems that now it doesn`t work? What I have done wrong? May be spring config should be modified? Use such dependency for spring:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.3</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>javax.servlet</groupId>
            </exclusion>
        </exclusions>
    </dependency>
like image 603
sphinks Avatar asked Oct 04 '22 01:10

sphinks


1 Answers

Half of the Spring provided Log4j 1.2 support is done, but unfortunately not the bit you're after (for which you'd need to dive into the Log4j2 Configurator class).

For web applications, reading the docs (and code where docs were not enough) at http://logging.apache.org/log4j/2.x/log4j-web/index.html, it seems you can use Log4J2's own configurer which does parameter substitution, and will load from the classpath.

Add the following to your pom.xml:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.0-beta6</version>
</dependency>

And in your web.xml, use the following:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>classloader:/config/env/log4j-${sys:env.variable}.xml</param-value>
</context-param>
<listener>
    <listener-class>org.apache.logging.log4j.core.web.Log4jContextListener</listener-class>
</listener>

Note the / after classloader: and that Log4j2 requires a sys: prefix before the env variable

*However, so far, I've not been able to get this to work on a straight migration from the Spring Log4jContextListener. It seems that Log4j2 has promise, but for now it looks like adoption will remain on a branch until it doesn't have so many gotchas, and has better documentation *

like image 114
NealeU Avatar answered Oct 13 '22 11:10

NealeU