r-将日志记录添加到复杂函数中



假设我有一个(至少主观上)复杂的函数,如下所示:

library(rgithub)
pull <- function(i){
 commits <- get.pull.request.commits(owner = owner, repo = repo, id = i, ctx = get.github.context(), per_page=100)
 links <- digest_header_links(commits)
 number_of_pages <- links[2,]$page
 if (number_of_pages != 0)
   try_default(for (n in 1:number_of_pages){
    if (as.integer(commits$headers$`x-ratelimit-remaining`) < 5)
     Sys.sleep(as.integer(commits$headers$`x-ratelimit-reset`)-    as.POSIXct(Sys.time()) %>% as.integer())
  else
    get.pull.request.commits(owner = owner, repo = repo, id = i, ctx = get.github.context(), per_page=100, page = n)
}, default = NULL)
else 
   return(commits)
}
list <- c(500, 501, 502)
pull_lists <- lapply(list, pull)

比方说,我想对这个函数内部实际发生的事情有更深入的了解。如何添加某种类型的日志记录,以帮助我在函数运行时跟踪函数内部的情况?

您可以使用futile.logger

然后您可以使用设置日志阈值级别

flog.threshold(INFO)

flog.debug或flog.info等函数用于生成日志信息

有关更多详细信息,请参阅:

http://www.r-bloggers.com/better-logging-in-r-aka-futile-logger-1-3-0-released/

http://cran.r-project.org/web/packages/futile.logger/index.html

最新更新