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

[pull] master from daroczig:master #33

Merged
merged 14 commits into from
Aug 6, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
docs/
README.html

/.quarto/
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RoxygenNote: 7.3.2
License: AGPL-3
Imports:
utils
Depends: R (>= 4.0.0)
Suggests:
glue,
pander,
Expand Down
35 changes: 16 additions & 19 deletions R/color.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#' Colorize string by the related log level
#' Color string by the related log level
#'
#' Adding color to a string to be used in terminal output. Supports ANSI standard colors 8 or 256.
#' @param msg string
#' Color log messages according to their severity with either a rainbow
#' or grayscale color scheme. The greyscale theme assumes a dark background on
#' the terminal.
#'
#' @param msg String to color.
#' @param level see [log_levels()]
#' @return string with ANSI escape code
#' @return A string with ANSI escape codes.
#' @export
#' @examplesIf requireNamespace("crayon")
#' cat(colorize_by_log_level('foobar', FATAL), '\n')
Expand All @@ -13,6 +16,14 @@
#' cat(colorize_by_log_level('foobar', INFO), '\n')
#' cat(colorize_by_log_level('foobar', DEBUG), '\n')
#' cat(colorize_by_log_level('foobar', TRACE), '\n')
#'
#' cat(grayscale_by_log_level('foobar', FATAL), '\n')
#' cat(grayscale_by_log_level('foobar', ERROR), '\n')
#' cat(grayscale_by_log_level('foobar', WARN), '\n')
#' cat(grayscale_by_log_level('foobar', SUCCESS), '\n')
#' cat(grayscale_by_log_level('foobar', INFO), '\n')
#' cat(grayscale_by_log_level('foobar', DEBUG), '\n')
#' cat(grayscale_by_log_level('foobar', TRACE), '\n')
colorize_by_log_level <- function(msg, level) {

fail_on_missing_package('crayon')
Expand All @@ -33,22 +44,8 @@ colorize_by_log_level <- function(msg, level) {

}


#' Render a string with light/dark gray based on the related log level
#'
#' Adding color to a string to be used in terminal output. Supports ANSI standard colors 8 or 256.
#' @param msg string
#' @param level see [log_levels()]
#' @return string with ANSI escape code
#' @export
#' @examplesIf requireNamespace("crayon")
#' cat(grayscale_by_log_level('foobar', FATAL), '\n')
#' cat(grayscale_by_log_level('foobar', ERROR), '\n')
#' cat(grayscale_by_log_level('foobar', WARN), '\n')
#' cat(grayscale_by_log_level('foobar', SUCCESS), '\n')
#' cat(grayscale_by_log_level('foobar', INFO), '\n')
#' cat(grayscale_by_log_level('foobar', DEBUG), '\n')
#' cat(grayscale_by_log_level('foobar', TRACE), '\n')
#' @rdname colorize_by_log_level
grayscale_by_log_level <- function(msg, level) {

fail_on_missing_package('crayon')
Expand Down
90 changes: 27 additions & 63 deletions R/hooks.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
#' Warn to update R to 4+
#' @keywords internal
warn_if_globalCallingHandlers_is_not_available <- function() {
log_warn(
'Using legacy version of global message/warning/error hook, ',
'please update your R installation to at least 4.0.0 ',
'to make use of the much more elegant globalCallingHandlers approach.')
}


#' Injects a logger call to standard messages
#'
Expand All @@ -17,23 +8,14 @@ warn_if_globalCallingHandlers_is_not_available <- function() {
#' message('hi there')
#' }
log_messages <- function() {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'],
attr, which = 'implements') == 'log_messages')) {
warning('Ignoring this call to log_messages as it was registered previously.')
} else {
globalCallingHandlers(
message = structure(function(m) {
logger::log_level(logger::INFO, m$message, .topcall = m$call)
}, implements = 'log_messages'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'],
attr, which = 'implements') == 'log_messages')) {
warning('Ignoring this call to log_messages as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'message',
exit = substitute(logger::log_info(logger::skip_formatter(cond$message))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
message = structure(function(m) {
logger::log_level(logger::INFO, m$message, .topcall = m$call)
}, implements = 'log_messages'))
}
}

Expand All @@ -48,26 +30,17 @@ log_messages <- function() {
#' for (i in 1:5) { Sys.sleep(runif(1)); warning(i) }
#' }
log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'],
attr, which = 'implements') == 'log_warnings')) {
warning('Ignoring this call to log_warnings as it was registered previously.')
} else {
globalCallingHandlers(
warning = structure(function(m) {
logger::log_level(logger::WARN, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('muffleWarning')
}
}, implements = 'log_warnings'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'],
attr, which = 'implements') == 'log_warnings')) {
warning('Ignoring this call to log_warnings as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'warning',
tracer = substitute(logger::log_warn(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
warning = structure(function(m) {
logger::log_level(logger::WARN, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('muffleWarning')
}
}, implements = 'log_warnings'))
}
}

Expand All @@ -82,26 +55,17 @@ log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) {
#' stop('foobar')
#' }
log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'],
attr, which = 'implements') == 'log_errors')) {
warning('Ignoring this call to log_errors as it was registered previously.')
} else {
globalCallingHandlers(
error = structure(function(m) {
logger::log_level(logger::ERROR, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('abort')
}
}, implements = 'log_errors'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'],
attr, which = 'implements') == 'log_errors')) {
warning('Ignoring this call to log_errors as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'stop',
tracer = substitute(logger::log_error(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
error = structure(function(m) {
logger::log_level(logger::ERROR, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('abort')
}
}, implements = 'log_errors'))
}
}

Expand Down
8 changes: 6 additions & 2 deletions R/layouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,15 @@ layout_glue <- layout_glue_generator()


#' Format a log message with `glue` and ANSI escape codes to add colors
#'
#' Colour log levels based on their severity. Log levels are coloured
#' with [colorize_by_log_level()] and the messages are coloured with
#' [grayscale_by_log_level()].
#'
#' @inheritParams layout_simple
#' @return character vector
#' @export
#' @examples \dontrun{
#' @examplesIf requireNamespace("crayon")
#' log_layout(layout_glue_colors)
#' log_threshold(TRACE)
#' log_info('Starting the script...')
Expand All @@ -182,7 +187,6 @@ layout_glue <- layout_glue_generator()
#' log_debug('Getting an error is usually bad')
#' log_error('This is another problem')
#' log_fatal('The last problem.')
#' }
#' @seealso This is a [log_layout()], for alternatives, see [layout_blank()], [layout_simple()], [layout_glue()], [layout_json()], [layout_json_parser()], or generator functions such as [layout_glue_generator()]
#' @note This functionality depends on the \pkg{crayon} package.
layout_glue_colors <- layout_glue_generator(
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,7 @@ catch_base_log <- function(
log_appender(orginal_appender, namespace = namespace)
res
}

in_pkgdown <- function() {
identical(Sys.getenv("IN_PKGDOWN"), "true")
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespaces <- new.env()
threshold = as.loglevel(Sys.getenv('LOGGER_LOG_LEVEL', unset = 'INFO')),
layout = layout_simple,
formatter = formatter_sprintf,
appender = appender_console))
appender = if (in_pkgdown()) appender_stdout else appender_console))

