日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)

發(fā)布時間:2023/12/1 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC-集成(Integration)-集成LOG4J示例(转载实践) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

以下內(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%n

TestWeb-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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。