简体   繁体   中英

Integrate Spring with Log4j2

I would like to Spring use Log4j2 for logging because sometimes i would like to get debug level. I've read tons of related topics and examples but noithing is working.. I managed to configure log4j2 and log from my own classes but not from spring. What i got:
Maven dependencies:

    <dependencies>
    <!-- SERVLT API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- APACHE TILES -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- APACHE FILEUPLOAD -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

    <!-- Spring and Transactions -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- Spring ORM support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- SPRING SECURITY -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <!-- LOGGING -->

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${logger.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${logger.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>${logger.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${logger.version}</version>
    </dependency>


    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- POSTGRE DRIVER -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1207.jre7</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- JACKSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.0</version>
    </dependency>

</dependencies>

log4j.xml: (edit: file name is log4j2.xml -sorry for mistake)

<Configuration status="debug">
<Appenders>
    <RollingFile name="systemLog" fileName="C:\\test\logs\system.log" includeLocation="true"
                filePattern="C:\\test\logs\system-%d{MM-dd-yyyy}-%i.log.gz">
        <PatternLayout pattern="%d{ISO8601} - %-5level [%t] %C %M %m%n" />
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="15 MB"/>
        </Policies>
    </RollingFile>
    <Async name="systemAsyncAppender" includeLocation="true">
        <AppenderRef ref="systemLog" />
    </Async>
</Appenders>
<Loggers>
    <Logger name="org.springframework.web" level="debug">
        <AppenderRef ref="systemAsyncAppender" />
    </Logger>
    <Root level="debug" includeLocation="true">
        <AppenderRef ref="systemAsyncAppender" />
    </Root>
</Loggers>

If there is something relevalt that i didnt told - tell me

I will be realy greatfull for helping me out with this

<!-- Log4j Configuration -->
<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod">
        <value>org.springframework.util.Log4jConfigurer.initLogging</value>
    </property>
    <property name="arguments">
        <list>
            <value>${path.to.log4j.file}</value>
        </list>
    </property>
</bean>

Add this in your spring context file

我相信 Spring 使用公共日志记录,因此您需要公共日志记录 jar 和 log4j-jcl jar。

If anyone is using Spring 5 and coming to this post, please note, as stated in the initial Spring 5 documentation (or more briefly explained in the current version ):

  • There is no need for external bridges like JCL-over-SLF4J anymore, and correspondingly no need for a manual exclude of the standard Commons Logging jar from spring-core dependencies.
  • Simply put Log4j 2.x or SLF4J on your classpath, without any extra bridge jars, or rely on default logging through JUL (with a customizable JUL setup).

That is, for Log4j 2 , add its dependency to your pom.xml as follows :

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>
  • As of Spring 5, drop any references to external Commons Logging bridges and also any manual exclude of the standard Commons Logging jar from your existing spring-core dependency setup. Your Log4j or SLF4J or JUL setup will keep working without changes. Note that you may still need a commons-logging exclude for other libraries (eg Apache HttpClient, Castor, HtmlUnit) in order to pick up Spring's JCL bridge instead.

As a reminder of how to use Log4j2 :

In your java code

As per the Apache Log4j 2 documentation :

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
     
    public class HelloWorld {
        private static final Logger logger = LogManager.getLogger("HelloWorld");
        public static void main(String[] args) {
            logger.info("Hello, World!");
        }
    }

Configuration file in /src/main/resources

You will need a log4j2 configuration file, either in xml , json or yaml format.

Details and example in Log4j 2 reference documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM