课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 使用 Log4J

当前位置:免费教程 » Java相关 » Spring

在 Spring 应用程序中使用 Log4J 的功能是非常容易的。下面的例子将带你通过简单的步骤解释 Log4J 和 Spring 之间的简单集成。

假设你已经在你的机器上安装了 Log4J,如果你还没有 Log4J,你可以从 http://logging.apache.org/ 中下载,并且仅仅在任何文件夹中提取压缩文件。在我们的项目中,我们将只使用 log4j-x.y.z.jar

接下来,我们让 Eclipse IDE 在恰当的位置工作,遵循以下步骤,使用 Spring Web 框架开发一个基于 Web 应用程序的动态表单:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint
2使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。
3使用 Add External JARs 选项,同样在你的项目中添加 log4j 库 log4j-x.y.z.jar
4com.tutorialspoint 包下创建 Java 类 HelloWorldMainApp
5src 文件中创建 Bean 配置文件 Beans.xml
6src 文件中创建 log4J 配置文件 log4j.properties
7最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。

这个是 HelloWorld.java 文件的内容:

  1. package com.tutorialspoint;
  2. public class HelloWorld {
  3. private String message;
  4. public void setMessage(String message){
  5. this.message = message;
  6. }
  7. public void getMessage(){
  8. System.out.println("Your Message : " + message);
  9. }
  10. }

下面的是第二个文件 MainApp.java 的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.apache.log4j.Logger;
  5. public class MainApp {
  6. static Logger log = Logger.getLogger(MainApp.class.getName());
  7. public static void main(String[] args) {
  8. ApplicationContext context =
  9. new ClassPathXmlApplicationContext("Beans.xml");
  10. log.info("Going to create HelloWord Obj");
  11. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  12. obj.getMessage();
  13. log.info("Exiting the program");
  14. }
  15. }

使用与我们已经生成信息消息类似的方法,你可以生成调试错误消息。现在让我们看看 Beans.xml 文件的内容:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
  7. <property name="message" value="Hello World!"/>
  8. </bean>
  9. </beans>

下面是 log4j.properties 的内容,它定义了使用 Log4J 生成日志信息所需的标准规则:

  1. # Define the root logger with appender file
  2. log4j.rootLogger = DEBUG, FILE
  3. # Define the file appender
  4. log4j.appender.FILE=org.apache.log4j.FileAppender
  5. # Set the name of the file
  6. log4j.appender.FILE.File=C:\\log.out
  7. # Set the immediate flush to true (default)
  8. log4j.appender.FILE.ImmediateFlush=true
  9. # Set the threshold to debug mode
  10. log4j.appender.FILE.Threshold=debug
  11. # Set the append to false, overwrite
  12. log4j.appender.FILE.Append=false
  13. # Define the layout for file appender
  14. log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
  15. log4j.appender.FILE.layout.conversionPattern=%m%n

一旦你完成了创建源和 bean 的配置文件后,我们就可以运行该应用程序。如果你的应用程序一切都正常,在 Eclipse 控制台将输出以下信息:

  1. Your Message : Hello World!

同时如果你检查你的 C:\ 驱动,那么你应该发现含有各种日志消息的日志文件 log.out,其中一些如下所示:

  1. <!-- initialization log messages -->
  2. Going to create HelloWord Obj
  3. Returning cached instance of singleton bean 'helloWorld'
  4. Exiting the program

Jakarta Commons Logging (JCL) API

或者,你可以使用 Jakarta Commons Logging(JCL) API 在你的 Spring 应用程序中生成日志。JCL 可以从 http://jakarta.apache.org/commons/logging/ 下载。我们在技术上需要这个包的唯一文件是 commons-logging-x.y.z.jar 文件,需要使用与上面的例子中你使用 log4j-x.y.z.jar 类似的方法来把 commons-logging-x.y.z.jar 放在你的类路径中。

为了使用日志功能,你需要一个 org.apache.commons.logging.Log 对象,然后你可以根据你的需要调用任何一个下面的方法:

  • fatal(Object message)

  • error(Object message)

  • warn(Object message)

  • info(Object message)

  • debug(Object message)

  • trace(Object message)

下面是使用 JCL API 对 MainApp.java 的替换:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.apache.commons.logging. Log;
  5. import org.apache.commons.logging. LogFactory;
  6. public class MainApp {
  7. static Log log = LogFactory.getLog(MainApp.class.getName());
  8. public static void main(String[] args) {
  9. ApplicationContext context =
  10. new ClassPathXmlApplicationContext("Beans.xml");
  11. log.info("Going to create HelloWord Obj");
  12. HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  13. obj.getMessage();
  14. log.info("Exiting the program");
  15. }
  16. }

你应该确保在编译和运行该程序之前在你的项目中已经引入了 commons-logging-x.y.z.jar 文件。

现在保持在上面的例子中剩下的配置和内容不变,如果你编译并运行你的应用程序,你就会得到与使用 Log4J API 后获得的结果类似的结果。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号