如何在Scala单元/功能测试中禁止日志记录?

huangapple go评论90阅读模式
英文:

How do I suppress logging for Scala unit / functional tests?

问题

以下是翻译好的部分:

"I'm testing code that uses a third-party package (Kafka Streams), and this package is generating a lot of log statements that are cluttering up the test output. The log level goes all the way down to INFO. To make it easier to read the output I'd like to suppress all logging when running tests. Is this possible?"

"我正在测试使用第三方包(Kafka Streams)的代码,而这个包生成了许多日志语句,这些语句混杂在测试输出中。日志级别一直下降到INFO。为了更容易阅读输出,我想在运行测试时禁止所有日志记录。这是否可能?"

"I'm using ScalaTest and log4j. I used sbt to build the project. I wasn't able to find anything about log suppression in the ScalaTest docs. Is the solution then to configure separate loggers, and use a logger configured for testing when testing? If so, how would I direct the third-party library to use this specific logger only when testing?"

"我正在使用ScalaTest和log4j。我使用 sbt构建项目。我在ScalaTest文档中找不到有关日志抑制的任何信息。解决方案是配置单独的记录器,然后在测试时使用配置为测试的记录器吗?如果是这样,我该如何指示第三方库仅在测试时使用这个特定的记录器?"

"Here's some sample output, there's really no need for me to see these WARN or INFO statements:"

"这里是一些示例输出,我真的没有必要看到这些WARNINFO语句:"

"And here's what the output looks like when zoomed out, so you can get a sense of how cluttered it is:"

"当放大时,输出的样子如下,这样你就可以感受到它有多混乱:"

"The green text represents actual test results, and the red text is the log clutter."

"绿色文字表示实际的测试结果,而红色文字是日志混乱。"

"I don't seem to be able to set log4j properties either. I have the following log4j.properties file in my project:"

"我似乎也无法设置log4j属性。我在我的项目中有以下log4j.properties文件:"

"But this doesn't seem to change logging behavior at all. If I understand correctly I should be seeing log statements written to a file, but that isn't happening."

"但这似乎根本不改变日志记录行为。如果我理解正确,我应该看到日志语句被写入文件,但这并没有发生。"

英文:

I'm testing code that uses a third-party package (Kafka Streams), and this package is generating a lot of log statements that are cluttering up the test output. The log level goes all the way down to INFO. To make it easier to read the output I'd like to suppress all logging when running tests. Is this possible?

I'm using ScalaTest and log4j. I used sbt to build the project. I wasn't able to find anything about log suppression in the ScalaTest docs. Is the solution then to configure separate loggers, and use a logger configured for testing when testing? If so, how would I direct the third-party library to use this specific logger only when testing?

Here's some sample output, there's really no need for me to see these WARN or INFO statments:

- should filter invalid HelmData
Preprocessor.create_topology
[pool-1-thread-1-ScalaTest-running-EntityResolutionAppTest] WARN org.apache.kafka.streams.processor.internals.StateDirectory - Using an OS temp directory in the state.dir property can cause failures with writing the checkpoint file due to the fact that this directory can be cleared by the OS. Resolved state.dir: [/var/folders/mk/42jzsvb14jn_q5czv6cp6tcw0000gn/T//kafka-streams]
[pool-1-thread-1-ScalaTest-running-EntityResolutionAppTest] INFO org.apache.kafka.streams.state.internals.RocksDBTimestampedStore - Opening store helmDataByIdStateStore in regular mode
[pool-1-thread-1-ScalaTest-running-EntityResolutionAppTest] INFO org.apache.kafka.streams.state.internals.RocksDBTimestampedStore - Opening store KSTREAM-AGGREGATE-STATE-STORE-0000000014 in regular mode
[pool-1-thread-1-ScalaTest-running-EntityResolutionAppTest] INFO org.apache.kafka.streams.state.internals.RocksDBTimestampedStore - Opening store customerProfileStateStore in regular mode
[pool-1-thread-1-ScalaTest-running-EntityResolutionAppTest] INFO org.apache.kafka.streams.state.internals.RocksDBTimestampedStore - Opening store KSTREAM-AGGREGATE-STATE-STORE-0000000048 in regular mode

And here's what the output looks like when zoomed out, so you can get a sense of how cluttered it is:

如何在Scala单元/功能测试中禁止日志记录?

The green text represents actual test results, and the red text is the log clutter.

I don't seem to be able to set log4j properties either. I have the following log4j.properties file in my project:

log = /tmp/log4j
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

But this doesn't seem to change logging behavior at all. If I understand correctly I should be seeing log statements written to a file, but that isn't happening.

答案1

得分: 2

这里提供了关于log4j的所有日志级别。不确定您使用哪种类型的配置,我是说,属性文件、XML文件等等。您需要将日志级别设置为 OFF。这里有一些关于如何在log4j中启用/禁用日志记录的示例的示例。

英文:

Here you have all the log levels for log4j. Not sure which type of configuration you have. I mean, properties file, xml file, etc. You have to set the log level to OFF. Here you have some examples on how to Enable/Disable Logging in log4j

huangapple
  • 本文由 发表于 2023年7月11日 00:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定