javascript
Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)
以下內(nèi)容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_log4j.htm
說明:示例基于Spring MVC?4.1.6。
以下示例說明如何使用Spring Web MVC框架來觸發(fā)LOG4J。首先,讓我們使用Eclipse IDE,并按照以下步驟使用Spring Web Framework開發(fā)基于動態(tài)窗體的Web應(yīng)用程序:
| 1 | 創(chuàng)建一個名為TestWeb的項目,在一個包com.tutorialspoint下,如Spring MVC - Hello World Example章節(jié)所述。 |
| 2 | 在com.tutorialspoint包下創(chuàng)建一個Java類HelloController。 |
| 3 | 從maven存儲庫頁面下載log4j庫LOG4J。把它放在你的CLASSPATH中。 |
| 4 | 在src文件夾下創(chuàng)建一個log4j.properties?。 |
| 5 | 最后一步是創(chuàng)建所有源和配置文件的內(nèi)容并導(dǎo)出應(yīng)用程序,如下所述。 |
HelloController.java
package com.tutorialspoint;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 messageif(LOGGER.isDebugEnabled()){LOGGER.debug("Inside: printHello");}//logs exceptionLOGGER.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%nTestWeb-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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="com.tutorialspoint" /><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>這里我們配置了log4j來記錄tomcat控制臺上的詳細(xì)信息,并在&t?tomcat home> logs as myapp.log。?
完成創(chuàng)建源文件和配置文件后,導(dǎo)出應(yīng)用程序。右鍵單擊應(yīng)用程序并使用Export > WAR File選項,并將您的TestWeb.war文件保存在Tomcat的webapps文件夾中。
現(xiàn)在啟動您的Tomcat服務(wù)器,并確保您可以使用標(biāo)準(zhǔn)瀏覽器從webapps文件夾訪問其他網(wǎng)頁?,F(xiàn)在嘗試URL?http://localhost:8080/TestWeb/hello,您應(yīng)該在Tomcat的日志中看到以下結(jié)果。
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test32
轉(zhuǎn)載于:https://www.cnblogs.com/EasonJim/p/7500456.html
總結(jié)
以上是生活随笔為你收集整理的Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: canvas 图片反色
- 下一篇: Spring-framework应用程序