Spring MVC整合 Log4j

衣舞晨風發表於2016-08-12

環境及工具:
Log4j 2.6.2
Spring 4.3.2.RELEASE
Maven 3
Tomcat 8(idea中tomcat配,參考:http://blog.csdn.net/jiankunking/article/details/51921092
IntelliJ IDEA 15.0.6(開啟maven自動匯入)

預設情況下,Spring(spring-core)是使用JCL(commons-logging)日誌記錄、JCL執行時能通過發現演算法在專案類路徑下找出其他日誌框架。

為了整合log4j你需要做以下幾個事情:
1、將log4j.jar放到專案類路徑下
2、建立log4j.properties或者 log4j.xml在專案根類路徑下(如果你遵循Maven標準目錄結構,該檔案應該放在resources 資料夾下 )

1. 專案目錄

以下是最終的專案結構:
這裡寫圖片描述

2、專案依賴

    <properties>
        <spring.version>4.3.2.RELEASE</spring.version>
        <log4j.version>2.6.2</log4j.version>
    </properties>

    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

    </dependencies>

3. log4j.properties

建立log4j.properties檔案,並把它放到resources資料夾下,參考上面的專案目錄結構。

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

4. Spring MVC Controller + Message Logging

一個簡單的controller 返回一個welcome頁面,該controller將展示如何用log4j 記日誌。

package com.jiankunking.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController
{

    private static final Logger logger = Logger.getLogger(WelcomeController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getWelcome()
    {
        //logs debug message
        if (logger.isDebugEnabled())
        {
            logger.debug("getWelcome is executed!");
        }

        //logs exception
        logger.error("This is Error message", new Exception("Testing"));

        ModelAndView model = new ModelAndView("welcome");
        model.addObject("msg", "Hello Spring MVC + Log4j");
        return model;
    }
}

5、原始碼下載

http://download.csdn.net/detail/xunzaosiyecao/9602083

本文參考:https://www.mkyong.com/spring-mvc/spring-mvc-log4j-integration-example/#download

作者:jiankunking 出處:http://blog.csdn.net/jiankunking

相關文章