| 1 |
context("Layout") |
| 2 |
|
| 3 |
|
| 4 |
tevent <- LogEvent$new( |
| 5 |
logger = Logger$new("dummy"), |
| 6 |
level = 200L, |
| 7 |
timestamp = as.POSIXct(1541175573.9308, origin = "1970-01-01", tz = "UTC"), |
| 8 |
caller = NA_character_, |
| 9 |
msg = "foo bar", |
| 10 |
rawMsg = "foo raw", |
| 11 |
customField = "hashbaz", |
| 12 |
customField2 = "barfoo" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
test_that("Layouts works as expected", { |
| 19 |
lo <- Layout$new() |
| 20 |
expect_match(lo$format_event(tevent), "19:33") |
| 21 |
expect_true(is_scalar_character(lo$format_event(tevent))) |
| 22 |
}) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
test_that("LayoutFormat works as expected", { |
| 27 |
lo <- LayoutFormat$new() |
| 28 |
expect_match(lo$format_event(tevent), "ERROR .*2018-11-02 .*:19:33.*\\.* foo bar") |
| 29 |
|
| 30 |
lo$set_timestamp_fmt("%Y-%m-%d") |
| 31 |
lo$set_fmt("[%t]") |
| 32 |
expect_identical(lo$format_event(tevent), "[2018-11-02]") |
| 33 |
}) |
| 34 |
|
| 35 |
|
| 36 |
test_that("LayoutFormat works as expected with custom fields", { |
| 37 |
lo <- LayoutFormat$new("%r %j") |
| 38 |
expect_identical(lo$format_event(tevent), "foo raw {\"customField\":\"hashbaz\",\"customField2\":\"barfoo\"}") |
| 39 |
|
| 40 |
lo$set_timestamp_fmt("%Y-%m-%d") |
| 41 |
lo$set_fmt("[%t]") |
| 42 |
expect_identical(lo$format_event(tevent), "[2018-11-02]") |
| 43 |
}) |
| 44 |
|
| 45 |
|
| 46 |
test_that("LayoutFormat works as expected with custom fields", { |
| 47 |
lo <- LayoutFormat$new("%r %j", excluded_fields = "customField2") |
| 48 |
expect_identical(lo$format_event(tevent), "foo raw {\"customField\":\"hashbaz\"}") |
| 49 |
|
| 50 |
lo$set_timestamp_fmt("%Y-%m-%d") |
| 51 |
lo$set_fmt("[%t]") |
| 52 |
expect_identical(lo$format_event(tevent), "[2018-11-02]") |
| 53 |
}) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
test_that("LayoutGlue works as expected", { |
| 58 |
# basic formatting works |
| 59 |
lo <- LayoutGlue$new(fmt = "{level} [{timestamp}] {msg}") |
| 60 |
expect_match(lo$format_event(tevent), "^200.*foo bar$") |
| 61 |
|
| 62 |
# active bindings work |
| 63 |
lo <- LayoutGlue$new(fmt = "{level_name} [{timestamp}] {msg}") |
| 64 |
expect_match(lo$format_event(tevent), "^error.*foo bar$") |
| 65 |
|
| 66 |
# functions work |
| 67 |
lo <- LayoutGlue$new(fmt = "{toupper(level_name)} [{timestamp}] {msg}") |
| 68 |
expect_match(lo$format_event(tevent), "^ERROR.*foo bar$") |
| 69 |
|
| 70 |
# default format works |
| 71 |
lo <- LayoutGlue$new(fmt = "{pad_right(colorize_levels(toupper(level_name)), 5)} [{timestamp}] {msg}") |
| 72 |
expect_match(lo$format_event(tevent), "ERROR.*foo bar$") |
| 73 |
|
| 74 |
if (crayon::has_color()){ |
| 75 |
expect_true(crayon::has_style(lo$format_event(tevent))) |
| 76 |
} |
| 77 |
}) |