lgr

A fully featured logging framework for R

https://github.com/s-fleck/lgr

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (17.7%) to scientific vocabulary

Keywords

log4j logging r r6
Last synced: 6 months ago · JSON representation

Repository

A fully featured logging framework for R

Basic Info
Statistics
  • Stars: 82
  • Watchers: 3
  • Forks: 9
  • Open Issues: 9
  • Releases: 9
Topics
log4j logging r r6
Created over 7 years ago · Last pushed 7 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(lgr)
basic_config(console_connection = stdout())  # ensure default config
# 
```
# lgr

[![CRAN status](https://www.r-pkg.org/badges/version/lgr)](https://cran.r-project.org/package=lgr)
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-stable-green.svg)](https://lifecycle.r-lib.org/articles/stages.html)

lgr is a logging package for R built on the back of 
[R6](https://github.com/r-lib/R6) classes. It is designed to be flexible,
performant and extensible. The package 
[vignette](https://s-fleck.github.io/lgr/articles/lgr.html) contains a 
comprehensive description of the features of lgr (some of them unique among
R logging packages) along with many code examples.

Users that have not worked with R6 classes before, will find configuring
Loggers a bit strange and verbose, but care was taken to keep
the syntax for common logging tasks and interactive usage simple and concise.
User that have experience with 
  [shiny](https://github.com/rstudio/shiny), 
  [plumber](https://github.com/rstudio/plumber),
  [python logging](https://docs.python.org/3/library/logging.html) or 
  [Apache Log4j](https://logging.apache.org/log4j/2.x/) will feel
at home. User that are proficient with R6 classes will also find it easy
to extend and customize lgr, for example with their own appenders Loggers or 
Appenders. 



## Features

* *Hierarchical loggers* like in log4j and python logging. This is useful if you
  want to be able to configure logging on a per-package basis.
* An *arbitrary number of appenders* for each logger. A single logger can write
  to the console, a logfile, a database, etc... .
* Support for structured logging. As opposed to many other logging 
  packages for R a log event is not just a message with a timestamp, but an
  object that can contain arbitrary data fields. This is useful for producing 
  machine readable logs.
* *Vectorized* logging (so `lgr$fatal(capture.output(iris))` works)
* Lightning fast *in-memory logs* for interactive use.
* Appenders that write logs to a wide range of destinations:
    * databases (buffered or directly)
    * email or pushbullet
    * plaintext files (with a powerful formatting syntax)
    * JSON files with arbitrary data fields
    * Rotating files that are reset and backed-up after they reach
      a certain file size or age
    * memory buffers
    * (colored) console output
* Optional support to use [glue](https://glue.tidyverse.org/) instead of 
  `sprintf()` for composing log messages.


## Usage

To log an *event* with with lgr we call `lgr$()`. Unnamed 
arguments to the logging function are interpreted by `sprintf()`. For a way
to create loggers that [glue](https://glue.tidyverse.org/) instead
please refer to the vignette.

```{r}
lgr$fatal("A critical error")
lgr$error("A less severe error")
lgr$warn("A potentially bad situation")
lgr$info("iris has %s rows", nrow(iris))

# the following log levels are hidden by default
lgr$debug("A debug message")
lgr$trace("A finer grained debug message")
```

A Logger can have several Appenders. For example, we can add a JSON appender to
log to a file with little effort. 

```{r}
tf <- tempfile()
lgr$add_appender(AppenderFile$new(tf, layout = LayoutJson$new()))
lgr$info("cars has %s rows", nrow(cars))
cat(readLines(tf))
```

By passing a named argument to the log function, you can log not only 
text but arbitrary R objects. Not all appenders support structured logging
perfectly, but JSON does. This way you can create 
logfiles that are machine as well as (somewhat) human readable.

```{r}
lgr$info("loading %s", "cars", rows = nrow(cars), cols = ncol(cars), vector = c(1, 2, 3))
cat(readLines(tf), sep = "\n")
```

```{r, echo=FALSE}
unlink(tf)
```

For more examples please see the 
package 
[vignette](https://s-fleck.github.io/lgr/articles/lgr.html) and 
[documentation](https://s-fleck.github.io/lgr/)


## See lgr in action

lgr is used to govern console output in my shiny based csv editor 
[shed](https://github.com/s-fleck/shed)

```{r eval = FALSE}
# install.packages("remotes")
remotes::install_github("s-fleck/shed")
library(shed)

# log only output from the "shed" logger to a file
logfile <- tempfile()
lgr::get_logger("shed")$add_appender(AppenderFile$new(logfile))
lgr::threshold("all")

# edit away and watch the rstudio console!
lgr$info("starting shed")
shed(iris)  
lgr$info("this will not end up in the log file")

readLines(logfile)

# cleanup
file.remove(logfile)
```


## Development status

lgr is stable and safe for use. I've been using it in production code for 
several years myself. There has been very little recent development because
it's pretty stable and contains (nearly) all planned features. 

Notable points that are still planned (without specific ETA):

* Support for config files is heavily experimental and incomplete. 
  This is an important basic feature, but I have not yet found a great way to
  support this in a generic way. For now, I recommend you come up with your own 
  solution if you need to lgr to work in a production environment that relies 
  on config files.

* Improve the documentation. The documentation should be mostly complete, but 
  is not perfect. If there's something missing or something you don't understand,
  please ask (for example via a github issue).


## Dependencies

[R6](https://github.com/r-lib/R6): The R6 class system provides the framework
on which lgr is built and the **only Package lgr will ever depend on**. If you 
are a **package developer** and want to add logging to your package, this is the
only transitive dependency you have to worry about, as configuring of the 
loggers should be left to the user of your package.


### Optional dependencies

lgr comes with a long list of optional dependencies that make a wide range of
appenders possible. You only need the dependencies for the Appenders you
actually want to use. Care was taken to choose packages that are slim, stable, 
have minimal dependencies, and are well maintained :
  
  Extra appenders (in the main package):
  
  - [jsonlite](https://github.com/jeroen/jsonlite) for JSON logging via 
    `LayoutJson`. JSON is a popular plaintext based file format that is easy to 
    read for humans and machines alike.
    
  - [rotor](https://github.com/s-fleck/rotor) for log rotation via 
    AppenderFileRotating and co. 
    
  - [data.table](https://github.com/Rdatatable/) for fast in-memory 
    logging with `AppenderDt`, and also by all database / DBI Appenders. 

  - [glue](https://glue.tidyverse.org/) for a more flexible formatting syntax
    via LoggerGlue and LayoutGlue.
    
  Extra appenders via lgrExtra:
  
  - For support for Elasticsearch, Dynatrace, Push- and Email notifications, 
    etc... as well as the relevant dependencies please refer to the 
    documentation of [lgrExtra](https://github.com/s-fleck/lgrExtra)

  Other extra features:
  
  - [yaml](https://CRAN.R-project.org/package=yaml) for configuring loggers 
    via YAML files (experimental)
  - [crayon](https://github.com/r-lib/crayon) for colored console output.  
  - [whoami](https://github.com/r-lib/whoami/blob/master/DESCRIPTION) for 
    guessing the user name from various sources. You can also set the user name 
    manually if you want to use it for logging.
  - [desc](https://CRAN.R-project.org/package=desc) for the package development
    convenience function `use_logger()`
  - [cli](https://CRAN.R-project.org/package=cli) for printing the tree 
    structure of registered loggers with `logger_tree()`
    
  Other `Suggests`
  ([future](https://CRAN.R-project.org/package=future), 
  [future.apply](https://CRAN.R-project.org/package=future.apply)) do not 
  provide extra functionality but had to be included for some of 
  the automated unit tests run by lgr.
    

## Installation

You can install lgr from CRAN

```{r eval = FALSE}
install.packages("lgr")
```

Or you can install the current development version directly from github

```{r eval = FALSE}
#install.packages("remotes")
remotes::install_github("s-fleck/lgr")
```

## Outlook

The long term goal is to support (nearly) all features of the python logging
module. If you have experience with python logging or Log4j and are missing
features/appenders that you'd like to see, please feel free to post a feature 
request on the issue tracker.


## Acknowledgement

* [diagrams.net](https://app.diagrams.net/) for the flow chart in the vignette

Owner

  • Name: Stefan Fleck
  • Login: s-fleck
  • Kind: user
  • Location: Vienna

GitHub Events

Total
  • Issues event: 7
  • Watch event: 3
  • Issue comment event: 11
  • Push event: 30
  • Pull request event: 12
  • Fork event: 1
  • Create event: 1
Last Year
  • Issues event: 7
  • Watch event: 3
  • Issue comment event: 11
  • Push event: 30
  • Pull request event: 12
  • Fork event: 1
  • Create event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 789
  • Total Committers: 8
  • Avg Commits per committer: 98.625
  • Development Distribution Score (DDS): 0.384
Past Year
  • Commits: 6
  • Committers: 3
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.333
Top Committers
Name Email Commits
Stefan Fleck s****k@g****m 486
Stefan Fleck s****k@s****t 291
Michael Chirico c****m@g****m 4
Maximilian Muecke m****n@g****m 4
Murat Tasan m****r@g****m 1
Michel Lang m****g@g****m 1
Garrick Aden-Buie g****k@a****m 1
Aaron Jacobs a****s@c****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 64
  • Total pull requests: 14
  • Average time to close issues: 5 months
  • Average time to close pull requests: 4 months
  • Total issue authors: 20
  • Total pull request authors: 8
  • Average comments per issue: 1.92
  • Average comments per pull request: 1.86
  • Merged pull requests: 12
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 3
  • Pull requests: 5
  • Average time to close issues: 17 days
  • Average time to close pull requests: 3 days
  • Issue authors: 3
  • Pull request authors: 2
  • Average comments per issue: 0.67
  • Average comments per pull request: 1.4
  • Merged pull requests: 5
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • s-fleck (28)
  • Fuco1 (7)
  • mmuurr (6)
  • chintanp (2)
  • selesnow (2)
  • gangstR (2)
  • dselivanov (2)
  • fgvieira (2)
  • mllg (2)
  • jonnybaik (1)
  • m-muecke (1)
  • atheriel (1)
  • sossaman (1)
  • paulponcet (1)
  • sn0001 (1)
Pull Request Authors
  • m-muecke (7)
  • MichaelChirico (6)
  • s-fleck (2)
  • mmuurr (1)
  • gadenbuie (1)
  • atheriel (1)
  • mllg (1)
Top Labels
Issue Labels
enhancement (6) wip (4) documentation (3) faq (1) invalid (1) question (1) wontfix (1)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • cran 60,791 last-month
  • Total docker downloads: 45,081
  • Total dependent packages: 44
    (may contain duplicates)
  • Total dependent repositories: 91
    (may contain duplicates)
  • Total versions: 19
  • Total maintainers: 1
cran.r-project.org: lgr

A Fully Featured Logging Framework

  • Versions: 13
  • Dependent Packages: 36
  • Dependent Repositories: 91
  • Downloads: 60,791 Last month
  • Docker Downloads: 45,081
Rankings
Dependent packages count: 2.2%
Dependent repos count: 2.4%
Downloads: 3.7%
Stargazers count: 4.7%
Average: 7.0%
Forks count: 7.9%
Docker downloads count: 21.1%
Maintainers (1)
Last synced: 6 months ago
conda-forge.org: r-lgr
  • Versions: 6
  • Dependent Packages: 8
  • Dependent Repositories: 0
Rankings
Dependent packages count: 7.1%
Average: 31.1%
Stargazers count: 33.9%
Dependent repos count: 34.0%
Forks count: 49.6%
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.2.0 depends
  • R6 >= 2.4.0 imports
  • cli * suggests
  • covr * suggests
  • crayon * suggests
  • data.table * suggests
  • desc * suggests
  • future * suggests
  • future.apply * suggests
  • glue * suggests
  • jsonlite * suggests
  • knitr * suggests
  • rmarkdown * suggests
  • rotor >= 0.3.5 suggests
  • rprojroot * suggests
  • testthat * suggests
  • tibble * suggests
  • tools * suggests
  • utils * suggests
  • whoami * suggests
  • yaml * suggests
.github/workflows/pkgdown.yaml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc v1 composite
  • r-lib/actions/setup-r v1 composite