Is there any standard logging package for R? [clos

2019-01-21 06:38发布

I am looking for the standard (if any) logging package for R, and some sample usage?

I also don't see any among the packages listed: http://cran.r-project.org/web/packages/

标签: logging r
7条回答
别忘想泡老子
2楼-- · 2019-01-21 07:09

I've started logR project in June 2014. Initially it was a R process logger with exception handling capable to log to csv and DBI/RODBDC/RJDBC databases.
Starting from 2.1 version I've switched to support only PostgreSQL as backend for logs.
If you are able to arrange single table in postgres database then you can easily use logR.

Upstream repo, and github mirror.

Some of logR features:

  • transactional logging: insert log, evaluate expression, update log
  • log to postgres database
  • records errors, warnings, messages, interrupts
  • log process metadata: in/out nrow, flexible list of custom metadata
  • high precision timing with optional microbenchmarkCore
  • support parallel processing
  • hierarchical logs - logging parent log id (new in 2.1.5)

It requires RPostgreSQL and data.table packages.

Usage:

# install logR
install.packages("logR", repos = c("https://jangorecki.github.io/logR", "https://cran.rstudio.com"))

# attach logR
library(logR)

# setup connection, default to env vars: `POSTGRES_DB`, etc.
# if you have docker then: docker run --rm -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=postgres --name pg-logr postgres:9.5
logR_connect()
# [1] TRUE

# create logr table
logR_schema()

# make some logging and calls

logR(1+2) # OK
#[1] 3
logR(log(-1)) # warning
#[1] NaN
f = function() stop("an error")
logR(r <- f()) # stop
#NULL
g = function(n) data.frame(a=sample(letters, n, TRUE))
logR(df <- g(4)) # out rows
#  a
#1 u
#2 c
#3 w
#4 p

# try CTRL+C / 'stop' button to interrupt
logR(Sys.sleep(15))

# wrapper to: dbReadTable(conn = getOption("logR.conn"), name = "logr")
logR_dump()
#   logr_id              logr_start          expr    status alert                logr_end      timing in_rows out_rows  mail message cond_call  cond_message
#1:       1 2016-02-08 16:35:00.148         1 + 2   success FALSE 2016-02-08 16:35:00.157 0.000049163      NA       NA FALSE      NA        NA            NA
#2:       2 2016-02-08 16:35:00.164       log(-1)   warning  TRUE 2016-02-08 16:35:00.171 0.000170801      NA       NA FALSE      NA   log(-1) NaNs produced
#3:       3 2016-02-08 16:35:00.180      r <- f()     error  TRUE 2016-02-08 16:35:00.187 0.000136896      NA       NA FALSE      NA       f()      an error
#4:       4 2016-02-08 16:35:00.197    df <- g(4)   success FALSE 2016-02-08 16:35:00.213 0.000696145      NA        4 FALSE      NA        NA            NA
#5:       5 2016-02-08 16:35:00.223 Sys.sleep(15) interrupt  TRUE 2016-02-08 16:35:05.434 5.202319000      NA       NA FALSE      NA        NA            NA

More examples can be found in logR unit tests.

查看更多
登录 后发表回答