| 1 |
#' Logger Tree |
| 2 |
#' |
| 3 |
#' Displays a tree structure of all registered Loggers. |
| 4 |
#' |
| 5 |
#' @section Symbology: |
| 6 |
#' |
| 7 |
#' * unconfigured Loggers are displayed in gray (if your terminal supports |
| 8 |
#' colors and you have the package \pkg{crayon} installed). |
| 9 |
#' * If a logger's `threshold` is set, it is displayed in square brackets next |
| 10 |
#' to its name (reminder: if the threshold is not set, it is inherited from |
| 11 |
#' next logger up the logger tree). |
| 12 |
#' * If a logger's `propagate` field is set to `FALSE` an red hash (`#`) sign |
| 13 |
#' is displayed in front of the logger name, to imply that it does not pass |
| 14 |
#' LogEvents up the tree. |
| 15 |
#' |
| 16 |
#' @return `data.frame` with subclass `"logger_tree"` |
| 17 |
#' |
| 18 |
#' @seealso [logger_index()] for a tidy `data.frame` representation of |
| 19 |
#' all registered loggers |
| 20 |
#' |
| 21 |
#' @export |
| 22 |
#' |
| 23 |
#' @examples |
| 24 |
#' get_logger("fancymodel") |
| 25 |
#' get_logger("fancymodel/shiny")$ |
| 26 |
#' set_propagate(FALSE) |
| 27 |
#' |
| 28 |
#' get_logger("fancymodel/shiny/ui")$ |
| 29 |
#' set_appenders(AppenderConsole$new()) |
| 30 |
#' |
| 31 |
#' get_logger("fancymodel/shiny/server")$ |
| 32 |
#' set_appenders(list(AppenderConsole$new(), AppenderConsole$new()))$ |
| 33 |
#' set_threshold("trace") |
| 34 |
#' |
| 35 |
#' get_logger("fancymodel/plumber") |
| 36 |
#' |
| 37 |
#' if (requireNamespace("cli")){ |
| 38 |
#' print(logger_tree()) |
| 39 |
#' } |
| 40 |
#' |
| 41 |
logger_tree <- function( |
| 42 |
){ |
| 43 |
names <- ls(envir = loggers) |
| 44 |
nodes <- lapply(names, function(.x) unlist(strsplit(.x, "/"))) |
| 45 |
max_depth <- seq_len(max(vapply(nodes, length, integer(1)))) |
| 46 |
second_tier <- setdiff(vapply(nodes, `[[`, character(1), 1L), "root") |
| 47 |
|
| 48 |
res <- data.frame( |
| 49 |
parent = "root", |
| 50 |
children = I(list(unique(second_tier))), |
| 51 |
configured = TRUE, |
| 52 |
threshold = get_logger()$threshold, |
| 53 |
threshold_inherited = FALSE, |
| 54 |
propagate = TRUE, |
| 55 |
n_appenders = length(get_logger()$appenders), |
| 56 |
stringsAsFactors = FALSE |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
for (i in seq_along(nodes)){ |
| 61 |
for (j in seq_along(nodes[[i]])){ |
| 62 |
parent_cur <- nodes[[i]][[j]] |
| 63 |
|
| 64 |
if (j < length(nodes[[i]])){ |
| 65 |
child_cur <- nodes[[i]][[j + 1]] |
| 66 |
} else { |
| 67 |
child_cur <- NULL |
| 68 |
} |
| 69 |
|
| 70 |
if (parent_cur %in% res$parent){ |
| 71 |
sel <- res$parent == parent_cur |
| 72 |
res$children[sel] <- I(list(unique(unlist(compact(c(res$children[sel], child_cur)))))) |
| 73 |
|
| 74 |
} else { |
| 75 |
logger_name <- paste(nodes[[i]][seq_len(j)], collapse = "/") |
| 76 |
cur_logger <- get_logger(logger_name) |
| 77 |
|
| 78 |
res <- rbind( |
| 79 |
res, |
| 80 |
data.frame( |
| 81 |
parent = parent_cur, |
| 82 |
children = I(list(child_cur)), |
| 83 |
configured = !is_virgin_Logger(logger_name), |
| 84 |
threshold = get_logger(logger_name)$threshold, |
| 85 |
threshold_inherited = is_threshold_inherited(cur_logger), |
| 86 |
propagate = cur_logger$propagate, |
| 87 |
n_appenders = length(cur_logger$appenders) |
| 88 |
) |
| 89 |
) |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
assert(all_are_distinct(res$parent)) |
| 95 |
structure( |
| 96 |
res, |
| 97 |
class = union("logger_tree", class(res)) |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#' Print Logger Trees |
| 105 |
#' |
| 106 |
#' @param x a [logger_tree][logger_tree()] |
| 107 |
#' @param color `logical` scalar. If `TRUE` terminal output is colorized via |
| 108 |
#' the package \pkg{crayon}? |
| 109 |
#' @param ... passed on to [cli::tree()] |
| 110 |
#' @return `x` (invisibly) |
| 111 |
#' @export |
| 112 |
print.logger_tree <- function( |
| 113 |
x, |
| 114 |
color = requireNamespace("crayon", quietly = TRUE), |
| 115 |
... |
| 116 |
){ |
| 117 |
if (requireNamespace("cli", quietly = TRUE)){ |
| 118 |
cat(format(x, color = color, ...), sep = "\n") |
| 119 |
} else { |
| 120 |
warning( |
| 121 |
"Console output of logger trees requires the package 'cli'. You can", |
| 122 |
"install it with `install.packages(\"cli\")`." |
| 123 |
) |
| 124 |
print(as.data.frame(x)) |
| 125 |
} |
| 126 |
invisible(x) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#' @rdname print.logger_tree |
| 133 |
#' @export |
| 134 |
format.logger_tree <- function( |
| 135 |
x, |
| 136 |
color = FALSE, |
| 137 |
... |
| 138 |
){ |
| 139 |
assert_namespace("cli") |
| 140 |
|
| 141 |
if (!color){ |
| 142 |
style_fatal <- identity |
| 143 |
style_color <- identity |
| 144 |
} |
| 145 |
|
| 146 |
label <- ifelse(x$configured, x$parent, style_subtle(x$parent)) |
| 147 |
label <- ifelse( |
| 148 |
x$threshold_inherited, |
| 149 |
label, |
| 150 |
paste0(label, " [", label_levels(x$threshold), "]") |
| 151 |
) |
| 152 |
label <- ifelse( |
| 153 |
x$n_appenders == 0, |
| 154 |
label, |
| 155 |
paste0(label, " -> ", x$n_appenders, " appender", ifelse(x$n_appenders > 1, "s", "")) |
| 156 |
) |
| 157 |
label <- ifelse( |
| 158 |
x$propagate, |
| 159 |
label, |
| 160 |
paste0(style_fatal("#"), label) |
| 161 |
) |
| 162 |
|
| 163 |
x_print <- data.frame( |
| 164 |
parent = x$parent, |
| 165 |
children = x$children, |
| 166 |
label = label |
| 167 |
) |
| 168 |
|
| 169 |
format(cli::tree(x_print, root = "root", ...)) |
| 170 |
} |