一千萬個為什麽

搜索

如何使用Apache Commons記錄Digester?

我無法使用Apache Commons Digester 來記錄任何內容。我會非常感激任何人都可以流下的光。

在我的代碼中,我這樣做:

Digester digester = new Digester();
// some Digester set up stuff

// What on earth should go in here????
digester.setLogger(LogFactory.getLog("org.apache.commons.logging.Log"));

我在類路徑中有一個 commons-logging.properties 文件,如下所示:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester=debug
org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester.sax=info

我根本沒有調試信息。

謝謝你的幫助!

Update: Thanks for the answer bwawok - that's what the problem was. In the docs for Digester, they suggest that you can just enable the SimpleLog of commons-logging. Unfortunately, the Digester doesn't appear to output any INFO messages, only DEBUG, and at least on eclipse, SimpleLog doesn't output DEBUG messages at all! The result was no INFO messages (because Digester sends none) and no DEBUG messages (because SimpleLog doesn't forward them!) Once I switched to log4j, all the debug messages came spewing out! Thanks again.

最佳答案

我建議你不要使用公共記錄。它有一些問題,比如不能很好地使用Tomcat 5.5及更高版本。

如果您正在編寫自己的應用程序(而不是庫),我建議您直接編寫Log4j代碼。閱讀 http://logging.apache.org/log4j/1.2/manual。 HTML 。你將有一個像這樣的log4j.properties文件

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

然後在您的應用程序中使用

Logger logger = Logger.getLogger(MyClassName.class);
logger.info("Doing something I want to log at info... " );
logger.warn("Danger!");

如果您正在編寫一個庫來分發(大多數人在當天使用的公共記錄),請查看sl4j。 http://www.slf4j.org/ 。大多數庫正在切換到此而不是公共日誌記錄。

轉載註明原文: 如何使用Apache Commons記錄Digester?