您的位置:首页 > 编程语言 > Java开发

Spring中commons-logging.jar的作用

2019-04-23 21:37 99 查看

commons-logging.jar包是使用spring的必备包。
(位置在spring-framework-2.5.6.SEC01\lib \jakarta-commons)用来记录程序运行时的活动的日志记录。

在java平台中,有几个可以选择的日志记录的实现(log4j,JDK Logging API)。

如果你希望你的日志记录与实现无关,你可以使用Apache Commons Logging,它使用抽象的API,这些API都是实现无关的,可以让你在不同的日志记录实现之间切换,而不需要修改你的代码。

只要你使用了Apache Commons Logging,你可以在Apache Commons Logging支持的日志记录实现任意选择(目前主要支持log4j和JDK Logging API),如果使用log4j,拷贝spring-framework-2.5.6.SEC01\lib\log4j下的包到程序里,一旦程序中检测 log4j在classpath,Commons Logging将会使用log4j作为底层实现。

你可以可以通过配置在classpath根目录下log4j.properties来配置Log4j。下面log4j配置文件定义了一个日志输出器 (log appender)叫做stdout,可以用来输出日志信息到控制台上。想了解更多的Log4J的信息。请参考Log4J

direct log messages to stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=
%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n

set root logger level

log4j.rootLogger=error, stdout

set application logger level

log4j.logger.com.apress.springrecipes.calculator=info

Log4J支持根据紧急度从高到低分为6个日志等级-fatal, error, warn, info, debug, 和trace。在上面的配置文件中,程序的根日志级别是error,这就意味着只有error和fatal级别的日志才会默认输出。但是对于 com.apress.springrecipes.calculator包和它的子包,只要日志级别高于info才会输出。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: