java 如何去掉http debug日志_Spring 使用 Log4J 记录日志

478b69bec27583832964198fed03fa2c.png

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

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

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

b6ecadd0439236e22494ade5d978f20b.png

这个是 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"?>

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

# Define the root logger with appender filelog4j.rootLogger = DEBUG, FILE# Define the file appenderlog4j.appender.FILE=org.apache.log4j.FileAppender# Set the name of the filelog4j.appender.FILE.File=C:log.out# Set the immediate flush to true (default)log4j.appender.FILE.ImmediateFlush=true# Set the threshold to debug modelog4j.appender.FILE.Threshold=debug# Set the append to false, overwritelog4j.appender.FILE.Append=false# Define the layout for file appenderlog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n

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

Your Message : Hello World!

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

Going to create HelloWord ObjReturning 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 后获得的结果类似的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值