| 1 |
context("Appender") |
| 2 |
|
| 3 |
|
| 4 |
event <- LogEvent$new( |
| 5 |
logger = Logger$new("dummy"), |
| 6 |
level = 200L, |
| 7 |
timestamp = structure(1541175573.9308, class = c("POSIXct", "POSIXt")), |
| 8 |
caller = NA_character_, |
| 9 |
msg = "foo bar", |
| 10 |
rawMsg = "foo raw" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
# Appender -------------------------------------------------------------------- |
| 17 |
test_that("Appender: $append() works", { |
| 18 |
app <- Appender$new() |
| 19 |
expect_match(app$append(event), "foo bar") |
| 20 |
}) |
| 21 |
|
| 22 |
|
| 23 |
test_that("Appender: $set_threshold() works", { |
| 24 |
app <- Appender$new() |
| 25 |
|
| 26 |
app$set_threshold(200) |
| 27 |
expect_identical(app$threshold, 200L) |
| 28 |
app$set_threshold("info") |
| 29 |
expect_identical(app$threshold, 400L) |
| 30 |
app$set_threshold(NA) |
| 31 |
expect_identical(app$threshold, NA_integer_) |
| 32 |
expect_error(app$set_threshold("blubb"), "log levels") |
| 33 |
}) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
# AppenderFile --------------------------------------------------------- |
| 39 |
test_that("AppenderFile: logging with LayoutFormat", { |
| 40 |
tf <- tempfile() |
| 41 |
on.exit(unlink(tf)) |
| 42 |
|
| 43 |
app <- AppenderFile$new(file = tf) |
| 44 |
app$append(event) |
| 45 |
app$append(event) |
| 46 |
res <- readLines(tf) |
| 47 |
expect_true(grepl("foo", res[[1]])) |
| 48 |
expect_true(grepl("bar", res[[2]])) |
| 49 |
}) |
| 50 |
|
| 51 |
|
| 52 |
test_that("AppenderFile: logging with LayoutJson", { |
| 53 |
tf <- tempfile() |
| 54 |
on.exit(unlink(tf)) |
| 55 |
|
| 56 |
app <- AppenderFile$new(file = tf, layout = LayoutJson$new()) |
| 57 |
app$append(event) |
| 58 |
app$append(event) |
| 59 |
tres <- read_json_lines(tf) |
| 60 |
eres <- data.table::rbindlist(list(event$values, event$values)) |
| 61 |
|
| 62 |
expect_identical(tres[["level"]], eres[["level"]]) |
| 63 |
expect_identical(tres[["msg"]], eres[["msg"]]) |
| 64 |
expect_true(all(is.na(tres[["caller"]])) && all(is.na(eres[["caller"]]))) |
| 65 |
expect_equal(as.POSIXct(tres[["timestamp"]]), eres[["timestamp"]], tolerance = 1) |
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
test_that("AppenderFile: creates empty log file on init", { |
| 70 |
tf <- tempfile() |
| 71 |
on.exit(unlink(tf)) |
| 72 |
|
| 73 |
expect_error(AppenderFile$new( |
| 74 |
file = file.path(tempdir(), "non", "existing", "directory" ) |
| 75 |
)) |
| 76 |
|
| 77 |
AppenderFile$new(file = file.path(tf)) |
| 78 |
expect_true(file.exists(tf)) |
| 79 |
}) |
| 80 |
|
| 81 |
|
| 82 |
test_that("AppenderFile: $show() works", { |
| 83 |
tf <- tempfile() |
| 84 |
on.exit(unlink(tf)) |
| 85 |
|
| 86 |
tf2 <- tempfile() |
| 87 |
on.exit(unlink(tf2), add = TRUE) |
| 88 |
|
| 89 |
lg <- get_logger("test")$ |
| 90 |
set_propagate(FALSE)$ |
| 91 |
set_threshold(NA)$ |
| 92 |
set_appenders(list( |
| 93 |
char = AppenderFile$new(file = tf), |
| 94 |
num = AppenderFile$new(file = tf2, layout = LayoutFormat$new("%n %m")) |
| 95 |
)) |
| 96 |
|
| 97 |
on.exit(get_logger("test", reset = TRUE), add = TRUE) |
| 98 |
|
| 99 |
lg$info("foo bar") |
| 100 |
lg$info("blah blubb") |
| 101 |
lg$warn("warnwarn") |
| 102 |
lg$debug("bugbug") |
| 103 |
|
| 104 |
expect_output({ |
| 105 |
expect_length(lg$appenders$char$show("warn"), 1) |
| 106 |
expect_length(lg$appenders$char$show("info"), 3) |
| 107 |
expect_length(lg$appenders$char$show("debug"), 4) |
| 108 |
|
| 109 |
expect_length(lg$appenders$num$show("warn"), 1) |
| 110 |
expect_length(lg$appenders$num$show("info"), 3) |
| 111 |
expect_length(lg$appenders$num$show("debug"), 4) |
| 112 |
}) |
| 113 |
}) |
| 114 |
|
| 115 |
|
| 116 |
test_that("AppenderFile$data throws an error", { |
| 117 |
tf <- tempfile() |
| 118 |
on.exit(unlink(tf)) |
| 119 |
|
| 120 |
lg <- get_logger("test")$ |
| 121 |
set_propagate(FALSE)$ |
| 122 |
set_threshold(NA)$ |
| 123 |
set_appenders(list(file = AppenderFile$new(file = tf))) |
| 124 |
|
| 125 |
on.exit(get_logger("test", reset = TRUE), add = TRUE) |
| 126 |
|
| 127 |
lg$info("foo bar") |
| 128 |
lg$info("blah blubb") |
| 129 |
|
| 130 |
expect_error(lg$appenders$file$data, class = "CannotParseLogError") |
| 131 |
}) |
| 132 |
|
| 133 |
|
| 134 |
test_that("AppenderFile: creates empty log file on init", { |
| 135 |
tf <- tempfile() |
| 136 |
on.exit(unlink(tf)) |
| 137 |
|
| 138 |
app <- AppenderFile$new(file = tf) |
| 139 |
|
| 140 |
lg <- |
| 141 |
get_logger("lgr/testAppenderFileCreatesEmptyLogFile")$ |
| 142 |
set_appenders(app)$ |
| 143 |
set_propagate(FALSE) |
| 144 |
|
| 145 |
lg$fatal("foo", foo = "bar") |
| 146 |
|
| 147 |
res <- readLines(tf) |
| 148 |
expect_match(res, '"bar"}') |
| 149 |
}) |
| 150 |
|
| 151 |
|
| 152 |
# AppenderJson ------------------------------------------------------------ |
| 153 |
|
| 154 |
test_that("AppenderJson: AppenderFile with LayoutJson$show() and $data() work", { |
| 155 |
tf <- tempfile() |
| 156 |
on.exit(unlink(tf)) |
| 157 |
|
| 158 |
# with default format |
| 159 |
app <- AppenderFile$new(file = tf, layout = LayoutJson$new()) |
| 160 |
|
| 161 |
for (i in 1:10) |
| 162 |
app$append(event) |
| 163 |
|
| 164 |
# show shows the correct number of lines |
| 165 |
r <- utils::capture.output(app$show(n = 3)) |
| 166 |
expect_true(grepl( "(level.*){3}", paste(r, collapse = "\n"))) |
| 167 |
expect_false(grepl("(level.*){4}", paste(r, collapse = "\n"))) |
| 168 |
|
| 169 |
r <- utils::capture.output(app$show(threshold = 100)) |
| 170 |
expect_identical(r, "") |
| 171 |
|
| 172 |
expect_identical(nrow(app$data), 10L) |
| 173 |
}) |
| 174 |
|
| 175 |
|
| 176 |
# AppenderConsole --------------------------------------------------------- |
| 177 |
|
| 178 |
test_that("AppenderConsole: $append() works", { |
| 179 |
app <- AppenderConsole$new() |
| 180 |
expect_match( |
| 181 |
capture.output(app$append(event)), |
| 182 |
"ERROR.*:19:33.*foo.*bar" |
| 183 |
) |
| 184 |
}) |
| 185 |
|
| 186 |
|
| 187 |
test_that("AppenderConsole: $filter() works", { |
| 188 |
app1 <- AppenderConsole$new() |
| 189 |
expect_true(app1$filter(event)) |
| 190 |
app1$set_filters(list(function(event) FALSE)) |
| 191 |
expect_false(app1$filter(event)) |
| 192 |
}) |
| 193 |
|
| 194 |
|
| 195 |
test_that("AppenderConsole - with connection = stderr() - outputs to stderr", { |
| 196 |
app <- AppenderConsole$new(connection = stderr()) |
| 197 |
|
| 198 |
std_out <- textConnection("msg_out", open = "w", local = TRUE) |
| 199 |
sink(std_out, append = TRUE, type = "message") |
| 200 |
on.exit(sink(NULL, type = "message"), add = TRUE) |
| 201 |
|
| 202 |
expect_silent( |
| 203 |
app$append(event) |
| 204 |
) |
| 205 |
|
| 206 |
expect_match( |
| 207 |
msg_out, |
| 208 |
"ERROR.*:19:33.*foo.*bar" |
| 209 |
) |
| 210 |
}) |
| 211 |
|
| 212 |
|
| 213 |
test_that("AppenderConsole: chooses stderr by default when in knitr", { |
| 214 |
opts <- options(knitr.in.progress = TRUE) |
| 215 |
on.exit(options(opts), add = TRUE) |
| 216 |
|
| 217 |
app <- AppenderConsole$new() |
| 218 |
std_out <- textConnection("msg_out", open = "w", local = TRUE) |
| 219 |
sink(std_out, append = TRUE, type = "message") |
| 220 |
on.exit(sink(NULL, type = "message"), add = TRUE) |
| 221 |
|
| 222 |
expect_silent( |
| 223 |
app$append(event) |
| 224 |
) |
| 225 |
|
| 226 |
expect_match( |
| 227 |
msg_out, |
| 228 |
"ERROR.*:19:33.*foo.*bar" |
| 229 |
) |
| 230 |
}) |
| 231 |
|
| 232 |
|
| 233 |
test_that("AppenderConsole: rawMessage visible when set in layout", { |
| 234 |
|
| 235 |
lo <- LayoutFormat$new("%m -- %r") |
| 236 |
app <- AppenderConsole$new(layout = lo) |
| 237 |
|
| 238 |
expect_match( |
| 239 |
capture.output(app$append(event)), |
| 240 |
"foo bar -- foo raw") |
| 241 |
}) |
| 242 |
|
| 243 |
|
| 244 |
# AppenderBuffer ---------------------------------------------------- |
| 245 |
# Tests must be executed in sequence |
| 246 |
# setup |
| 247 |
buffer_log <- tempfile() |
| 248 |
teardown(unlink(buffer_log)) |
| 249 |
l <- Logger$new( |
| 250 |
"dummy", |
| 251 |
appenders = list( |
| 252 |
buffer = AppenderBuffer$new( |
| 253 |
appenders = list(file = AppenderFile$new(file = buffer_log)), |
| 254 |
buffer_size = 10, |
| 255 |
flush_threshold = "fatal" |
| 256 |
) |
| 257 |
), propagate = FALSE |
| 258 |
) |
| 259 |
|
| 260 |
|
| 261 |
test_that("AppenderBuffer: FATAL log level triggers flush", { |
| 262 |
l$info(LETTERS[1:3]) |
| 263 |
expect_length(l$appenders$buffer$buffer_events, 1) |
| 264 |
l$info(LETTERS[4:7]) |
| 265 |
expect_identical(length(l$appenders$buffer$buffer_events), 2L) |
| 266 |
|
| 267 |
# FATAL triggers flush with default filters |
| 268 |
l$fatal(letters[1:3]) |
| 269 |
expect_identical(l$appenders$buffer$buffer_events, event_list()) |
| 270 |
expect_match( |
| 271 |
paste(readLines(buffer_log), collapse = "#"), |
| 272 |
"INFO.*A#INFO.*B#INFO.*C#INFO.*D#INFO.*E#INFO.*F#INFO.*G#FATAL.*a#FATAL.*b#FATAL.*c" |
| 273 |
) |
| 274 |
|
| 275 |
# Does the next flush flush the correct event? |
| 276 |
l$fatal("x") |
| 277 |
expect_identical(length(readLines(buffer_log)), 11L) |
| 278 |
expect_identical(l$appenders$buffer$buffer_events, event_list()) |
| 279 |
expect_match(paste(readLines(buffer_log), collapse = "#"), ".*A#.*B#.*C#.*a#.*b#.*c#.*x") |
| 280 |
}) |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
test_that("AppenderBuffer: buffer cycling triggers flush", { |
| 286 |
replicate(10, l$info("z")) |
| 287 |
expect_identical(length(l$appenders$buffer$buffer_events), 10L) |
| 288 |
l$info(c("y", "y", "y")) |
| 289 |
expect_identical(length(readLines(buffer_log)), 24L) |
| 290 |
expect_identical(l$appenders$buffer$buffer_events, event_list()) |
| 291 |
expect_match( |
| 292 |
paste(readLines(buffer_log), collapse = "#"), |
| 293 |
".*A#.*B#.*C#.*a#.*b#.*c#.*x(.*z.*){10}(.*y.*){3}" |
| 294 |
) |
| 295 |
}) |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
test_that("AppenderBuffer: manual flush trigger works", { |
| 301 |
l$info(c("y", "y", "y")) |
| 302 |
l$appenders$buffer$flush() |
| 303 |
expect_identical(length(readLines(buffer_log)), 27L) |
| 304 |
eres <- readLines(buffer_log) |
| 305 |
expect_match(paste(eres, collapse = "#"), "(.*z.*){10}(.*y.*){6}") |
| 306 |
}) |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
test_that("AppenderBuffer: flush on buffer cycling can be suppressed", { |
| 312 |
eres <- readLines(buffer_log) |
| 313 |
l$appenders$buffer$set_flush_on_rotate(FALSE) |
| 314 |
for (i in 1:15) l$info(i) |
| 315 |
|
| 316 |
# theres a 10% tolerance for flushing if no flush on rotate is set |
| 317 |
expect_true(length(l$appenders$buffer$buffer_events) >= 10L) |
| 318 |
# Nothing should have been flushed to the log file |
| 319 |
expect_identical(readLines(buffer_log), eres) |
| 320 |
}) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
test_that("AppenderBuffer: object destruction triggers flush", { |
| 326 |
# this test also cleans up behind the logger created at the beginning |
| 327 |
# of this section |
| 328 |
l$appenders$buffer$flush() # ensure empty appender |
| 329 |
l$info(c("destruction", "destruction")) |
| 330 |
expect_identical(length(l$appenders$buffer$buffer_events), 1L) |
| 331 |
rm(l, inherits = TRUE) |
| 332 |
gc() |
| 333 |
expect_match( |
| 334 |
paste(readLines(buffer_log), collapse = "#"), |
| 335 |
"(.*destruction){2}" |
| 336 |
) |
| 337 |
try(file.remove(buffer_log), silent = TRUE) |
| 338 |
}) |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
# the following AppenderBuffer tests are self contained again |
| 344 |
test_that("AppenderBuffer: flush on object destruction can be suppressed", { |
| 345 |
# must re-initilaize logger |
| 346 |
buffer_log <- tempfile() |
| 347 |
l <- Logger$new( |
| 348 |
"dummy", |
| 349 |
appenders = list( |
| 350 |
buffer = AppenderBuffer$new( |
| 351 |
appenders = list(file = AppenderFile$new(file = buffer_log)), |
| 352 |
buffer_size = 10 |
| 353 |
) |
| 354 |
), |
| 355 |
propagate = FALSE |
| 356 |
) |
| 357 |
|
| 358 |
l$info(LETTERS[1:3]) |
| 359 |
l$appenders$buffer$set_flush_on_exit(FALSE) |
| 360 |
rm(l) |
| 361 |
gc() |
| 362 |
expect_true(file.exists(buffer_log)) |
| 363 |
expect_equal(file.size(buffer_log), 0) |
| 364 |
|
| 365 |
expect_true(file.remove(buffer_log)) |
| 366 |
}) |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
test_that("AppenderBuffer: $add_appender()/$remove_appender()", { |
| 372 |
sapp <- AppenderBuffer$new() |
| 373 |
app1 <- AppenderConsole$new(threshold = 100) |
| 374 |
app2 <- AppenderConsole$new(threshold = 300) |
| 375 |
|
| 376 |
# add |
| 377 |
expect_silent({ |
| 378 |
sapp$add_appender(app1) |
| 379 |
sapp$add_appender(app2, "blah") |
| 380 |
sapp$add_appender(AppenderBuffer$new(), "blubb") |
| 381 |
}) |
| 382 |
expect_identical(sapp$appenders[[1]], app1) |
| 383 |
expect_identical(sapp$appenders$blah, app2) |
| 384 |
|
| 385 |
# remove |
| 386 |
sapp$remove_appender(1) |
| 387 |
expect_length(sapp$appenders, 2L) |
| 388 |
sapp$remove_appender(c("blah", "blubb")) |
| 389 |
|
| 390 |
# set appenders |
| 391 |
sapp$set_appenders(list(app1, app2)) |
| 392 |
expect_length(sapp$appenders, 2) |
| 393 |
sapp$set_appenders(list(app1, app2)) |
| 394 |
expect_length(sapp$appenders, 2) |
| 395 |
expect_identical(sapp$appenders[[1]], app1) |
| 396 |
expect_identical(sapp$appenders[[2]], app2) |
| 397 |
}) |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
test_that("AppenderBuffer: $show()", { |
| 403 |
l <- Logger$new( |
| 404 |
"buffer test", |
| 405 |
appenders = AppenderBuffer$new(should_flush = NULL), |
| 406 |
propagate = FALSE |
| 407 |
) |
| 408 |
|
| 409 |
l$fatal("foo") |
| 410 |
l$warn("foo", bar = "foo") |
| 411 |
l$info(1:3) |
| 412 |
l$error("and a list column", df = head(iris), env = environment()) |
| 413 |
|
| 414 |
expect_identical(nrow(l$appenders[[1]]$buffer_df), 6L) |
| 415 |
expect_identical(nrow(l$appenders[[1]]$buffer_dt), 6L) |
| 416 |
expect_length(capture.output(l$appenders[[1]]$show(n = 5, threshold = "warn")), 3L) |
| 417 |
}) |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
test_that("AppenderBuffer: cycling works", { |
| 422 |
l <- Logger$new( |
| 423 |
"buffer test", |
| 424 |
appenders = AppenderBuffer$new(buffer_size = 3L, flush_on_rotate = FALSE), |
| 425 |
propagate = FALSE |
| 426 |
) |
| 427 |
|
| 428 |
l$info("test1") |
| 429 |
l$info("test2") |
| 430 |
l$info("test3") |
| 431 |
l$info("test4") |
| 432 |
l$info("test5") |
| 433 |
|
| 434 |
expect_identical( |
| 435 |
sapply(l$appenders[[1]]$buffer_events, `[[`, "msg"), |
| 436 |
paste0("test", 2:5) |
| 437 |
) |
| 438 |
}) |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
test_that("AppenderBuffer: Custom $should_flush works", { |
| 444 |
l <- Logger$new( |
| 445 |
"buffer test", |
| 446 |
appenders = AppenderBuffer$new(), |
| 447 |
propagate = FALSE |
| 448 |
) |
| 449 |
|
| 450 |
# FALSE |
| 451 |
l$appenders[[1]]$set_should_flush(function(event) FALSE) |
| 452 |
l$fatal("test") |
| 453 |
expect_length(l$appenders[[1]]$buffer_events, 1L) |
| 454 |
|
| 455 |
# TRUE |
| 456 |
l$appenders[[1]]$set_should_flush( |
| 457 |
function(event) { |
| 458 |
inherits(event, "LogEvent") && inherits(.obj(), "AppenderBuffer") |
| 459 |
} |
| 460 |
) |
| 461 |
l$fatal("test") |
| 462 |
expect_length(l$appenders[[1]]$buffer_events, 0L) |
| 463 |
|
| 464 |
# Undefined |
| 465 |
l$appenders[[1]]$set_should_flush(function(event) NA) |
| 466 |
expect_warning(l$fatal("test")) |
| 467 |
expect_length(l$appenders[[1]]$buffer_events, 1L) |
| 468 |
l$appenders[[1]]$set_should_flush(function(event) iris) |
| 469 |
expect_warning(l$fatal("test")) |
| 470 |
expect_length(l$appenders[[1]]$buffer_events, 2L) |
| 471 |
|
| 472 |
# illegal filter |
| 473 |
expect_error(l$appenders[[1]]$set_should_flush(mean)) |
| 474 |
}) |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
# self contained buffer tests |
| 480 |
test_that("AppenderBuffer: buffer_size 0 works as expected", { |
| 481 |
l <- Logger$new( |
| 482 |
"0 buffer test", |
| 483 |
appenders = list(buffer = AppenderBuffer$new()$ |
| 484 |
set_appenders(list(file = AppenderFile$new(file = tempfile())))), |
| 485 |
propagate = FALSE |
| 486 |
) |
| 487 |
on.exit(unlink(l$appenders$buffer$appenders$file$file)) |
| 488 |
|
| 489 |
l$appenders$buffer$set_buffer_size(0) |
| 490 |
expect_silent(l$info(LETTERS[1:3])) |
| 491 |
expect_length(readLines(l$appenders$buffer$appenders$file$file), 3L) |
| 492 |
|
| 493 |
expect_silent({ |
| 494 |
expect_identical(nrow(l$appenders$buffer$buffer_df), 0L) |
| 495 |
expect_identical(nrow(l$appenders$buffer$buffer_dt), 0L) |
| 496 |
expect_length(l$appenders$buffer$buffer_events, 0L) |
| 497 |
}) |
| 498 |
}) |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
# utils ------------------------------------------------------------------- |
| 504 |
|
| 505 |
test_that("default_file_reader() works", { |
| 506 |
tf <- tempfile() |
| 507 |
on.exit(unlink(tf)) |
| 508 |
|
| 509 |
expect_error(expect_warning(default_file_reader(tf, threshold = NA, n = 0))) |
| 510 |
|
| 511 |
writeLines(LETTERS, tf) |
| 512 |
expect_identical(default_file_reader(tf, threshold = NA, n = 3), c("X", "Y", "Z")) |
| 513 |
|
| 514 |
writeLines(LETTERS, tf) |
| 515 |
expect_warning(default_file_reader(tf, threshold = 4, n = 3)) |
| 516 |
}) |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
test_that("standardize_should_flush_output() works", { |
| 522 |
expect_identical(standardize_should_flush_output(TRUE), TRUE) |
| 523 |
expect_identical(standardize_should_flush_output(FALSE), FALSE) |
| 524 |
expect_warning( |
| 525 |
expect_identical(standardize_should_flush_output(NA), FALSE), |
| 526 |
class = "ValueIsNotBoolError" |
| 527 |
) |
| 528 |
expect_warning( |
| 529 |
expect_identical(standardize_should_flush_output(iris), FALSE), |
| 530 |
class = "ValueIsNotBoolError" |
| 531 |
) |
| 532 |
}) |