1. 首页
  2. Spring教程

Spring教程45篇:Spring 使用 Log4J 记录日志

使用 Log4J 记录日志

在 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 使用AddExternalJARs选项,添加所需的Spring库,解释见SpringHelloWorldExample章节。
3 使用AddExternalJARs选项,同样在你的项目中添加log4j库log4j-x.y.z.jar。
4 在com.tutorialspoint包下创建Java类HelloWorld和MainApp。
5 在src文件中创建Bean配置文件Beans.xml。
6 在src文件中创建log4J配置文件log4j.properties。
7 最后一步是创建的所有Java文件和Bean配置文件的内容,并运行应用程序,解释如下所示。

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

  package com.tutorialspoint;
    public class HelloWorld {
       private String message;
       public void setMessage(String message){
          this.message  = message;
       }
       public void getMessage(){
          System.out.println("Your Message : " + message);
       }
    }

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

  package com.tutorialspoint;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.apache.log4j.Logger;
    public class MainApp {
       static Logger log = Logger.getLogger(MainApp.class.getName());
       public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
          log.info("Going to create HelloWord Obj");
          HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
          obj.getMessage();
          log.info("Exiting the program");
       }
    }

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

  <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

       <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
           <property name="message" value="Hello World!"/>
       </bean>

    </beans>

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

  # Define the root logger with appender file
    log4j.rootLogger = DEBUG, FILE

    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.FileAppender
    # Set the name of the file
    log4j.appender.FILE.File=C:\\log.out

    # Set the immediate flush to true (default)
    log4j.appender.FILE.ImmediateFlush=true

    # Set the threshold to debug mode
    log4j.appender.FILE.Threshold=debug

    # Set the append to false, overwrite
    log4j.appender.FILE.Append=false

    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.conversionPattern=%m%n

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

  Your Message : Hello World!

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

  <!-- initialization log messages -->

    Going to create HelloWord Obj
    Returning cached instance of singleton bean 'helloWorld'
    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 的替换:

  package com.tutorialspoint;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.apache.commons.logging. Log;
    import org.apache.commons.logging. LogFactory;
    public class MainApp {
       static Log log = LogFactory.getLog(MainApp.class.getName());
       public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
          log.info("Going to create HelloWord Obj");
          HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
          obj.getMessage();
          log.info("Exiting the program");
       }
    }

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

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

作者:陈

来源:https://www.w3cschool.cn/wkspring/47o21icw.html


看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「方志朋」,公众号后台回复「666」 免费领取我精心整理的进阶资源教程
  4. JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

    本文著作权归作者所有,如若转载,请注明出处

    转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

    标题:Spring教程45篇:Spring 使用 Log4J 记录日志

    链接:https://www.javajike.com/article/2016.html

« Spring教程25篇:Spring JSR-250 注释
Spring教程44篇:Spring 异常处理例子»

相关推荐

QR code