【Spring】Spring在JavaWeb工程中整合log4j

在《【Spring】Spring3.0.5的下载、配置与Helloworld》(点击打开链接)一文各位已经可能看到了。如果Spring不整合log4j直接启动,则会出现如下关于Spring整合log4j的警告。这个挺烦人的,一方面自己提倡高内聚,低耦合,另一方面,自己没有整合log4j就提出警告。我们程序猿写出来的程序就叫做“耦合”,它Spring就叫做“整合”。好吧!你只能同时搞明白,log4j是个什么鬼东西,Spring怎么整合log4j,两个问题:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

由于此前介绍的都是Spring在Java工程中简单示例,log4j必须在JavaWeb工程中运行才有意义,你同时还要搞明白Spring怎么在JavaWeb工程上面运行,Spring怎么在JavaWeb工程中整合log4j。

一、Spring与Log4j的下载

1、你首先要有这两个JavaWeb组件的jar包吧?不然怎么搞出来?Spring在《【Spring】Spring3.0.5的下载、配置与Helloworld》(点击打开链接)一文中已经讲过怎么下载了。Log4j则打开Apache的官网(点击打开链接)如下图,选择log4j-1.2.17.zip(Windows)或者log4j-1.2.17.tar.gz(Linux)。同样是Apache的东西,在一些高版本的Tomcat中还整合了这个东西,当然,你最好还是在WEB-INF\lib目录下补上这个包,以致于你的工程在所有Tomcat都能跑。

2、在Eclipse for JavaEE中新建一个名为SpringLog4j的Dynamic Web Project,还在解压之后,把spring-framework-3.0.5.RELEASE-dependencies的所有东西与spring-framework-3.0.5.RELEASE\dist中的所有Jar包,不包括那个LIBD文件,apache-log4j-1.2.17下的log4j-1.2.17.jar,拷贝到WEB\lib文件夹。

3、之后你的Eclipse for JavaEE如下所示。WEB-INF目录下的Web.xml、applicationContext.xml、log4j.properties与根目录下的Log4j.jsp是一会儿我们要写的东西。

二、Spring与Log4j的配置

1、Web.xml

首先是这个关于JavaWeb的工程的总配置位置。我们要在里面声明要使用Spring与Log4j。值得注意的Log4j的配置必须在Spring配置之前,否则如果先启动Spring,那个必须整合Log4j才不吐警告的Spring,由于Log4j还没有启动,找不到Spring,又会在任性地吐警告。当然,你设置那些什么优先级也行,不过,先启动的直接放前面,这个文件不是更好看吗?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<!-- Log4j配置 -->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- 指定Log4j的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.properties</param-value>
	</context-param>

	<!-- Spring配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

</web-app>  

2、log4j.properties

这家伙的后缀名是这么长,你有什么办法?必须照打,都是Linux那边带过来的主,你看看人家Windows的配置文件的后缀名仅仅就是ini3个字母!

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...
log4j.rootLogger = all, console, R

#Console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

#File
log4j.appender.R = org.apache.log4j.RollingFileAppender
log4j.appender.R.File = c:/log.txt
log4j.appender.R.MaxFileSize = 500KB
log4j.appender.R.MaxBackupIndex = 1
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] - %m%n

这东西还加不了中文注释,给大家一行一行地讲。#后面还仅能写英文的东西,把我大天朝的程序猿放哪里?我就加个中文注释都不让!

首先第一部分是log4j的总配置部分,all代表debug,info,error,fatal四种类型的信息都会输出。一般不设置为all,这里只是为了让大家看到效果。因为那些debug,info信息对我们半点意义没有,还因为有很多系统内部的文件运行都会输出debug与info信息刷屏、刷版。关键是输出到磁盘的日志文件会极速递增,浪费磁盘空间。玩SQL Server的时候大家又不知道那个.ldb是多么恐怖?

因此第一部分,一般写成:

log4j.rootLogger = ERROR, console, R

代表仅输出error与fatal错误。

之后的console,R分别代表在控制台与文件输出。同时在之后的代码必须配置好这个两输出。

第二部分控制台#Console

首先要使用log4j特定的包,这个没有什么好说,最后一句指明输出格式。一会儿大招对照输出结果就明白怎么回事了。

第三部分文件#File

log4j.appender.R.File=c:/log.txt是指这个Web工程错误日志皆输出到c:/log.txt。不要像网上那些大神输出一个什么.log后缀,关键是能够直接打开。

