Is there any standard logging package for R?

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/

25371 次浏览

I'm not aware of any, so I was about to release a wrapper for log4j in the next few days (I've been testing it for a while now). I'll let you know when it's available.

The in-built (package base) functions are "warning", "message", "stop". These functions support multiple languages. If you want to log to a file, maybe you could use these functions together with "sink".

Searching with RSeek brought up the package futile with a logger functionality.

I just submitted a logging package to CRAN. it is based on some parts of an older version of the 'futile' package (by Brian Lee Yung Rowe).

You find the logging package:

It mimics the standard python logging package, but please be careful if you decide to use it. I also attempted to document it by example, the package home page on R-Forge points to a couple of possible usage sessions.

Any feedback will be read with interest!

I suggest the futile.logger package, it implements multiple hierarchical loggers with formatted output strings and you can send the output different ways. It also implements per-package loggers naturally.

A simpler alternative compared to logging and futile.logger:

log4r (cran, github)

The log4r package is meant to provide a clean, lightweight object-oriented approach to logging in R based roughly on the widely emulated log4j API. The example code below shows how the logger is used in practice to print output to a simple plaintext log file.

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.

At the moment, there is still no native library for logging. But there are four of them available on CRAN:

1) logging
- simple & log4j-like
- resembles standard Python library (uses that documentation as guideline)
- author started it in 2010, 'mature' by 2012
- adopted by WLOGSolutions
- actively maintained

2) futile.logger (recommended! I am also using it)
- actively maintaining
- supports json error logging
- similar semantics to Python’s logging as well as log4j-like
- may be complicated

3) log4r
- easy and log4j-like
- not being maintained since 2014

4) luzlogr
- supersimple - (open, write, close file)