[英]Grails log4j configuration
我反復遇到過Grails在版本1.1中引入的用於配置Log4J的DSL問題。 我當前的配置如下所示:
log4j = {
debug 'com.mypackages'
appenders {
console name: 'stdout', layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n')
rollingFile name: 'hibeFile', file: "hibeFile", maxFileSize: '500KB'
}
// By default, messages are logged at the error level to both the console and hibeFile
root {
error 'stdout', 'hibeFile'
additivity = true
}
}
目的是:
com.mypackages
,在錯誤級別記錄所有其他日志 當我運行應用程序或集成測試時,這可以正常工作。 但是,當我運行單元測試時,控制台或Grails測試報告中顯示的“ System.out”或“ System.err”鏈接中都沒有日志記錄。 運行單元測試時如何查看日志?
謝謝唐
AFAIK,在運行Grails單元測試時,無法通過log4j進行整個日志記錄,僅使用來模擬域類中的log.xxxx調用。
mockLogging(ClassUnderTest, true)
“ true”代表“啟用調試”。 為此,單元測試類必須擴展GrailsUnitTestCase。 如果使用mockController(class),它將隱式調用嘲笑LogLogging(class,false),因此不會獲得調試日志記錄。 有關詳細信息,請查看grails源,尤其是GrailsUnitTestCase和MockUtils。
與上述相反,在集成測試中,將啟動整個機器,並且log4j可用。
這是一個想法,您沒有完全詢問,但是在grails用戶組上提出了相同的問題。 我也在這里發布我的答案,以傳播知識。
如果您在類中明確地說def log = org.apache.commons.logging.LogFactory.getLog(this),而不是像grails用戶組中所述那樣依賴於依賴項注入,則可以在LogFactory上模擬getLog。
以下摘自grails.tests.MockUtils.mockLogging,並進行了修改以返回記錄器。
class LoggingEnabledTestCase extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
registerMetaClass(org.apache.commons.logging.LogFactory)
org.apache.commons.logging.LogFactory.metaClass.'static'.getLog = {instance ->
// This is taken from grails.tests.MockUtils and slightly changed to return a logger.
// Get the name of the class + the last component of the package
// (if it the class is in a package).
def pos = instance.class.name.lastIndexOf('.')
if (pos != -1) pos = instance.class.name.lastIndexOf('.', pos - 1)
def shortName = instance.class.name.substring(pos + 1)
// Dynamically inject a mock logger that simply prints the
// log message (and optional exception) to stdout.
def mockLogger = [
fatal: {String msg, Throwable t = null ->
println "FATAL (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
error: {String msg, Throwable t = null ->
println "ERROR (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
warn: {String msg, Throwable t = null ->
println "WARN (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
info: {String msg, Throwable t = null ->
println "INFO (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
debug: {String msg, Throwable t = null ->
println "DEBUG (${shortName}): $msg"
if (t) {
println " Exception thrown - ${t.message}"
}
},
trace: {String msg, Throwable t = null -> },
isFatalEnabled: {-> true},
isErrorEnabled: {-> true},
isWarnEnabled: {-> true},
isInfoEnabled: {-> true},
isDebugEnabled: {-> true},
isTraceEnabled: {-> false}] as Log
return mockLogger
}
}
protected void tearDown() {
super.tearDown()
}
}
使用Grails 1.1.1和您的設置的近似值,我有一個名為FooTests.groovy
的單元測試。
運行grails test-app
,我可以在目錄中查看測試的輸出:
./test/reports/plain
具體在文件中,視情況而定:
TEST-com.mypackages.FooTests-err.txt
TEST-com.mypackages.FooTests-out.txt
TEST-com.mypackages.FooTests.txt
請注意,我在hibeFile中看不到任何輸出。 我不確定,但是我懷疑以前的海報是正確的,因為單元測試未收到日志記錄設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.