Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbosity flags in R code #325

Open
AlexAxthelm opened this issue Feb 8, 2024 · 0 comments
Open

Verbosity flags in R code #325

AlexAxthelm opened this issue Feb 8, 2024 · 0 comments
Labels
best-practice Towards best practices r-package Anything r-package r-stats Anything r ropensci Anything rOpenSci

Comments

@AlexAxthelm
Copy link

https://ropensci.org/blog/2024/02/06/verbosity-control-packages/

I like the spirit of this, but disagree with the approach.

Namely, I like the logger approach for package developers (https://daroczig.github.io/logger/articles/r_packages.html), since that gives a very practical way to set different logging levels for different namespaces (packages).

For example, to only enable detailed logging from the workflow.factset package, with the rest of your environment at a higher level, you could have

logger::log_threshold("WARN") # ignore INFO, DEBUG, TRACE globally
logger::log_threshold("TRACE", namespace = "workflow.pacta") # Log everything from this package

This could be easily extended and built into a package infrastructure with something along the lines of

# Not tested
.onLoad <- function(libname, pkgname) {
  log_level <- coalesce(
    options(paste0("loglevel.", pkgname)),
    Sys.getenv(paste0("loglevel_", pkgname)),
    options("loglevel.pacta"),
    Sys.getenv("log_level_pacta"),
    options("loglevel"),
    Sys.getenv("log_level"),
    "INFO"
    logger::log_threshold(log_level, namespace = pkgname)
}

Which would look through a series of options ancd envvars (growing more generic as it looks) until it finds a valid logging level.

About the only thing that I'm not a fan of with logger is that it "doubles up" on the normal R output to stdout if you want ot retain use of the normal warnings and stop functions (for controlling execution), but it does allow for very informative messages (due to using glue by default), and can be combined with the base::warning() / base::stop() if you want:

if (foobar > 5) {
  logger::log_warn("argument \"foobar\" is only supported for values 5 and below. Current value: {foobar}.")
  warning("foobar too high") # could also add custom classes here
}
@jdhoffa jdhoffa added r-package Anything r-package best-practice Towards best practices ropensci Anything rOpenSci r-stats Anything r labels Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
best-practice Towards best practices r-package Anything r-package r-stats Anything r ropensci Anything rOpenSci
Projects
None yet
Development

No branches or pull requests

2 participants