log4j2在SpringMVC工程中的运用

阅读更多

        SpringMVC返回json数据的三种方式文件可知,我的SpringMVC工程已支持返回json数据,下面我们再给它增加日志输出功能。

一.引入日志相关的jar包

log4j2在SpringMVC工程中的运用_第1张图片
        不知为什么,经不是特别全面的验证,这七个jar包必须都引用到我的SpringMVC的工程中来。

 

二.增加log4j2.xml配置

  
  
  
      
      
          
          
              
              
              
              
          
          
          
              
          
  
          
          
              
              
          
      
      
    
    	
    		
    		  
            
    	
          
          
              
              
              
          
    

 

三.修改代码验证

HelloController.java

package com.bijian.study.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bijian.study.service.HelloService;

@Controller
public class HelloController {

    private static Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private HelloService helloService;

    @RequestMapping("/greeting")
    public ModelAndView greeting(@RequestParam(value = "name", defaultValue = "World") String name) {

        Map map = new HashMap();
        try {
            //由于浏览器会把中文直接换成ISO-8859-1编码格式,如果用户在地址打入中文,需要进行如下转换处理
            String tempName = new String(name.getBytes("ISO-8859-1"), "utf-8");

            logger.trace("tempName:" + tempName);
            logger.info(tempName);

            String userName = helloService.processService(tempName);

            map.put("userName", userName);
            
            logger.trace("运行结果:" + map);
        } catch (UnsupportedEncodingException e) {
            logger.error("HelloController greeting方法发生UnsupportedEncodingException异常:" + e);
        } catch (Exception e) {
            logger.error("HelloController greeting方法发生Exception异常:" + e);
        }
        return new ModelAndView("/hello", map);
    }

    @ResponseBody
    @RequestMapping("/greet")
    public Map greet(HttpServletResponse response,
            @RequestParam(value = "name", defaultValue = "World") String name) {

        Map map = null;
        try {
            //由于浏览器会把中文直接换成ISO-8859-1编码格式,如果用户在地址打入中文,需要进行如下转换处理
            String tempName = new String(name.getBytes("ISO-8859-1"), "utf-8");
            
            logger.trace("tempName:" + tempName);
            logger.info(tempName);

            String userName = helloService.processService(tempName);

            map = new HashMap();
            map.put("userName", userName);
            
            logger.trace("运行结果:" + map);
        } catch (UnsupportedEncodingException e) {
            logger.error("HelloController greet方法发生UnsupportedEncodingException异常:" + e);
        } catch (Exception e) {
            logger.error("HelloController greet方法发生Exception异常:" + e);
        }
        return map;
    }
}

HelloService.java

package com.bijian.study.service;

public interface HelloService {

    public String processService(String name) throws Exception;
}

HelloServiceImpl.java

package com.bijian.study.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.bijian.study.service.HelloService;

@Service("helloService")
public class HelloServiceImpl implements HelloService {

    private static Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
    
    @Override
    public String processService(String name) throws Exception {
        
        logger.info("HelloService processService name:" + name);
        return "Hello " + name;
    }
}

        启动Tomcat,浏览器中输入http://localhost:8080/SpringMVC/greet?name=lisi,运行结果如下:


        控制台输出:

16:08:34.120 TRACE com.bijian.study.controller.HelloController 62 greet - tempName:lisi
16:08:34.125 INFO  com.bijian.study.controller.HelloController 63 greet - lisi
16:08:34.125 INFO  com.bijian.study.service.impl.HelloServiceImpl 17 processService - HelloService processService name:lisi
16:08:34.126 TRACE com.bijian.study.controller.HelloController 70 greet - 运行结果:{userName=Hello lisi}

        app.log输出:

2016-06-26 at 16:08:34 CST TRACE com.bijian.study.controller.HelloController 62 greet - tempName:lisi
2016-06-26 at 16:08:34 CST INFO  com.bijian.study.controller.HelloController 63 greet - lisi
2016-06-26 at 16:08:34 CST INFO  com.bijian.study.service.impl.HelloServiceImpl 17 processService - HelloService processService name:lisi
2016-06-26 at 16:08:34 CST TRACE com.bijian.study.controller.HelloController 70 greet - 运行结果:{userName=Hello lisi}

 

        整个工程见附件《SpringMVC.zip》。

  • log4j2在SpringMVC工程中的运用_第2张图片
  • 大小: 14.3 KB
  • log4j2在SpringMVC工程中的运用_第3张图片
  • 大小: 7.4 KB
  • SpringMVC.zip (9 MB)
  • 下载次数: 123
  • 查看图片附件

你可能感兴趣的:(log4j2,json,spring)