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
- timestamp_fmt
- a - characterscalar or a- functionthat accepts a- POSIXctas its single argument
- transform_event
- a - functionthat accepts a- LogEventas its single argument
- transform_event_names
- a named - charactervector or a function that accepts a- charactervector of field names as its single argument.
Methods
Inherited methods
Method new()
Creates a new instance of this R6 class.
Usage
LayoutJson$new(
  toJSON_args = list(auto_unbox = TRUE),
  timestamp_fmt = NULL,
  transform_event = function(event) event[["values"]],
  transform_event_names = NULL,
  excluded_fields = "rawMsg"
)Arguments
- toJSON_args
- a list of arguments passed to - jsonlite::toJSON(),
- timestamp_fmt
- Format to be applied to the timestamp. This is applied after - transform_eventbut- before transform_event_names- NULL: formatting of the timestamp is left to- jsonlite::toJSON(),
- a - characterscalar as for- format.POSIXct(), or
- a - functionthat returns a vector of the same length as its (POSIXct) input. The returned vector can be of any type supported by- jsonlite::toJSON().
 
- transform_event
- a - functionwith a single argument that takes a LogEvent object and returns a- listof values.
- transform_event_names
- NULL: don't process names
- a named - charactervector of the format- new_name = old_name
- or a - functionwith a single mandatory argument that accepts a- charactervector of field names. Applied after- transform_event.
 
- excluded_fields
- A - charactervector of field names to exclude from the final output. Applied after- transform_event_names.
Method parse()
Read and parse file written using this Layout
This can be used by the $data active binding of an Appender
Method read()
Read a file written using this Layout (without parsing)
This can be used by the $show() method of an Appender
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"
)
lo <- LayoutJson$new()
lo$format_event(event)
#> {"level":200,"timestamp":"2025-07-23 06:40:29","logger":"dummy logger","caller":null,"msg":"a test message","custom_field":"LayoutJson can handle arbitrary fields"} 
lo <- LayoutJson$new(
  transform_event_names = toupper,
  excluded_fields = c("RAWMSG", "CALLER"))
lo$format_event(event)
#> {"LEVEL":200,"TIMESTAMP":"2025-07-23 06:40:29","LOGGER":"dummy logger","MSG":"a test message","CUSTOM_FIELD":"LayoutJson can handle arbitrary fields"} 
lo <- LayoutJson$new(
  transform_event = function(e) {
    values <- e$values
    values$msg <- toupper(values$msg)
    values
  },
  timestamp_fmt = "%a %b %d %H:%M:%S %Y",
  excluded_fields = c("RAWMSG", "CALLER"))
lo$format_event(event)
#> {"level":200,"timestamp":"Wed Jul 23 06:40:29 2025","logger":"dummy logger","caller":null,"msg":"A TEST MESSAGE","rawMsg":"a test message","custom_field":"LayoutJson can handle arbitrary fields"}