Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Imports:
utils
Suggests:
botor,
cli,
covr,
crayon,
devtools,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export(colorize_by_log_level)
export(delete_logger_index)
export(deparse_to_one_line)
export(fail_on_missing_package)
export(formatter_cli)
export(formatter_glue)
export(formatter_glue_or_sprintf)
export(formatter_glue_safe)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* `log_appender()`, `log_layout()` and `log_formatter()` now check that you are calling them with a function, and return the previously set value (#170, @hadley)
* new function to return number of log indices (#194, @WurmPeter)
* `appender_async` is now using `mirai` instead of a custom background process and queue system (#214, @hadley @shikokuchuo)
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)

## Fixes

Expand Down
30 changes: 30 additions & 0 deletions R/formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@
}
attr(formatter_glue_or_sprintf, "generator") <- quote(formatter_glue_or_sprintf())

#' Apply [cli::cli_text()] to format string with cli syntax
#' @param ... passed to [cli::cli_text()] for the text interpolation
#' @inheritParams log_level
#' @return character vector
#' @export
#' @family log_formatters
#' @importFrom utils str
formatter_cli <- function(...,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()) {
fail_on_missing_package("cli")

withCallingHandlers(
cli::cli_fmt(cli::cli_text(..., .envir = .topenv)),
error = function(e) {
args <- paste0(capture.output(str(...)), collapse = "\n")

Check warning on line 192 in R/formatters.R

View check run for this annotation

Codecov / codecov/patch

R/formatters.R#L192

Added line #L192 was not covered by tests

stop(paste0(
"`cli` failed in `formatter_cli` on:\n\n",
args,
"\n\nRaw error message:\n\n",
conditionMessage(e),
"\n\nPlease consider using another `log_formatter` or ",
"`skip_formatter` on strings with curly braces."
))

Check warning on line 201 in R/formatters.R

View check run for this annotation

Codecov / codecov/patch

R/formatters.R#L194-L201

Added lines #L194 - L201 were not covered by tests
}
)
}
attr(formatter_cli, "generator") <- quote(formatter_cli())

#' Transforms all passed R objects into a JSON list
#' @param ... passed to `toJSON` wrapped into a `list`
Expand Down
46 changes: 46 additions & 0 deletions man/formatter_cli.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_glue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_glue_or_sprintf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_glue_safe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_json.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_logging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_pander.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_paste.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/formatter_sprintf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/testthat/test-formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ test_that("glue+sprintf works", {
}
})

test_that("cli works", {
local_test_logger(formatter = formatter_cli)

a <- 43

expect_equal(formatter_cli("Hi"), "Hi")
expect_equal(formatter_cli("{.arg Hi}"), "`Hi`")
expect_equal(formatter_cli("1 + {1}"), "1 + 1")
expect_equal(formatter_cli("{1:2}"), "1 and 2")
expect_equal(formatter_cli("pi is {round(pi, 2)}"), "pi is 3.14")
expect_equal(formatter_cli("Hi {42}"), "Hi 42")
expect_equal(formatter_cli("Hi {1:2}"), paste("Hi 1 and 2"))

expect_output(do.call(logger, namespaces$global[[1]])(INFO, 42), "42")
expect_output(do.call(logger, namespaces$global[[1]])(INFO, "Hi {a}"), "43")

expect_equal(formatter_cli("Hi {a}"), "Hi 43")
expect_output(log_info("Hi {a}"), "43")
expect_output(log_warn("Hi {a}"), "43")
f <- function() log_info("Hi {a}")
expect_output(f(), "43")
})

test_that("formatter_logging works", {
local_test_logger(formatter = formatter_logging)

Expand Down
Loading