Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring 使用 Log4J 记录日志
Spring 使用 Log4J 记录日志
作者:--    发布时间:2019-11-20

在 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 文件的内容:

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 后获得的结果类似的结果。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册