Even though I have awarded the bounties to the users below that have
attempted to help,the original question remains unanswered. No actual
workable solution exists to ensure that the logback.groovy configured
logging is honoured within the junit tests. The tests load up the
logback config and it does report the correct level and yet still the
actual test logging (exclusively through slf4j) is at ANY or TRACE
levelI know others have encountered this same problem and it is very
annoying when tests for big projects take a lot longer due to console
logging being much too verbose. I cannot keep throwing bounties at
this question. I hope someone comes up with a good solution that
allows the tests logging to be properly configured at different levels
through a system property. Then,different configurations can be
created for the project so that tests will be able to configured at different logging threshold levels.
我的日志记录是通过logback.groovy文件进行logback配置的
现在,当我的Maven POM项目聚合所有其他项目时,它会传递所有的System属性来设置正确的日志记录级别.
但是,当junit测试运行时,由于某些原因,即使测试类静态@beforeClass确保日志配置正确配置,日志记录器也不会接收到正确的级别.
测试中的Logger并不是问题,而且 – 也是 – 他们也是 – 真正的问题是,运行代码段(我的程序记录器到处都是)的记录器设置为错误的日志记录级别.当配置程序测试时,他们没有收到日志记录.
但是,使用logback.goovy文件进行logback初始化时,项目报告正确.但是,实际的记录级别设置为TRACE或ALL
从下面的输出,很明显logback配置为INFO.但是在TRACE(最后一行)的第一个项目日志记录表明没有被提取.
帮帮我.
------------------------------------------------------- T E S T S ------------------------------------------------------- Running groovy.text.StreamingTemplateEngineTest Tests run: 1,Failures: 0,Errors: 0,Skipped: 0,Time elapsed: 1.245 sec Running net.abcd.templating.InlinerTest 01:22:15,265 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener] 01:22:15,290 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Setting ReconfigureOnChangeFilter scanning period to 5 minutes 01:22:15,290 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[C:\Users\ABDC\DropBox\workspace\abcd\AbcdTemplating\conf\logback.groovy]] every 300 seconds. 01:22:15,290 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Adding ReconfigureOnChangeFilter as a turbo filter 01:22:15,312 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 01:22:15,316 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Naming appender as [STDOUT] *********************************************************** LOGGING MODE PROPERTY 'net.abcd.logging.level' SET TO: [info] IT CAN BE SET TO: OFF,ERROR,WARN,INFO,DEBUG,TRACE,ALL,INFO *********************************************************** getLogLevel() returned 'INFO' 01:22:15,496 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Setting level of logger [ROOT] to INFO 01:22:15,532 |-INFO in ch.qos.logback.classic.gaffer.ConfigurationDelegate@1e8c3cb - Attaching appender named [STDOUT] to Logger[ROOT] 01:22:15.846 [main] TRACE net.abcd.templating.Inliner - Document:
我的logback.groovy文件是:
displayStatusOnConsole() scan('5 minutes') // Scan for changes every 5 minutes. setupAppenders() setupLoggers() def displayStatusOnConsole() { statusListener OnConsoleStatusListener } def setupAppenders() { appender('STDOUT',ConsoleAppender) { encoder(PatternLayoutEncoder) { pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %-16logger{50} - %msg%n" } } } def setupLoggers() { def loglevel = getLogLevel() println("getLogLevel() returned '${loglevel}'") root(loglevel,['STDOUT']) } def getLogLevel() { def mode = System.getProperty('net.abcd.logging.level','') println("***********************************************************") println("") println("LOGGING MODE PROPERTY 'net.abcd.logging.level' SET TO: [${mode}]") println("IT CAN BE SET TO: OFF,INFO") println("") println("***********************************************************") switch(mode.toLowerCase()){ case 'off': return OFF case 'error': return ERROR case 'warn': return WARN case 'info': return INFO case 'debug': return DEBUG case 'trace': return TRACE case 'all': return ALL default: return INFO } }
解决方法
我找不到一个很好的解决方案.我以下使用下面的工作:
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import org.slf4j.LoggerFactory; ... static Logger logger; static{ // Logger.ROOT_LOGGER_NAME == "ROOT" logger = ((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)); logger.setLevel(Level.INFO); }
…
我认为在你的情况下,不知何故,一个库使用它自己的ConsoleAppender实例,名称不是“STDOUT”.
希望设置根日志级别可以解决问题.
root(loglevel,['ROOT']);