81

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/

0

7 Answers 7

54

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!

11
  • by the way, I recently added a handler that allows sending records to a sentry server.
    – mariotomo
    Jun 2, 2012 at 9:01
  • 6
    This package is no longer on CRAN.
    – Spacedman
    Mar 28, 2013 at 20:11
  • @Spacedman, yes, somehow it was removed. a user noted that and asked the CRAN maintainers, putting me in the cc. I did not go after the issue yet.
    – mariotomo
    Apr 7, 2013 at 5:12
  • I had forgotten updating my email address, it's back online.
    – mariotomo
    Apr 12, 2013 at 6:32
  • 4
    This doesn't seem to be actively maintained anymore (last commit 2 years ago). The futile.logger package (see Spacedman's answer), however, is. So that's the path to follow?
    – Eike P.
    Feb 18, 2015 at 10:56
37

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)

5
  • 15
    I'm the author and current maintainer of 'logging'. it looks indeed unmaintained because I'm not updating it. but I react to bug reports and I am more than available to accept any quality contributions. more than "unmaintained", I would call it "mature" ;)
    – mariotomo
    Aug 31, 2016 at 16:41
  • There is yet another log4j port here: github.com/mfrasca/r-logging although it doesn't have recent activity Jan 9, 2017 at 19:51
  • And finally github.com/mjkallen/rlogging masks stop/warning/message to incorporate logging functionality directly Jan 9, 2017 at 19:52
  • 2
    I found this tutorial for using futile.logger.
    – David Leal
    Feb 6, 2017 at 23:05
  • 2
    Simple but fast to apply is also github.com/smbache/loggr Apr 17, 2017 at 20:39
16

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.

5
  • As of now, how does it compare to mariotomo's logging package?
    – krlmlr
    Apr 12, 2013 at 8:20
  • 1
    futile prefers the naming style coming from C/C++, when I tried it I missed a clear distinction logger/handler, in the meanwhile it surely got better, but I didn't test it again. I offered cooperation (and shared my work) for producing one package, the reply came late for my work related needs, and I perceived it as a "I'm working at it, when I'm done I'll share the result with you". This is possibly the main difference: futile is on CRAN, logging on github, r-forge, CRAN.
    – mariotomo
    Apr 18, 2013 at 6:57
  • 2
    @mariotomo: futile.logger is also on GitHub now: github.com/muxspace/futile.logger, and it seems to be maintained actively. Indeed, it would be better to have one logging package.
    – krlmlr
    Jul 8, 2013 at 12:49
  • Does this support logging in parallel applications, e.g. using the parallel package?
    – Eike P.
    Feb 18, 2015 at 11:01
  • 2
    @jhin you should be able to use logR. Which logs to db + concurrency write.
    – jangorecki
    Mar 14, 2015 at 22:45
10

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.

2
  • futile looks interesting. If I understood correctly, it doesn't (yet) implement the whole structure logger/handler as in Python and Java.
    – mariotomo
    Jan 5, 2010 at 10:52
  • I have been in contact with the author of the futile library. we might come out with a candidate logging functionality, either as part of futile or as a new package.
    – mariotomo
    Jan 13, 2010 at 13:36
4

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.

4

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.

3

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.

5
  • 2
    a "wrapper"? in the sense that it links to the .jar? I'm curious about the possibility!
    – mariotomo
    Dec 18, 2009 at 14:23
  • 1
    That's right. It uses rJava: rforge.net/rJava
    – Shane
    Dec 18, 2009 at 22:50
  • saw rJava, looks good! but back to the wrapper, if you plan to make it GPL, you could also share it while it's not yet ready, so we can work on it concurrently.
    – mariotomo
    Jan 5, 2010 at 10:54
  • Mariotomo: Sorry for the delay with this. I'll post it on Google code so that you can build from it. It's the documentation that always kills me in trying to post to CRAN.
    – Shane
    Jan 6, 2010 at 2:57
  • very late reply: why not on r-forge? I might find the time to help documenting and avoiding R CMD check errors and warnings. after that, posting to CRAN is just 3 clicks away.
    – mariotomo
    Oct 18, 2010 at 5:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.