loggit

Modern Logging for the R Ecosystem

31
0
R

title: “README”
output:
md_document:
variant: gfm
vignette: >
%\VignetteIndexEntry{README}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Modern Logging for the R Ecosystem

Ryan Price [email protected]

CRAN_Status_Badge
R-CMD-check
Monthly downloads
Support me on Ko-Fi


loggit is an ndJSON logging library
for R software. It is blazingly fast when writing logs, and has zero external
dependencies. loggit can be as simple and unobstrusive as you’d like, or as
involved as your application needs it to be.

Please see below for some quick examples, and read the
vignettes
for the
Getting Started guide, as well as some other use case examples.

Why use loggit?

There are indeed several logging packages available for R. loggit, however,
takes a more modern approach approach to logging in R:

  • Opting to use the JSON format, which is parsable by most modern software
  • Designed with log streams in mind
  • Unobtrusive, yet highly flexible
  • Convenient ability to log data, then analyze that log data on the same host.

Additionally, the boilerplate to get going with loggit is minimal at worst,
only requiring you to point to the log file. If deploying your R code in a
container ecosystem, you don’t even need to do that, since loggit will
echo its formatted logs to stdout. No need to write custom formatters,
handlers, levels, etc. – just f&ck#n’ loggit!

Quick Examples

The quickest way to get up & running with loggit is to… do nothing special.
loggit’s simplest functionality does its best to stay out of your way. You’ll
probably want to point it to a log file, though; otherwise, logs will print to
the console, but land in a tempfile.

library(loggit)
# set_logfile("./loggit.log")

message("This is a message")
warning("This is a warning")
# stop("This actually throws a critical error, so I'm not actually going to run it here :)"))
#> {"timestamp": "2020-05-31T20:59:33-0500", "log_lvl": "ERROR", "log_msg": "This actually throws a critical error, so I'm not actually going to run it here :)"}

You can suppress each part of the console output separately (both the loggit
ndJSON and the regular R output) but the default is to post both. Only the
ndJSON is written to the log file.

You can also use the loggit() function directly to compose much more custom
logs, including entirely custom fields (and prevent throwing actual status
codes until you wish to handle them). loggit doesn’t require that you set
custom logger objects or anything like that: just throw whatever you want at it,
and it’ll become a structured log.

loggit("ERROR", "This will log an error - but not actually throw one yet", rows = nrow(iris), anything_else = "you want to include")

# Read log file into data frame to implement logic based on entries
logdata <- read_logs()
print(logdata)
if (any(logdata$log_lvl == "ERROR")) {
  print("Errors detected somewhere in your code!") # but you can throw a stop() here, too, for example
}

Again, check out the
vignettes
for more
details!

Installation

You can install the latest CRAN release of loggit via
install.packages("loggit").

Or, to get the latest development version from GitHub –

Via devtools:

devtools::install_github("ryapric/loggit")

Or, clone & build from source:

cd /path/to/your/repos
git clone https://github.com/ryapric/loggit.git loggit
make install

To use the most recent development version of loggit in your own package, you
can include it in your Remotes: field in your DESCRIPTION file:

Remotes: github::ryapric/loggit

Note that packages being submitted to CRAN cannot have a Remotes field.
Refer
here
for more info.

License

MIT