| 1 |
context("AppenderFileRotating") |
| 2 |
|
| 3 |
setup({ |
| 4 |
td <- file.path(tempdir(), "lgr") |
| 5 |
assign("td", td, parent.env(environment())) |
| 6 |
dir.create(td, recursive = TRUE) |
| 7 |
}) |
| 8 |
|
| 9 |
|
| 10 |
teardown({ |
| 11 |
unlink(td, recursive = TRUE) |
| 12 |
}) |
| 13 |
|
| 14 |
# AppenderFileRotating ----------------------------------------------------- |
| 15 |
|
| 16 |
test_that("AppenderFileRotating: works as expected", { |
| 17 |
skip_if_not_installed("rotor", "0.3.0") |
| 18 |
|
| 19 |
if (!is_zipcmd_available()){ |
| 20 |
skip("Test requires a workings system zip command") |
| 21 |
} |
| 22 |
|
| 23 |
tf <- file.path(td, "test.log") |
| 24 |
app <- AppenderFileRotating$new(file = tf, size = "1tb") |
| 25 |
|
| 26 |
lg <- |
| 27 |
lgr::get_logger("test")$ |
| 28 |
set_propagate(FALSE)$ |
| 29 |
set_appenders(app) |
| 30 |
|
| 31 |
on.exit({ |
| 32 |
lg$config(NULL) |
| 33 |
file.remove(tf) |
| 34 |
app$prune(0) |
| 35 |
}) |
| 36 |
|
| 37 |
# appender logs to file |
| 38 |
expect_identical(app, lg$appenders[[1]]) |
| 39 |
lg$fatal("test973") |
| 40 |
expect_true(file.exists(app$file)) |
| 41 |
expect_length(readLines(app$file), 1) |
| 42 |
expect_match(readLines(app$file), "test973") |
| 43 |
|
| 44 |
# first rotate rotates a file with content |
| 45 |
app$rotate(force = TRUE) |
| 46 |
expect_gt(lg$appenders[[1]]$backups[1, ]$size, 0L) |
| 47 |
expect_identical(nrow(lg$appenders[[1]]$backups), 1L) |
| 48 |
expect_length(readLines(app$file), 0) |
| 49 |
expect_match(readLines(app$backups$path[[1]]), "test973") |
| 50 |
|
| 51 |
# second rotate only has a file of size 0 to rotate |
| 52 |
app$rotate(force = TRUE) |
| 53 |
expect_equal(app$backups[1, ]$size, 0) |
| 54 |
expect_identical(nrow(lg$appenders[[1]]$backups), 2L) |
| 55 |
|
| 56 |
# compression works |
| 57 |
lg$fatal("test987") |
| 58 |
lg$appenders[[1]]$set_compression(TRUE) |
| 59 |
lg$appenders[[1]]$rotate(force = TRUE) |
| 60 |
expect_identical(lg$appenders[[1]]$backups$ext, c("log.zip", "log", "log")) |
| 61 |
expect_identical(lg$appenders[[1]]$backups$sfx, as.character(1:3)) |
| 62 |
con <- unz(app$backups$path[[1]], filename = "test.log") |
| 63 |
on.exit(close(con), add = TRUE) |
| 64 |
expect_match(readLines(con), "test987") |
| 65 |
|
| 66 |
# pruning works |
| 67 |
expect_identical(nrow(app$prune(0)$backups), 0L) |
| 68 |
}) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
test_that("AppenderFileRotating: works with different backup_dir", { |
| 73 |
if (!is_zipcmd_available()) { |
| 74 |
skip("Test requires a workings system zip command") |
| 75 |
} |
| 76 |
|
| 77 |
skip_if_not_installed("rotor", "0.3.0") |
| 78 |
|
| 79 |
tf <- file.path(td, "test.log") |
| 80 |
bu_dir <- file.path(td, "backups") |
| 81 |
|
| 82 |
# backup_dir does not exist |
| 83 |
expect_error( |
| 84 |
app <- AppenderFileRotating$new(file = tf, backup_dir = bu_dir), |
| 85 |
class = "DirDoesNotExistError" |
| 86 |
) |
| 87 |
|
| 88 |
# rotating to different dir works |
| 89 |
dir.create(bu_dir) |
| 90 |
app <- AppenderFileRotating$new( |
| 91 |
file = tf, |
| 92 |
backup_dir = bu_dir, |
| 93 |
size = 10 |
| 94 |
) |
| 95 |
lg <- get_logger("test")$ |
| 96 |
set_propagate(FALSE)$ |
| 97 |
add_appender(app) |
| 98 |
|
| 99 |
on.exit({ |
| 100 |
app$prune(0) |
| 101 |
lg$config(NULL) |
| 102 |
unlink(c(tf, bu_dir), recursive = TRUE) |
| 103 |
}) |
| 104 |
|
| 105 |
|
| 106 |
# triggers rotation because resulting file will is bigger than 100 byte |
| 107 |
lg$info(paste(LETTERS, collapse = "-")) |
| 108 |
app$set_compression(TRUE) |
| 109 |
lg$info(paste(letters, collapse = "-")) |
| 110 |
|
| 111 |
# paths are as expected |
| 112 |
expect_equal(file.size(tf), 0) |
| 113 |
expect_equal(list.files(bu_dir), basename(app$backups$path)) |
| 114 |
expect_setequal(app$backups$ext, c("log.zip", "log")) |
| 115 |
expect_match(app$backups$path[[1]], "lgr/backups") |
| 116 |
expect_match(app$backups$path[[2]], "lgr/backups") |
| 117 |
|
| 118 |
# file contents are as expected |
| 119 |
con <- unz(app$backups$path[[1]], filename = "test.log") |
| 120 |
on.exit(close(con), add = TRUE) |
| 121 |
expect_match(readLines(con), "a-b-.*-y-z") |
| 122 |
expect_match(readLines(app$backups$path[[2]]), "A-B-.*-Y-Z") |
| 123 |
|
| 124 |
|
| 125 |
file.remove(app$backups$path) |
| 126 |
}) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
test_that("AppenderFileRotating: `size` argument works as expected", { |
| 132 |
|
| 133 |
skip_if_not_installed("rotor", "0.3.0") |
| 134 |
|
| 135 |
#setup |
| 136 |
tf <- file.path(td, "test.log") |
| 137 |
app <- AppenderFileRotating$new(file = tf)$set_size(-1) |
| 138 |
saveRDS(iris, app$file) |
| 139 |
on.exit({ |
| 140 |
unlink(tf) |
| 141 |
app$prune(0) |
| 142 |
}) |
| 143 |
|
| 144 |
# logic |
| 145 |
app$set_size("3 KiB") |
| 146 |
app$rotate() |
| 147 |
expect_identical(nrow(app$backups), 0L) |
| 148 |
|
| 149 |
app$set_size("0.5 KiB") |
| 150 |
app$rotate() |
| 151 |
expect_identical(nrow(app$backups), 1L) |
| 152 |
expect_gt(app$backups$size[[1]], 10) |
| 153 |
expect_equal(file.size(app$file), 0) |
| 154 |
}) |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
# AppenderFileRotatingDate ---------------------------------------------------- |
| 159 |
|
| 160 |
test_that("AppenderFileRotatingDate: works as expected", { |
| 161 |
if (!is_zipcmd_available()) |
| 162 |
skip("Test requires a workings system zip command") |
| 163 |
|
| 164 |
skip_if_not_installed("rotor", "0.3.0") |
| 165 |
|
| 166 |
tf <- file.path(td, "test.log") |
| 167 |
app <- AppenderFileRotatingDate$new(file = tf, size = "1tb") |
| 168 |
lg <- |
| 169 |
lgr::get_logger("test")$ |
| 170 |
set_propagate(FALSE)$ |
| 171 |
set_appenders(app) |
| 172 |
|
| 173 |
on.exit({ |
| 174 |
lg$config(NULL) |
| 175 |
file.remove(tf) |
| 176 |
app$prune(0) |
| 177 |
}) |
| 178 |
|
| 179 |
# appender logs to file |
| 180 |
expect_identical(app, lg$appenders[[1]]) |
| 181 |
lg$fatal("test341") |
| 182 |
expect_true(file.exists(app$file)) |
| 183 |
expect_length(readLines(app$file), 1) |
| 184 |
expect_match(readLines(app$file), "test341") |
| 185 |
expect_identical(nrow(app$backups), 0L) |
| 186 |
|
| 187 |
# first rotate rotates a file with content |
| 188 |
app$set_size(-1)$set_age("1 day") |
| 189 |
app$rotate(now = as.Date("2019-01-01"), force = TRUE) |
| 190 |
expect_identical(nrow(app$backups), 1L) |
| 191 |
expect_gt(lg$appenders[[1]]$backups[1, ]$size, 0L) |
| 192 |
expect_length(readLines(app$file), 0) |
| 193 |
expect_match(app$backups$path[[1]], "2019-01-01") |
| 194 |
expect_match(readLines(app$backups$path[[1]]), "test341") |
| 195 |
|
| 196 |
# second rotate only has a file of size 0 to rotate |
| 197 |
app$rotate(now = "2019-01-02") |
| 198 |
expect_identical(nrow(app$backups), 2L) |
| 199 |
expect_equal(app$backups[1, ]$size, 0) |
| 200 |
expect_match(app$backups$path[[1]], "2019-01-02") |
| 201 |
|
| 202 |
# compression works |
| 203 |
app$set_age("10000 years") # prevent rotation on log |
| 204 |
lg$fatal("test938") |
| 205 |
app$set_age("1 day") |
| 206 |
lg$appenders[[1]]$set_compression(TRUE) |
| 207 |
lg$appenders[[1]]$rotate(now = as.Date("2019-01-03")) |
| 208 |
expect_identical(nrow(app$backups), 3L) |
| 209 |
expect_identical(app$backups$ext, c("log.zip", "log", "log")) |
| 210 |
expect_identical(lg$appenders[[1]]$backups$sfx, c("2019-01-03", "2019-01-02", "2019-01-01")) |
| 211 |
con <- unz(app$backups$path[[1]], filename = "test.log") |
| 212 |
on.exit(close(con), add = TRUE) |
| 213 |
expect_match(readLines(con), "test938") |
| 214 |
|
| 215 |
# pruning works |
| 216 |
expect_identical(nrow(app$prune(0)$backups), 0L) |
| 217 |
}) |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
test_that("AppenderFileRotatingDate: works with different backup_dir", { |
| 223 |
skip_if_not(is_zipcmd_available(), "Test requires a workings system zip command") |
| 224 |
skip_if_not_installed("rotor", "0.3.0") |
| 225 |
|
| 226 |
tf <- file.path(td, "test.log") |
| 227 |
bu_dir <- file.path(td, "backups") |
| 228 |
|
| 229 |
# backup_dir does not exist |
| 230 |
expect_error( |
| 231 |
app <- AppenderFileRotatingDate$new(file = tf, backup_dir = bu_dir) |
| 232 |
) |
| 233 |
|
| 234 |
# setup |
| 235 |
dir.create(bu_dir) |
| 236 |
app <- AppenderFileRotatingDate$new( |
| 237 |
file = tf, |
| 238 |
backup_dir = bu_dir, |
| 239 |
size = 100 |
| 240 |
) |
| 241 |
lg <- get_logger("test")$set_propagate(FALSE) |
| 242 |
lg$add_appender(app) |
| 243 |
|
| 244 |
on.exit({ |
| 245 |
app$prune(0) |
| 246 |
unlink(c(tf, bu_dir), recursive = TRUE) |
| 247 |
lg$config(NULL) |
| 248 |
}) |
| 249 |
|
| 250 |
|
| 251 |
# rotating to different dir works |
| 252 |
app$set_age(-1)$set_size(-1) |
| 253 |
lg$info(paste(LETTERS)) |
| 254 |
app$set_compression(TRUE) |
| 255 |
lg$info(paste(LETTERS)) |
| 256 |
|
| 257 |
expect_equal(file.size(tf), 0) |
| 258 |
|
| 259 |
expect_equal(list.files(bu_dir), basename(app$backups$path)) |
| 260 |
expect_setequal(app$backups$ext, c("log.zip", "log")) |
| 261 |
file.remove(app$backups$path) |
| 262 |
}) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
test_that("AppenderFileRotatingDate: `size` and `age` arguments work as expected", { |
| 268 |
skip_if_not_installed("rotor", "0.3.0") |
| 269 |
|
| 270 |
#setup |
| 271 |
tf <- file.path(td, "test.log") |
| 272 |
app <- AppenderFileRotatingDate$new(file = tf)$set_age(-1) |
| 273 |
saveRDS(iris, app$file) |
| 274 |
on.exit({ |
| 275 |
unlink(tf) |
| 276 |
app$prune(0) |
| 277 |
}) |
| 278 |
|
| 279 |
# file size to small |
| 280 |
app$set_size("3 KiB") |
| 281 |
app$rotate() |
| 282 |
expect_identical(nrow(app$backups), 0L) |
| 283 |
|
| 284 |
app$set_size("0.5 KiB") |
| 285 |
app$rotate(now = "2999-01-01") |
| 286 |
expect_identical(nrow(app$backups), 1L) |
| 287 |
|
| 288 |
# age to small |
| 289 |
app$set_size(-1) |
| 290 |
app$set_age("1 day") |
| 291 |
app$rotate(now = "2999-01-01") |
| 292 |
expect_identical(nrow(app$backups), 1L) |
| 293 |
|
| 294 |
app$rotate(now = "2999-01-02") |
| 295 |
expect_identical(nrow(app$backups), 2L) |
| 296 |
}) |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
# AppenderFileRotatingTime ---------------------------------------------------- |
| 302 |
|
| 303 |
test_that("AppenderFileRotatingTime: works as expected", { |
| 304 |
skip_if_not(is_zipcmd_available(), "Test requires a workings system zip command") |
| 305 |
skip_if_not_installed("rotor", "0.3.0") |
| 306 |
|
| 307 |
tf <- file.path(td, "test.log") |
| 308 |
app <- AppenderFileRotatingTime$new(file = tf) |
| 309 |
lg <- |
| 310 |
lgr::get_logger("test")$ |
| 311 |
set_propagate(FALSE)$ |
| 312 |
set_appenders(app) |
| 313 |
|
| 314 |
on.exit({ |
| 315 |
app$prune(0) |
| 316 |
lg$config(NULL) |
| 317 |
unlink(tf) |
| 318 |
}) |
| 319 |
|
| 320 |
lg$fatal("test") |
| 321 |
|
| 322 |
# first rotate roates a file with content |
| 323 |
app$set_size(-1) |
| 324 |
app$set_age(-1) |
| 325 |
app$rotate(now = "2999-01-03--12-01") |
| 326 |
expect_gt(app$backups[1, ]$size, 0) |
| 327 |
expect_match(app$backups[1, ]$path, "2999-01-03--12-01-00") |
| 328 |
|
| 329 |
# second rotate only has a file of size 0 to rotate |
| 330 |
lg$appenders[[1]]$rotate(now = "2999-01-03--12-02") |
| 331 |
expect_equal(lg$appenders[[1]]$backups[1, ]$size, 0) |
| 332 |
expect_match(app$backups[1, ]$path, "2999-01-03--12-02-00") |
| 333 |
|
| 334 |
# compression is possible |
| 335 |
lg$appenders[[1]]$set_compression(TRUE) |
| 336 |
lg$appenders[[1]]$rotate(now = "2999-01-03--12-03") |
| 337 |
expect_identical(lg$appenders[[1]]$backups$ext, c("log.zip", "log", "log")) |
| 338 |
expect_match(app$backups[1, ]$path, "2999-01-03--12-03-00.log.zip") |
| 339 |
|
| 340 |
# cleanup |
| 341 |
app$prune(0) |
| 342 |
expect_identical(nrow(app$backups), 0L) |
| 343 |
lg$config(NULL) |
| 344 |
}) |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
test_that("AppenderFileRotatingTime: works with different backup_dir", { |
| 350 |
skip_if_not(is_zipcmd_available(), "Test requires a workings system zip command") |
| 351 |
skip_if_not_installed("rotor", "0.3.0") |
| 352 |
|
| 353 |
tf <- file.path(td, "test.log") |
| 354 |
bu_dir <- file.path(td, "backups") |
| 355 |
|
| 356 |
# backup_dir does not exist |
| 357 |
expect_error( |
| 358 |
app <- AppenderFileRotatingTime$new(file = tf, backup_dir = bu_dir) |
| 359 |
) |
| 360 |
|
| 361 |
# setup |
| 362 |
dir.create(bu_dir) |
| 363 |
app <- AppenderFileRotatingTime$new( |
| 364 |
file = tf, |
| 365 |
backup_dir = bu_dir, |
| 366 |
size = 100, |
| 367 |
age = -1 |
| 368 |
) |
| 369 |
lg <- get_logger("test")$ |
| 370 |
set_propagate(FALSE)$ |
| 371 |
add_appender(app) |
| 372 |
|
| 373 |
on.exit({ |
| 374 |
app$prune(0) |
| 375 |
unlink(c(tf, bu_dir), recursive = TRUE) |
| 376 |
lg$config(NULL) |
| 377 |
}) |
| 378 |
|
| 379 |
|
| 380 |
# rotating to different dir works |
| 381 |
lg$info(paste(LETTERS)) |
| 382 |
app$set_compression(TRUE) |
| 383 |
Sys.sleep(1) # for sort order of backups |
| 384 |
lg$info(paste(LETTERS)) |
| 385 |
|
| 386 |
expect_equal(file.size(tf), 0) |
| 387 |
|
| 388 |
expect_equal(rev(list.files(bu_dir)), basename(app$backups$path)) |
| 389 |
expect_equal(app$backups$ext, c("log.zip", "log")) |
| 390 |
file.remove(app$backups$path) |
| 391 |
}) |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
test_that("AppenderFileRotatingTime: `size` and `age` arguments work as expected", { |
| 397 |
|
| 398 |
skip_if_not_installed("rotor", "0.3.0") |
| 399 |
|
| 400 |
#setup |
| 401 |
tf <- file.path(td, "test.log") |
| 402 |
app <- AppenderFileRotatingTime$new(file = tf)$set_age(-1) |
| 403 |
saveRDS(iris, app$file) |
| 404 |
on.exit({ |
| 405 |
unlink(tf) |
| 406 |
app$prune(0) |
| 407 |
}) |
| 408 |
|
| 409 |
# file size to small |
| 410 |
app$set_size(file.size(tf) + 2) |
| 411 |
app$rotate() |
| 412 |
expect_identical(nrow(app$backups), 0L) |
| 413 |
|
| 414 |
app$set_size(floor(file.size(tf) / 2)) |
| 415 |
app$rotate(now = "2999-01-01") |
| 416 |
expect_identical(nrow(app$backups), 1L) |
| 417 |
|
| 418 |
# age to small |
| 419 |
app$set_size(-1) |
| 420 |
app$set_age("1 day") |
| 421 |
app$rotate(now = "2999-01-01") |
| 422 |
expect_identical(nrow(app$backups), 1L) |
| 423 |
|
| 424 |
app$rotate(now = "2999-01-02") |
| 425 |
expect_identical(nrow(app$backups), 2L) |
| 426 |
}) |
| 427 |
|
| 428 |
|
| 429 |
# Issues ------------------------------------------------------------------ |
| 430 |
|
| 431 |
test_that("AppenderFileRotatingTime: `size` and `age` arguments work as expected #39", { |
| 432 |
skip_if_not_installed("rotor", "0.3.0") |
| 433 |
|
| 434 |
#setup |
| 435 |
tf <- file.path(td, "test.log") |
| 436 |
log_dir <- file.path(td, "backups") |
| 437 |
dir.create(log_dir) |
| 438 |
app <- AppenderFileRotatingTime$new( |
| 439 |
file = tf, |
| 440 |
layout = LayoutJson$new(), |
| 441 |
age = -1, |
| 442 |
size = "0.5 kb", |
| 443 |
max_backups = 5, |
| 444 |
backup_dir = log_dir, |
| 445 |
overwrite = FALSE, |
| 446 |
compression = FALSE, |
| 447 |
threshold = "info" |
| 448 |
) |
| 449 |
|
| 450 |
on.exit({ |
| 451 |
unlink(tf) |
| 452 |
unlink(log_dir, recursive = TRUE) |
| 453 |
app$prune(0) |
| 454 |
}) |
| 455 |
|
| 456 |
lg <- get_logger("test_issue_39")$ |
| 457 |
set_propagate(FALSE)$ |
| 458 |
set_appenders(list(rotating = app)) |
| 459 |
|
| 460 |
# push log messages until rotation is triggered, should only take a few iterations |
| 461 |
for (i in 1:100){ |
| 462 |
lg$info("test") |
| 463 |
if (nrow(lg$appenders$rotating$backups) >= 1) |
| 464 |
break |
| 465 |
} |
| 466 |
|
| 467 |
expect_identical(nrow(lg$appenders$rotating$backups), 1L) |
| 468 |
expect_length(list.files(log_dir), 1) |
| 469 |
}) |
| 470 |
|