spring mvc 配置 log4j
2016-09-12 18:52
141 查看
spring mvc 配置 log4j
spring mvc 配置 log4j 注意事项:1). 如果 log4j.properties 在 src 目录,则不需要配置 Log4jConfigListener。
2). 如果 log4j.properties 不在 src 目录下,则需要配置 Log4jConfigListener。
具体代码如下:
1). controller 层代码如下:
package com.test.controller;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping(“/hello”)
@Controller
public class Log4jController {
private static Logger logger = Logger.getLogger(Log4jController.class); @RequestMapping("/hello") public ModelAndView log4j(HttpServletRequest request){ ModelAndView model = new ModelAndView("hello"); model.addObject("msg", "this is log4j record..."); logger.info("my first log for java ..."); return model; }
}
log4j.properties 内容如下:
# Set root logger level to DEBUG and its appender to A1 and file. log4j.rootLogger=DEBUG, file, A1 log4j.category.org.springframework = warn # file is set to a DailyRollingFileAppender log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=test.log log4j.appender.file.Append=true log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[test] [%d{yyyy-mm-dd HH:MM:ss}] [%-5p] %l - %m%n # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=[test] [%d{yyyy-mm-dd HH:MM:ss}] [%-5p] %l - %m%n
web.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd"> <servlet> <servlet-name>spring-web</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-web</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- if log4j.properties in src dir, Log4jConfigListener don't need to config. otherwise, if log4j.properties don't in src dir, Log4jConfigListener must be config. --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/ui/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
applicationcontext.xml 内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <context:component-scan base-package="com.test.controller"></context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/ui/" /> <property name="suffix" value = ".jsp" /> <property name="order" value = "1" /> </bean> </beans>
pom.xml 文件如下:
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
jsp 页面如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < ba3b span class="hljs-tag"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <div>tips:${msg}</div> </body> </html>
相关文章推荐
- 项目生产环境下 log4j 和spring MVC视图解析viewResolvers的配置
- 完美的Log4j在Spring MVC+iBATIS配置
- Spring MVC中log4j.properties简单配置
- Spring MVC代码实例系列-05:Spring MVC配置Log4j以及 log4j.properties 属性详解介绍
- java学习笔记9 - Spring mvc中log4j的配置与使用
- spring mvc 小记(二):关于log4j的配置
- spring mvc中log4j的配置与使用
- 项目生产环境下 log4j 和spring MVC视图解析viewResolvers的配置
- spring mvc log4j 配置
- spring mvc 异常处理和Log4j配置
- Spring MVC 项目中配置log4j 出现错误log4j:ERROR Failed to excute sql com.mysql.jdbc.exceptions.jdbc4.MySQLSynt
- Spring MVC - log4j 配置
- spring mvc 异常处理和Log4j配置
- spring mvc学习(三)之log4j.properties和hibernate.properties的配置
- spring mvc中log4j的配置与使用
- Spring MVC 项目中配置log4j 出现错误log4j:ERROR Failed to excute sql
- spring mvc中log4j的配置与使用
- spring mvc中log4j的配置与使用
- Spring mvc中log4j的配置与使用
- 项目生产环境下 log4j 和spring MVC视图解析viewResolvers的配置