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/

joran
  • 169,992
  • 32
  • 429
  • 468
mariotomo
  • 9,438
  • 8
  • 47
  • 66

7 Answers7

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!

Artem Klevtsov
  • 9,193
  • 6
  • 52
  • 57
mariotomo
  • 9,438
  • 8
  • 47
  • 66
  • by the way, I recently added a handler that allows sending records to a [sentry](https://www.getsentry.com/welcome/) server. – mariotomo Jun 02 '12 at 09:01
  • 6
    This package is no longer on CRAN. – Spacedman Mar 28 '13 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 07 '13 at 05:12
  • I had forgotten updating my email address, it's back online. – mariotomo Apr 12 '13 at 06: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 '15 at 10:56
  • There is an important flaw with the `logging` package: subsequent loggers in the hierarachy do not inherit the log level from their ancestors. Example: setting the logger `io` to `DEBUG` does not cause messages with `logdebug(msg, logger="io.csv")` to be published. I prefer `futile.logger` in the latest version since IMHO it is more transparent and easier to setup. – SaschaH Mar 09 '15 at 15:42
  • this package is not _unmaitained_,: it has two contributors, Kirill Müller and me. We don't receive much feedback on github, and I personally stopped using R about three years ago. Anybody who wants to raise issues, use the github project, and contribute pull requests. – mariotomo Apr 04 '15 at 19:58
  • @SaschaH: messages are filtered twice, once by the _logger_ receiving it, once by the various _handlers_ attached to the loggers to which the message is offered. so please read the docs and check your handlers' levels. – mariotomo Apr 04 '15 at 20:02
  • @mariotomo: check the following [example](https://bitbucket.org/snippets/S-Holzhauer/peLz/r-logging-no-inheritance) (handler set to DEBUG). Do I miss something else? – SaschaH Apr 16 '15 at 10:24
  • your examples show the intended behaviour. the logger discards all logging events below its own level, then offers what remains to its handlers and to its parent logger. – mariotomo Apr 16 '15 at 15:14
  • @SaschaH ... if you want a logger to pass the messages it receives to its parent, but not to handle it directly, set the logger ad a low level, do not add handlers to it (or add a handler at a higher level). then play with the level of the handlers up the path to the root logger. – mariotomo Jun 22 '15 at 12:02
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)

mariotomo
  • 9,438
  • 8
  • 47
  • 66
torina
  • 3,825
  • 2
  • 26
  • 31
  • 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 '16 at 16:41
  • There is yet another log4j port here: https://github.com/mfrasca/r-logging although it doesn't have recent activity – shadowtalker Jan 09 '17 at 19:51
  • And finally https://github.com/mjkallen/rlogging masks `stop`/`warning`/`message` to incorporate logging functionality directly – shadowtalker Jan 09 '17 at 19:52
  • 2
    I found this [tutorial](https://cartesianfaith.com/2013/03/10/better-logging-in-r-aka-futile-logger-1-3-0-released/) for using `futile.logger`. – David Leal Feb 06 '17 at 23:05
  • 2
    Simple but fast to apply is also https://github.com/smbache/loggr – user2030503 Apr 17 '17 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.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • As of now, how does it compare to mariotomo's `logging` package? – krlmlr Apr 12 '13 at 08: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 '13 at 06:57
  • 2
    @mariotomo: `futile.logger` is also on GitHub now: https://github.com/muxspace/futile.logger, and it seems to be maintained actively. Indeed, it would be better to have *one* logging package. – krlmlr Jul 08 '13 at 12:49
  • Does this support logging in parallel applications, e.g. using the parallel package? – Eike P. Feb 18 '15 at 11:01
  • 2
    @jhin you should be able to use [logR](https://github.com/jangorecki/logR). Which logs to db + concurrency write. – jangorecki Mar 14 '15 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.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • _futile_ looks interesting. If I understood correctly, it doesn't (yet) implement the whole structure logger/handler as in Python and Java. – mariotomo Jan 05 '10 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 '10 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.

talegari
  • 343
  • 2
  • 9
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.

jangorecki
  • 16,384
  • 4
  • 79
  • 160
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.

Shane
  • 98,550
  • 35
  • 224
  • 217
  • 2
    a "wrapper"? in the sense that it links to the .jar? I'm curious about the possibility! – mariotomo Dec 18 '09 at 14:23
  • 1
    That's right. It uses rJava: http://www.rforge.net/rJava/ – Shane Dec 18 '09 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 05 '10 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 06 '10 at 02: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 '10 at 05:46