Skip to content

Commit

Permalink
Merge branch 'master' of github.com:daroczig/logger
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Aug 5, 2024
2 parents d2ebdb4 + d1cd0fa commit 0a37b72
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 92 deletions.
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/
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
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
2 changes: 1 addition & 1 deletion 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
37 changes: 18 additions & 19 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
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.

21 changes: 17 additions & 4 deletions man/colorize_by_log_level.Rd

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

30 changes: 0 additions & 30 deletions man/grayscale_by_log_level.Rd

This file was deleted.

8 changes: 5 additions & 3 deletions man/layout_glue_colors.Rd

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

0 comments on commit 0a37b72

Please sign in to comment.