Skip to content

Commit 5b175c4

Browse files
committed
removed needlessly convoluted suspend mechanic
1 parent 9fd3ef5 commit 5b175c4

10 files changed

+54
-96
lines changed

R/Appender.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Appender <- R6::R6Class(
135135
#'
136136
#' logger$add_appender(AppenderConsole$new())
137137
#' logger$add_appender(AppenderConsole$new(
138-
#' layout = LayoutFormat$new("[%t] %c(): [%n] %m from user %u", colors = getOption("lgr.colors"))))
138+
#' layout = LayoutFormat$new("[%t] %c(): [%n] %m", colors = getOption("lgr.colors"))))
139139
#'
140140
#' # Will output the message twice because we attached two console appenders
141141
#' logger$warn("A test message")
@@ -2144,6 +2144,10 @@ AppenderGmail <- R6::R6Class(
21442144
)
21452145
le <- self$buffer_events[[length(self$buffer_events)]]
21462146
title <- self$subject_layout$format_event(le)
2147+
title <- try(
2148+
iconv(title, from = "UTF-8", to = "ASCII//TRANSLIT"),
2149+
silent = TRUE
2150+
)
21472151

21482152
mail <- gmailr::mime()
21492153
mail <- gmailr::to(mail, self$to)

R/Logger.R

+29-73
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ Logger <- R6::R6Class(
253253
timestamp = Sys.time(),
254254
caller = get_caller(-7)
255255
){
256+
if (identical(get(".threshold", envir = private), 0L)) return(NULL)
257+
256258
tryCatch({
257259
# preconditions
258260
level <- standardize_log_levels(level)
@@ -338,6 +340,8 @@ Logger <- R6::R6Class(
338340

339341

340342
fatal = function(msg, ..., caller = get_caller(-8L)){
343+
if (isTRUE(get(".threshold", envir = private) < 100L)) return(NULL)
344+
341345
get("log", envir = self)(
342346
msg = msg,
343347
caller = caller,
@@ -348,6 +352,8 @@ Logger <- R6::R6Class(
348352
},
349353

350354
error = function(msg, ..., caller = get_caller(-8L)){
355+
if (isTRUE(get(".threshold", envir = private) < 200L)) return(NULL)
356+
351357
get("log", envir = self)(
352358
msg = msg,
353359
caller = caller,
@@ -358,6 +364,8 @@ Logger <- R6::R6Class(
358364
},
359365

360366
warn = function(msg, ..., caller = get_caller(-8L)){
367+
if (isTRUE(get(".threshold", envir = private) < 300L)) return(NULL)
368+
361369
get("log", envir = self)(
362370
msg = msg,
363371
caller = caller,
@@ -368,6 +376,8 @@ Logger <- R6::R6Class(
368376
},
369377

370378
info = function(msg, ..., caller = get_caller(-8L)){
379+
if (isTRUE(get(".threshold", envir = private) < 400L)) return(NULL)
380+
371381
get("log", envir = self)(
372382
msg = msg,
373383
caller = caller,
@@ -378,6 +388,8 @@ Logger <- R6::R6Class(
378388
},
379389

380390
debug = function(msg, ..., caller = get_caller(-8L)){
391+
if (isTRUE(get(".threshold", envir = private) < 500L)) return(NULL)
392+
381393
get("log", envir = self)(
382394
msg = msg,
383395
caller = caller,
@@ -388,6 +400,8 @@ Logger <- R6::R6Class(
388400
},
389401

390402
trace = function(msg, ..., caller = get_caller(-8L)){
403+
if (isTRUE(get(".threshold", envir = private) < 600L)) return(NULL)
404+
391405
get("log", envir = self)(
392406
msg = msg,
393407
caller = caller,
@@ -469,10 +483,6 @@ Logger <- R6::R6Class(
469483

470484
set_threshold = function(level){
471485
level <- standardize_threshold(level)
472-
473-
private$unsuspend()
474-
private$suspend(level, inclusive = FALSE)
475-
476486
private$.threshold <- as.integer(level)
477487
invisible(self)
478488
},
@@ -549,82 +559,14 @@ Logger <- R6::R6Class(
549559
# private -----------------------------------------------------------------
550560
private = list(
551561

552-
# +- methods --------------------------------------------------------------
553-
suspend = function(
554-
threshold = 0,
555-
inclusive = TRUE
556-
){
557-
assert(is_scalar_bool(inclusive))
558-
threshold <- standardize_threshold(threshold)
559-
560-
if (is.na(threshold))
561-
threshold <- Inf
562-
563-
if (inclusive){
564-
ll <- private$.log_levels[private$.log_levels >= threshold]
565-
} else {
566-
ll <- private$.log_levels[private$.log_levels > threshold]
567-
}
568-
569-
for (i in seq_along(ll)){
570-
nm <- names(ll)[[i]]
571-
lvl <- ll[[i]]
572-
private$suspended_loggers[[nm]] <- self[[nm]]
573-
unlockBinding(nm, env = self)
574-
self[[nm]] <- function(...) NULL
575-
lockBinding(nm, env = self)
576-
}
577-
invisible(self)
578-
},
579-
580-
581-
unsuspend = function(
582-
threshold = 0,
583-
inclusive = TRUE
584-
){
585-
assert(is_scalar_bool(inclusive))
586-
threshold <- standardize_threshold(threshold)
587-
if (is.na(threshold))
588-
threshold <- Inf
589-
590-
if (inclusive){
591-
ll <- private$.log_levels[private$.log_levels >= threshold]
592-
} else {
593-
ll <- private$.log_levels[private$.log_levels > threshold]
594-
}
595-
596-
for (i in seq_along(private$suspended_loggers)){
597-
nm <- names(private$suspended_loggers)[[i]]
598-
unlockBinding(nm, env = self)
599-
self[[nm]] <- private$suspended_loggers[[nm]]
600-
lockBinding(nm, env = self)
601-
}
602-
private$suspended_loggers <- list()
603-
invisible(self)
604-
},
605-
606-
607562
# +- fields ---------------------------------------------------------------
608563
.propagate = NULL,
609564
.exception_handler = NULL,
610565
.name = NULL,
611566
.parent = NULL,
612567
.appenders = NULL,
613568
.threshold = NA_integer_,
614-
.last_event = NULL,
615-
616-
# intentionaly hardcoded and not using the global options. this is used to
617-
# track which logging functions are available for the logger. used by
618-
# suspend/unsuspend
619-
.log_levels = as_log_levels(c(
620-
"fatal" = 100L,
621-
"error" = 200L,
622-
"warn" = 300L,
623-
"info" = 400L,
624-
"debug" = 500L,
625-
"trace" = 600L
626-
)),
627-
suspended_loggers = list()
569+
.last_event = NULL
628570
)
629571
)
630572

@@ -642,6 +584,8 @@ LoggerGlue <- R6::R6Class(
642584
public = list(
643585

644586
fatal = function(..., caller = get_caller(-8L), .envir = parent.frame()){
587+
if (isTRUE(get(".threshold", envir = private) < 100L)) return(NULL)
588+
645589
force(.envir)
646590
get("log", envir = self)(
647591
...,
@@ -653,6 +597,8 @@ LoggerGlue <- R6::R6Class(
653597
},
654598

655599
error = function(..., caller = get_caller(-8L), .envir = parent.frame()){
600+
if (isTRUE(get(".threshold", envir = private) < 200L)) return(NULL)
601+
656602
get("log", envir = self)(
657603
...,
658604
caller = caller,
@@ -663,6 +609,8 @@ LoggerGlue <- R6::R6Class(
663609
},
664610

665611
warn = function(..., caller = get_caller(-8L), .envir = parent.frame()){
612+
if (isTRUE(get(".threshold", envir = private) < 300L)) return(NULL)
613+
666614
get("log", envir = self)(
667615
...,
668616
caller = caller,
@@ -673,6 +621,8 @@ LoggerGlue <- R6::R6Class(
673621
},
674622

675623
info = function(..., caller = get_caller(-8L), .envir = parent.frame()){
624+
if (isTRUE(get(".threshold", envir = private) < 400L)) return(NULL)
625+
676626
get("log", envir = self)(
677627
...,
678628
caller = caller,
@@ -683,6 +633,8 @@ LoggerGlue <- R6::R6Class(
683633
},
684634

685635
debug = function(..., caller = get_caller(-8L), .envir = parent.frame()){
636+
if (isTRUE(get(".threshold", envir = private) < 500L)) return(NULL)
637+
686638
force(.envir)
687639
get("log", envir = self)(
688640
...,
@@ -694,6 +646,8 @@ LoggerGlue <- R6::R6Class(
694646
},
695647

696648
trace = function(..., caller = get_caller(-8L), .envir = parent.frame()){
649+
if (isTRUE(get(".threshold", envir = private) < 600L)) return(NULL)
650+
697651
force(.envir)
698652
get("log", envir = self)(
699653
...,
@@ -711,6 +665,8 @@ LoggerGlue <- R6::R6Class(
711665
caller = get_caller(-7),
712666
.envir = parent.frame()
713667
){
668+
if (identical(get(".threshold", envir = private), 0L)) return(NULL)
669+
714670
force(.envir)
715671
tryCatch({
716672
# preconditions

R/log_levels.R

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ as_log_levels <- function(x){
1616

1717
DEFAULT_LOG_LEVELS <- c("fatal", "error", "warn", "info", "debug", "trace")
1818

19+
20+
21+
1922
# manage options ----------------------------------------------------------
2023

2124
#' Manage Log Levels
@@ -95,7 +98,9 @@ add_log_levels <- function(
9598

9699

97100

101+
98102
#' union for named vectors. names of `y` override names of `x`
103+
#' @noRd
99104
named_union <- function(x, y){
100105
assert(identical(length(names(x)), length(x)))
101106
assert(identical(length(names(y)), length(y)))
@@ -107,6 +112,8 @@ named_union <- function(x, y){
107112
}
108113

109114

115+
116+
110117
#' @param level_names a `character` vector of the names of the levels to remove
111118
#' @rdname get_log_levels
112119
#' @export
@@ -129,7 +136,6 @@ remove_log_levels <- function(
129136

130137

131138

132-
133139
# format ------------------------------------------------------------------
134140

135141
fmt_log_levels <- function(
@@ -140,6 +146,7 @@ fmt_log_levels <- function(
140146

141147

142148

149+
143150
#' Colorize Levels
144151
#'
145152
#' @param x `numeric` or `character` levels to be colored. Unlike in many other
@@ -177,6 +184,7 @@ colorize_levels <- function(
177184

178185

179186

187+
180188
# standardize ------------------------------------------------------------
181189

182190
#' Standardize User-Input Log Levels to Their Integer Representation

R/simple_logging.R

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ TRACE <- function(msg, ...){
8181

8282

8383

84+
#' @inheritParams with_log_value
8485
#' @param logfun a `function` for processing the log request, usually
8586
#' `lgr$info()`, `lgr$debug()`, etc... .
8687
#' @param caller a `character` scalar. The name of the calling function

man/AppenderConsole.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/as.data.frame.LogEvent.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/named_union.Rd

-11
This file was deleted.

man/simple_logging.Rd

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/manual_tests/test_AppenderDigest.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# AppenderPushbullet ----------------------------------------------------
22

3+
email <- whoami::email_address()
4+
35
test_that("AppenderPushbullet: ERROR log level triggers push", {
46
skip_if_not_installed("RPushbullet")
57

@@ -25,7 +27,7 @@ test_that("AppenderPushbullet: ERROR log level triggers push", {
2527
l$debug("4")
2628
l$debug("something something 5ish has occurred")
2729

28-
l$fatal("Oh well, here we go", foo = "bar")
30+
l$fatal("pushbullet wörks ÄÖÜßÄ", foo = "bar")
2931
})
3032

3133

@@ -36,7 +38,7 @@ test_that("AppenderSendmail: ERROR log level triggers push", {
3638

3739
skip_if_not_installed("sendmailR")
3840
smtp <- getOption("stat.smtp_server")
39-
email <- whoami::email_address()
41+
4042

4143
if (is.null(smtp)) skip("Please set the 'stat.smtp_server' option")
4244
if (is.null(email)) skip("Cannot determine email addresse")
@@ -82,7 +84,7 @@ test_that("AppenderGmail: ERROR log level triggers push", {
8284
l$info("2")
8385
l$trace("3")
8486
l$debug("4")
85-
l$debug("something something 5ish has occurred")
87+
l$debug("ÄÖÜßÄ")
8688

87-
l$fatal("gmailr works", foo = "bar")
89+
l$fatal("gmailr wörks ÄÖÜßÄ", foo = "bar")
8890
})

tests/testthat/test_Logger.R

-4
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ test_that("setting appender threshold works", {
9898
test_that("suspending loggers works", {
9999
ml <- Logger$new("test_logger")
100100

101-
expect_output(ml$info("blubb"), "blubb")
102-
ml$.__enclos_env__$private$suspend("info", inclusive = TRUE)
103-
expect_silent(ml$info("blubb"))
104-
ml$.__enclos_env__$private$unsuspend("info", inclusive = TRUE)
105101
expect_output(ml$info("blubb"), "blubb")
106102

107103
expect_output(ml$fatal("blubb"), "FATAL")

0 commit comments

Comments
 (0)