SpringMVC 专题
您的位置:java > SpringMVC专题 > Spring MVC集成Log4j
Spring MVC集成Log4j
作者:--    发布时间:2019-11-19

以下示例显示如何使用spring web mvc框架集成log4j。首先使用eclipse ide,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:

  1. 创建一个名称为 integratelog4j 的动态web项目。
  2. com.h3.springmvc 包下创建一个java类:hellocontroller
  3. maven存储库页面下载log4库: log4j 。 把它放在classpath中。
  4. src文件夹下创建一个 log4j.properties 文件。
  5. 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。

完整的项目文件目录结构如下所示 -

hellocontroller.java 的代码如下所示 -

package com.h3.springmvc;
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.ui.modelmap;

@controller
@requestmapping("/hello")
public class hellocontroller{
   private static final logger logger = logger.getlogger(hellocontroller.class);
   @requestmapping(method = requestmethod.get)
   public string printhello(modelmap model) {
      logger.info("printhello started.");

      //logs debug message
      if(logger.isdebugenabled()){
         logger.debug("inside:  printhello");
      }
      //logs exception
      logger.error("logging a sample exception", new exception("testing"));

      model.addattribute("message", "hello spring mvc framework!");
      logger.info("printhello ended.");
      return "hello";
   }
}

log4j.properties 的代码如下所示 -

# 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

integratelog4j-servlet.xml 配置如下所示 -

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"   
   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemalocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   <context:component-scan base-package="com.h3.springmvc" />

   <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
      <property name="prefix" value="/web-inf/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

hello.jsp 文件中的配置如下所示 -

<%@ page contenttype="text/html; charset=utf-8" %>
<html>
<head>
<title>hello world</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

在上面的代码中,已经在tomcat控制台中配置了log4j,用它来记录日志详细信息,并且在 tomcat 目录下将日志文件保存为:myapp.log

完成创建源和配置文件后,发布应用程序到tomcat服务器。

现在启动tomcat服务器,当访问url => http://localhost:8080/integratelog4j/hello , 如果spring web应用程序没有问题,应该看到以下结果:


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册