Skip to content

Logging

Ben Tupper edited this page Feb 2, 2021 · 2 revisions

We recommend using the futile.logger package. We don't use it's full functionality, but it's interface is pretty simple and easy. Well, except for kicking it off.

[NOTE we reserve the right to change external packages, like futile.logger, to another package that provides similar functionality. To protect the users, we provide "wrappers" to wrap around function for external packages. For instance, you might be accustomed to calling futile.logger::info("this is an info message"). We provide you an alternative, charlier::info("this is an info message"). The user doesn't need to worry about what is going on under the hood - it should just work. In theory, if we ever change what is under the hood it shouldn't affect your scripts (or, in the worst case, only minimally.)]

We suggest that you log to the console and a log file at the same time - called a unixy term tee. Configuring the logger to tee can be a bit opaque to the casual scripter, so we have wrapped the details in a simplifying function. Once you load the futile.logger package it's logging services are available to you, then just configure the tee, and, finally, start messaging.

library(charlier)
charlier::start_logger(filename = "~/my_log")

At the simplest usage, we have three levels of messaging available to us: informational, warning and error. Messages can be crafted programmatically using the same string construction as used in the sprintf function. The point is to use logging in an automated script or workflow, so learning to build messages programmatically is worthwhile.

what <- list("coffee", 2, "you're gonna need a lot more")

charlier::info("Good morning, have you had your %s?", what[[1]])
#INFO [2020-12-14 07:52:44] Good morning, have you had your coffee?

charlier::warn("Oh, you might need %i cups of %s", what[[2]], what[[1]])
#WARN [2020-12-14 07:53:43] Oh, you might need 2 cups of coffee

charlier::error("Uh, %s than %i cups of %s", what[[3]], what[[2]], what[[1]])
#ERROR [2020-12-14 07:53:57] Uh, you're gonna need a lot more than 2 cups of coffee

Clone this wiki locally