if (requireNamespace('glue', quietly = TRUE)) {
log_formatter(formatter_glue, namespace = 'global', index = 1)
Expand Down
10 changes: 7 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ for (letter in letters) {
log_warn('There might be many, like {1:2} or more warnings!!!')
```

You can even use a custom log layout to render the log records with colors, as you can see in `demo(colors, package = 'logger', echo = FALSE)`:
You can even use a custom log layout to render the log records with colors, as you can see in `layout_glue_colors()`:

<img src="man/figures/colors.png" alt="colored log output">

Expand Down Expand Up @@ -100,10 +100,10 @@ Why use logger? I decided to write the `n+1`th extensible `log4j` logger that fi

Welcome to the [Bazaar](https://en.wikipedia.org/wiki/The_Cathedral_and_the_Bazaar)! If you already use any of the above packages for logging, you might find `vignette("migration")` useful.

::: .pkgdown-hide

## Interested in more details?

::: .pkgdown-hide

Check out the main documentation site at <https://daroczig.github.io/logger> or the vignettes on the below topics:

* [Introduction to logger](https://daroczig.github.io/logger/articles/Intro.html)
Expand All @@ -115,3 +115,7 @@ Check out the main documentation site at <https://daroczig.github.io/logger> or
* [Simple Benchmarks on Performance](https://daroczig.github.io/logger/articles/performance.html)

:::

If you prefer visual content, you can watch the video recording of the "Getting things logged" talk at RStudio::conf(2020):

[![Gergely Daroczi presenting "Getting things logged" on using the `logger` R package at the RStudio conference in 2020](https://img.youtube.com/vi/_rUuBbml9dU/0.jpg)](https://www.youtube.com/watch?v=_rUuBbml9dU)
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ messages in ad-hoc and programmatic ways:
library(logger)
log_threshold(DEBUG)
log_info('Script starting up...')
#> INFO [2024-07-31 08:26:30] Script starting up...
#> INFO [2024-08-05 16:05:22] Script starting up...

pkgs <- available.packages()
log_info('There are {nrow(pkgs)} R packages hosted on CRAN!')
#> INFO [2024-07-31 08:26:30] There are 21122 R packages hosted on CRAN!
#> INFO [2024-08-05 16:05:23] There are 21132 R packages hosted on CRAN!

for (letter in letters) {
lpkgs <- sum(grepl(letter, pkgs[, 'Package'], ignore.case = TRUE))
Expand All @@ -59,28 +59,27 @@ for (letter in letters) {
'{lpkgs} R packages including the {shQuote(letter)} letter'
)
}
#> DEBUG [2024-07-31 08:26:30] 10188 R packages including the 'a' letter
#> DEBUG [2024-07-31 08:26:30] 7013 R packages including the 'c' letter
#> DEBUG [2024-07-31 08:26:30] 5750 R packages including the 'd' letter
#> DEBUG [2024-07-31 08:26:30] 10902 R packages including the 'e' letter
#> DEBUG [2024-07-31 08:26:30] 8821 R packages including the 'i' letter
#> DEBUG [2024-07-31 08:26:30] 7055 R packages including the 'l' letter
#> DEBUG [2024-07-31 08:26:30] 7039 R packages including the 'm' letter
#> DEBUG [2024-07-31 08:26:30] 6661 R packages including the 'n' letter
#> DEBUG [2024-07-31 08:26:30] 7859 R packages including the 'o' letter
#> DEBUG [2024-07-31 08:26:30] 6579 R packages including the 'p' letter
#> DEBUG [2024-07-31 08:26:31] 11224 R packages including the 'r' letter
#> DEBUG [2024-07-31 08:26:31] 10292 R packages including the 's' letter
#> DEBUG [2024-07-31 08:26:31] 9526 R packages including the 't' letter
#> DEBUG [2024-08-05 16:05:23] 10194 R packages including the 'a' letter
#> DEBUG [2024-08-05 16:05:23] 7016 R packages including the 'c' letter
#> DEBUG [2024-08-05 16:05:23] 5751 R packages including the 'd' letter
#> DEBUG [2024-08-05 16:05:23] 10908 R packages including the 'e' letter
#> DEBUG [2024-08-05 16:05:23] 8825 R packages including the 'i' letter
#> DEBUG [2024-08-05 16:05:23] 7060 R packages including the 'l' letter
#> DEBUG [2024-08-05 16:05:23] 7045 R packages including the 'm' letter
#> DEBUG [2024-08-05 16:05:23] 6665 R packages including the 'n' letter
#> DEBUG [2024-08-05 16:05:23] 7863 R packages including the 'o' letter
#> DEBUG [2024-08-05 16:05:23] 6582 R packages including the 'p' letter
#> DEBUG [2024-08-05 16:05:23] 11230 R packages including the 'r' letter
#> DEBUG [2024-08-05 16:05:23] 10296 R packages including the 's' letter
#> DEBUG [2024-08-05 16:05:23] 9531 R packages including the 't' letter

log_warn('There might be many, like {1:2} or more warnings!!!')
#> WARN [2024-07-31 08:26:31] There might be many, like 1 or more warnings!!!
#> WARN [2024-07-31 08:26:31] There might be many, like 2 or more warnings!!!
#> WARN [2024-08-05 16:05:23] There might be many, like 1 or more warnings!!!
#> WARN [2024-08-05 16:05:23] There might be many, like 2 or more warnings!!!
```

You can even use a custom log layout to render the log records with
colors, as you can see in
`demo(colors, package = 'logger', echo = FALSE)`:
colors, as you can see in `layout_glue_colors()`:

<img src="man/figures/colors.png" alt="colored log output">

Expand Down Expand Up @@ -137,10 +136,10 @@ Welcome to the
you already use any of the above packages for logging, you might find
`vignette("migration")` useful.

<div class=".pkgdown-hide">

## Interested in more details?

<div class=".pkgdown-hide">

Check out the main documentation site at
<https://daroczig.github.io/logger> or the vignettes on the below
topics:
Expand All @@ -161,3 +160,7 @@ topics:
Performance](https://daroczig.github.io/logger/articles/performance.html)

</div>

If you prefer visual content, you can watch the video recording of the "Getting things logged" talk at RStudio::conf(2020):

[![Gergely Daroczi presenting "Getting things logged" on using the `logger` R package at the RStudio conference in 2020](https://img.youtube.com/vi/_rUuBbml9dU/0.jpg)](https://www.youtube.com/watch?v=_rUuBbml9dU)
1 change: 0 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ reference:
- title: Other helpers
contents:
- colorize_by_log_level
- grayscale_by_log_level
- logger
- delete_logger_index
- "%except%"
Expand Down
1 change: 0 additions & 1 deletion demo/00Index

This file was deleted.

11 changes: 0 additions & 11 deletions demo/colors.R

This file was deleted.

Loading
Loading