【问题标题】:Using tryCatchLog with futile.logger: write to file instead of console将 tryCatchLog 与 futile.logger 一起使用:写入文件而不是控制台
【发布时间】:2022-01-13 12:09:32
【问题描述】:

我正在使用 tryCatchLog 并希望将所有警告和错误消息发送到日志文件。我不希望控制台有任何输出。

tryCatchLog 的文档中,我遇到了这段代码 sn-p:

library(futile.logger)
# log to a file (not the console  which is the default target of futile.logger).
# You could also redirect console output into a file if start your R > script with a shell script using Rscript!
flog.appender(appender.file("my_app.log"))

vignette for tryCatchLog 包含以下代码 sn-p 以更改日志记录行为并将错误发送到文件而不是控制台:

  library(futile.logger)

  flog.appender(appender.file("app.log"))

  flog.threshold(ERROR)    # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

  try(log(-1))             # the warning will not be logged!

这表明我可以使用 flog.apppender(appender.file()) 简单地将消息重定向到日志文件。但是,我没有写入文件,而是在控制台上得到以下输出:

空 空值 日志中的警告(-1):产生了 NaN [1] NaN

vignette for tryCatchLog 在最佳实践部分提供了此代码示例:

库(futile.logger)

library(tryCatchLog)

options(keep.source = TRUE)        # source code file name and line number tracking
options("tryCatchLog.write.error.dump.file" = TRUE) # dump for post-mortem analysis

flog.appender(appender.file("my_app.log"))  # to log into a file instead of console
flog.threshold(INFO)    # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

tryCatchLog(source("your_main_script.R"))

将最后一行代码修改为tryCatchLog(log("this will produce an error")) 以使示例更容易,我仍然不是写入日志文件而是写入控制台:

空 空值 日志中的错误(“这将产生错误”):数学函数的非数字参数

查看documentation of futile.loggerexamples on stackoverflow 也没有帮助我。基于它们,我认为以下应该将错误消息写入文件。我使用 try() 作为更多涉及的 tryCatchLog() 版本的替代,以确保它不是 tryCatchLog 的问题。

library(futile.logger)
flog.appender(appender.file(file.path(getwd(),'logs.txt')))
flog.threshold(WARN)    # TRACE, DEBUG, INFO, WARN, ERROR, FATAL
try(log(-1)) # this will create a warning
try(log("this will create an error"))  # this will create an error

该命令既不创建日志文件也不附加现有的日志文件。 相反,flog.appender() 和 flog.threshold() 返回 NULL(到控制台)。并且警告和错误消息也会打印到控制台。据推测,在将记录器链接到文件时我遗漏了一些东西(因此返回 NULL 值?)。

如何将 tryCatchLog 捕获的所有警告和错误重定向到文件(使用 futile.logger)而不向控制台输出任何内容?

【问题讨论】:

  • 顺便说一句:futile.logger 具有类似的功能ftry(),您可以使用它来代替标准的try

标签: r logging error-logging


【解决方案1】:

try()的输出与futile.logger()无关。

如果您在futile.logger 中配置文件附加程序,您仍然需要使用flog.* 函数(这就是日志记录的工作方式)写入日志输出:

library(futile.logger)

flog.info("this is an info log output")
# INFO [2021-12-08 19:51:13] this is an info log output
flog.warn("this is an warning log output")
# WARN [2021-12-08 19:51:14] this is an warning log output
flog.error("this is an error log output")
# ERROR [2021-12-08 19:51:14] this is an error log output

我猜你想要的是标准或错误输出的一种输出重定向(例如,通过capture.output),但是这个函数不会像时间戳一样添加日志信息,也不支持严重级别管理(“info”,“警告”、“错误”...)。

如果您想将try*() 与日志记录结合使用,您可以使用CRAN 包tryCatchLog,它完全符合您的要求(并且也支持futile.logger):

library(tryCatchLog)
tryLog(log(-1)) # this will create a warning
tryLog(log("this will create an error"))  # this will create an error

【讨论】:

  • 感谢您的回复。我实际上遇到了这个问题,因为我想使用 tryCatchLog 并且您正确地怀疑我想重定向输出。我正在编辑我的帖子以明确这种联系。我认为我设置文件可能存在单独的问题,这就是我没有提到 tryCatchLog 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 2017-09-30
  • 2013-07-22
  • 2012-12-18
相关资源
最近更新 更多