A format for formatting LogEvents as jsonlines log files. This provides a nice balance between human- an machine-readability.
See also
read_json_lines()
, https://jsonlines.org/
Other Layouts:
Layout
,
LayoutFormat
,
LayoutGlue
Super class
lgr::Layout
-> LayoutJson
Active bindings
toJSON_args
a list of values passed on to
jsonlite::toJSON()
timestamp_fmt
Used by
$format_event()
to format timestamps.
Methods
Method new()
Usage
LayoutJson$new(toJSON_args = list(auto_unbox = TRUE), timestamp_fmt = NULL)
Method set_toJSON_args()
Set arguments to pass on to jsonlite::toJSON()
Method set_timestamp_fmt()
Set a format that this Layout will apply to timestamps.
Arguments
x
NULL
(the default): formatting of the timestamp is left tojsonlite::toJSON()
,a
character
scalar as forformat.POSIXct()
, ora
function
that returns a vector of the same length as its (POSIXct) input. The returned vector can be of any type supported byjsonlite::toJSON()
, but should usually becharacter
.
Method toString()
Method parse()
Examples
# setup a dummy LogEvent
event <- LogEvent$new(
logger = Logger$new("dummy logger"),
level = 200,
timestamp = Sys.time(),
caller = NA_character_,
msg = "a test message",
custom_field = "LayoutJson can handle arbitrary fields"
)
# Default settings show all event fals
lo <- LayoutJson$new()
lo$format_event(event)
#> {"level":200,"timestamp":"2025-04-14 20:58:45","logger":"dummy logger","caller":null,"msg":"a test message","custom_field":"LayoutJson can handle arbitrary fields"}