From 812430d26d08c6295c92fa016bd4fbb104d8b007 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Sun, 18 Aug 2024 16:40:25 -0700 Subject: [PATCH] Import glue I'm not sure I got all the docs, but hopefully I got most of them. --- DESCRIPTION | 6 +++--- NEWS.md | 1 + R/formatters.R | 8 +++----- R/logger.R | 3 +-- R/zzz.R | 6 +----- _pkgdown.yml | 2 +- man/formatter_glue.Rd | 7 +------ man/logger.Rd | 3 +-- vignettes/customize_logger.Rmd | 2 +- 9 files changed, 13 insertions(+), 25 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 02ae9f35..b4604271 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,9 +14,10 @@ Description: Inspired by the the 'futile.logger' R package and 'logging' License: AGPL-3 URL: https://daroczig.github.io/logger/ BugReports: https://github.com/daroczig/logger/issues -Depends: +Depends: R (>= 4.0.0) Imports: + glue, utils Suggests: botor, @@ -24,7 +25,6 @@ Suggests: covr, crayon, devtools, - glue, jsonlite, knitr, pander, @@ -45,7 +45,7 @@ Enhances: futile.logger, log4r, logging -VignetteBuilder: +VignetteBuilder: knitr Config/testthat/edition: 3 Config/testthat/parallel: TRUE diff --git a/NEWS.md b/NEWS.md index 67e79efe..d5ca1d0c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # logger (development version) +* logger now imports glue, always making `formatter_glue()` the default. * `log_appender()`, `log_layout()` and `log_formatter()` now check that you are calling them with a function, and return the previously set value. # logger 0.3.0 (2024-03-03) diff --git a/R/formatters.R b/R/formatters.R index 11e771f1..eb93ea01 100644 --- a/R/formatters.R +++ b/R/formatters.R @@ -24,18 +24,16 @@ formatter_sprintf <- function(fmt, ..., attr(formatter_sprintf, "generator") <- quote(formatter_sprintf()) #' Apply `glue` to convert R objects into a character vector +#' +#' This is the default formatter. +#' #' @param ... passed to `glue` for the text interpolation #' @inheritParams log_level #' @return character vector #' @export -#' @note Although this is the default log message formatter function, -#' but when \pkg{glue} is not installed, [formatter_sprintf()] -#' will be used as a fallback. #' @family `log_formatters` #' @importFrom utils str formatter_glue <- function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { - fail_on_missing_package("glue") - withCallingHandlers( glue::glue(..., .envir = .topenv), error = function(e) { diff --git a/R/logger.R b/R/logger.R index b28fa624..c7397aa9 100644 --- a/R/logger.R +++ b/R/logger.R @@ -9,8 +9,7 @@ #' #' * [INFO()] (or as per the `LOGGER_LOG_LEVEL` environment variable override) as the log level threshold #' * [layout_simple()] as the layout function showing the log level, timestamp and log message -#' * [formatter_glue()] (or [formatter_sprintf()] if \pkg{glue} is not installed) as the -#' default formatter function transforming the R objects to be logged to a character vector +#' * [formatter_glue()] as the default formatter function transforming the R objects to be logged to a character vector #' * [appender_console()] as the default log record destination #' #' @param threshold omit log messages below this [log_levels()] diff --git a/R/zzz.R b/R/zzz.R index 5bfd3732..5071ea76 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -14,15 +14,11 @@ namespaces_reset <- function() { default = list( threshold = as.loglevel(Sys.getenv("LOGGER_LOG_LEVEL", unset = "INFO")), layout = layout_simple, - formatter = formatter_sprintf, + formatter = formatter_glue, appender = if (in_pkgdown()) appender_stdout else appender_console ) ) - if (requireNamespace("glue", quietly = TRUE)) { - log_formatter(formatter_glue, namespace = "global", index = 1) - } - ## internal namespace for debugging logger namespaces$.logger <- list( default = list( diff --git a/_pkgdown.yml b/_pkgdown.yml index 05e5044e..a88d7dde 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -37,7 +37,7 @@ reference: - title: Formatters desc: > Log formatters control how the inputs to the `log_` functions are converted - to a string. The default is `formatter_glue()` when `glue` is installed. + to a string. The default is `formatter_glue()`. contents: - log_formatter - starts_with("formatter_") diff --git a/man/formatter_glue.Rd b/man/formatter_glue.Rd index 577e660e..a8d69323 100644 --- a/man/formatter_glue.Rd +++ b/man/formatter_glue.Rd @@ -30,12 +30,7 @@ to look up the \code{namespace} as well via \code{logger:::top_env_name}} character vector } \description{ -Apply \code{glue} to convert R objects into a character vector -} -\note{ -Although this is the default log message formatter function, -but when \pkg{glue} is not installed, \code{\link[=formatter_sprintf]{formatter_sprintf()}} -will be used as a fallback. +This is the default formatter. } \seealso{ Other \code{log_formatters}: diff --git a/man/logger.Rd b/man/logger.Rd index 1f87ce1b..77b0fe1d 100644 --- a/man/logger.Rd +++ b/man/logger.Rd @@ -40,8 +40,7 @@ By default, a general logger definition is created when loading the \code{logger \itemize{ \item \code{\link[=INFO]{INFO()}} (or as per the \code{LOGGER_LOG_LEVEL} environment variable override) as the log level threshold \item \code{\link[=layout_simple]{layout_simple()}} as the layout function showing the log level, timestamp and log message -\item \code{\link[=formatter_glue]{formatter_glue()}} (or \code{\link[=formatter_sprintf]{formatter_sprintf()}} if \pkg{glue} is not installed) as the -default formatter function transforming the R objects to be logged to a character vector +\item \code{\link[=formatter_glue]{formatter_glue()}} as the default formatter function transforming the R objects to be logged to a character vector \item \code{\link[=appender_console]{appender_console()}} as the default log record destination } } diff --git a/vignettes/customize_logger.Rmd b/vignettes/customize_logger.Rmd index 9eb7cd98..b40a079e 100644 --- a/vignettes/customize_logger.Rmd +++ b/vignettes/customize_logger.Rmd @@ -114,7 +114,7 @@ log_info("There are {nrow(mtcars)} cars in the mtcars dataset") log_info("2 + 2 = {2+2}") ``` -If you don't like this syntax, or want to save a dependency, you can use other formatter functions as well, such as `?formatter_sprintf` (being the default in eg the [`logging` and `futile.logger` packages](https://daroczig.github.io/logger/articles/migration.html)) or `?formatter_paste`, or [write your own formatter function](https://daroczig.github.io/logger/articles/write_custom_extensions.html) converting R objects into string. +If you don't like this syntax you can use other formatter functions as well, such as `?formatter_sprintf` (being the default in eg the [`logging` and `futile.logger` packages](https://daroczig.github.io/logger/articles/migration.html)) or `?formatter_paste`, or [write your own formatter function](https://daroczig.github.io/logger/articles/write_custom_extensions.html) converting R objects into string. ## Log message layouts