之后log4j.appender.R.MaxFileSize = 500KB指明这个log.txt文件大小最多为500KB,如果超过这个大小,自动开一个新文件,而log4j.appender.R.MaxBackupIndex=1指明此工程顶多只能有1个这个的日志文件。多了的话,新内容覆盖旧内容,就像那些闭路电视摄像头一样。

Log4j到这里就搞完了。

3、applicationContext.xml

之后是Spring的部分,由于这次根本就没有用Spring做任何东西,因此,这个applicationContext.xml这样写就行了:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>

能删的我都删了。

4、Log4j.jsp

最后是在WebContent根目录下的Log4j.jsp。在里面写入如下代码,整个程序的入口就是这个地方。

Servlet我都不搞了,就是为了让大家直接看清楚问题的关键所在。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="org.apache.log4j.Logger"%>
<%
Logger.getLogger(this.getClass()).fatal("致命错误!");
Logger.getLogger(this.getClass()).error("出错信息!");
Logger.getLogger(this.getClass()).info("普通信息!");
Logger.getLogger(this.getClass()).debug("调试信息!");
%>

三、运行结果

把SpringLog4j这个JavaWeb工程挂到Tomcat里面,运行,之后在任意浏览器输入http://localhost:8080/SpringLog4j/Log4j.jsp,待网页成功加载之后,直接回到Eclipse则得到如下的运行结果,你输入一次网址刷新一次,则输出一次以下结果:

同时,你的C盘,则多出一个log.txt,里面的内容如下,可以看到,在程序运行的时候,Spring的运行会吐出很多无意义的DEBUG、INFO、TRACE的信息,对于我们来说,真正有用的最后面的四句话。因此,可以理解,为何在上面的log4j.properties(这个后缀名又难记,又长,再次鄙视之!)的第一部分一般不写all,只写ERROR,这就只输出ERROR以上等级的错误,也就是ERROR与FATAL。或者写成TRACE,输出TRACE、ERROR与FATAL信息。

2015-05-10 09:54:34 [org.springframework.web.context.ContextLoader]-[INFO] - Root WebApplicationContext: initialization started
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[INFO] - Refreshing Root WebApplicationContext: startup date [Sun May 10 09:54:34 CST 2015]; root of context hierarchy
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.DefaultDocumentLoader]-[DEBUG] - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[TRACE] - Trying to resolve XML entity with public id [null] and system id [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd]
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] - Loading schema mappings from [META-INF/spring.schemas]
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] - Loaded schema mappings: {http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-3.0.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader]-[DEBUG] - Loading bean definitions
2015-05-10 09:54:34 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[DEBUG] - Loaded 0 bean definitions from location pattern [/WEB-INF/applicationContext.xml]
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[DEBUG] - Bean factory for Root WebApplicationContext: org.s[email protected]5eedf162: defining beans []; root of factory hierarchy
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[DEBUG] - Unable to locate MessageSource with name 'messageSource': using default [[email protected]0824]
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[DEBUG] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.[email protected]708a538f]
2015-05-10 09:54:34 [org.springframework.ui.context.support.UiApplicationContextUtils]-[DEBUG] - Unable to locate ThemeSource with name 'themeSource': using default [o[email protected]1fce66ba]
2015-05-10 09:54:34 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] - Pre-instantiating singletons in org.s[email protected]5eedf162: defining beans []; root of factory hierarchy
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[DEBUG] - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [[email protected]bc2c83]
2015-05-10 09:54:34 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] - Returning cached instance of singleton bean 'lifecycleProcessor'
2015-05-10 09:54:34 [org.springframework.web.context.support.XmlWebApplicationContext]-[TRACE] - Publishing event in Root WebApplicationContext: org.springframework.context.event.ContextRefreshedEvent[source=Root WebApplicationContext: startup date [Sun May 10 09:54:34 CST 2015]; root of context hierarchy]
2015-05-10 09:54:34 [org.springframework.web.context.ContextLoader]-[DEBUG] - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2015-05-10 09:54:34 [org.springframework.web.context.ContextLoader]-[INFO] - Root WebApplicationContext: initialization completed in 230 ms
2015-05-10 09:54:48 [org.apache.jsp.Log4j_jsp]-[FATAL] - 致命错误!
2015-05-10 09:54:48 [org.apache.jsp.Log4j_jsp]-[ERROR] - 出错信息!
2015-05-10 09:54:48 [org.apache.jsp.Log4j_jsp]-[INFO] - 普通信息!
2015-05-10 09:54:48 [org.apache.jsp.Log4j_jsp]-[DEBUG] - 调试信息!

四、总结

Log4j一般不会像这样直接写在log4j.jsp中。一般处于那些try-catch异常结构中的catch里面,或许一些操作文件、数据库的关键数据之间,直接给程序员看的。程序猿有空看看那个在log.txt,也就像保安有空看看闭路电视,来确定你的WEB工程到底正不正常。可以成为系统运维的一部分。

时间: 2024-12-19 21:41:43

【Spring】Spring在JavaWeb工程中整合log4j的相关文章

玩转Spring MVC(五)----在spring中整合log4j

在前边的基础上,本文主要总结一下如何在spring 中配置log4j,在本文末尾会给出完整项目的链接. 首先是web.xml中要新添加的代码: <!-- 6. 配置log4j --> <!--6.1 配置加载log4j.xml文件路径 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/spring

JavaWeb应用中初始化Log4j的两种方式

本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletContextListener 监听 ServletContext 的初始化事件来初始化 Log4j . 先来看下方式一,直接上代码: web.xml 编写如下: 1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 2 xmlns

关于Spring JavaWeb工程中的ContextRefreshedEvent事件

在应用启动时,通常想在此时预加载一些资源,全局使用. Spring会在操作应用上下文时,使用ApplicationEventPublisher触发相关ApplicationContextEvent,我们可以监听这些事件来做一些事情. Spring中ApplicationContextEvent有以下几种: 其中ContextRefreshedEvent的执行时机为: 1 Event raised when an {@code ApplicationContext} gets initialize

Javaweb 工程中 监听器 listener 讲解

监听器用于监听web应用中某些对象.信息的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等等. 分类: 按监听的对象划分,可以分为 ServletContext对象监听器 HttpSession对象监听器 ServletRequest对象监听器 按监听的事件划分 对象自身的创建和销毁的监听器 对象中属性的创建和消除的监听器 session中

Spring整合log4j日志组件

PHP转Java有一段时间了,最近在学习Spring的一些组件安装.配置及使用.今天学习了log4j作为项目日志操作组件为web开发节省了大量在项目过程中记录日志及日志输送存储的工作. Log4j是Apache的一个开放源代码项目,通过使用Log4j,控制日志信息输送的目的地可以为控制台.文件.数据库.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等:可以控制每一条日志的信息内容和信息输出格式:通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程:

Spring整合log4j日志组件(转)

Log4j是Apache的一个开放源代码项目,通过使用Log4j,控制日志信息输送的目的地可以为控制台.文件.数据库.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等:可以控制每一条日志的信息内容和信息输出格式:通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程:甚至还可以在不需要修改业务逻辑代码.重启web服务,只需要通过一个修改配置文件就可以实现控制项目的日志动作. 首先,日志的级别有:OFF .FATAL .ERROR.WARN.INFO.

如何在Spring MVC工程中进行单元测试

直接以代码方式演示如何在Spring MVC工程中进行单元测试: package net.chinaedu.projects.dubhe; import java.util.List; import net.chinaedu.projects.dubhe.publisher.IPublisherService; import net.chinaedu.projects.venus.domain.Publisher; import org.junit.Test; import org.junit.

log4j在javaWeb项目中的使用

在前边的文章中对log4j的配置文件进行了说明,今天介绍如何在普通的javaWeb项目中使用log4j. 在日常的开发过程中,日志使用的很频繁,我们可以利用日志来跟踪程序的错误,程序运行时的输出参数等,很多情况下可能会使用System.out.println()这个方法,但是还有一种更加简洁的方式,那就是使用日志框架,今天就看看log4j这个日志框架如何在javaWeb的类中使用. 一.log4j的配置文件 我们要使用log4j必须要有log4j的配置文件,前面一篇文章提到,log4j的配置文件

如何为Eclipse开发工具中创建的JavaWeb工程创建Servlet

在博客<在Eclipse中如何创建JavaWeb工程>中图文并茂的说明了Eclipse中创建JavaWeb工程的方法,本篇博客将告诉大家如何为Eclipse开发工具中创建的JavaWeb工程创建Servlet: 1.在Eclipse开发工具中创建的JavaWeb工程文件目录结构如下图: 说明: a).红框框定的结构用于存放Java类及其相应的包:用于存放Libraries文件夹: b).灰框框定的结构用于显示(非"存放")JavaWeb工程所依赖的JDK相关的jar包: c