Skip to contents

Generate log layout function rendering JSON after merging meta fields with parsed list from JSON message

Usage

layout_json_parser(fields = default_fields())

Arguments

fields

character vector of field names to be included in the JSON. If named, the names will be used as field names in the JSON.

Note

This functionality depends on the jsonlite package.

Examples

log_formatter(formatter_json)
log_info(everything = 42)
#> INFO [2025-01-28 13:49:29] {"everything":42}

log_layout(layout_json_parser())
log_info(everything = 42)
#> {"time":"2025-01-28 13:49:29","level":"INFO","ns":"global","ans":"global","topenv":"R_GlobalEnv","fn":"eval","node":"fv-az1370-267","arch":"x86_64","os_name":"Linux","os_release":"6.8.0-1020-azure","os_version":"#23-Ubuntu SMP Mon Dec  9 16:58:58 UTC 2024","pid":9760,"user":"runner","everything":42}

log_layout(layout_json_parser(fields = c("time", "node")))
log_info(cars = row.names(mtcars), species = unique(iris$Species))
#> {"time":"2025-01-28 13:49:29","node":"fv-az1370-267","cars":["Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive","Hornet Sportabout","Valiant","Duster 360","Merc 240D","Merc 230","Merc 280","Merc 280C","Merc 450SE","Merc 450SL","Merc 450SLC","Cadillac Fleetwood","Lincoln Continental","Chrysler Imperial","Fiat 128","Honda Civic","Toyota Corolla","Toyota Corona","Dodge Challenger","AMC Javelin","Camaro Z28","Pontiac Firebird","Fiat X1-9","Porsche 914-2","Lotus Europa","Ford Pantera L","Ferrari Dino","Maserati Bora","Volvo 142E"],"species":["setosa","versicolor","virginica"]}

log_layout(layout_json_parser(fields = c(timestamp = "time", "node")))
log_info(
  message = paste(
    "Compared to the previous example,
    the 'time' field is renamed to 'timestamp'"
  )
)
#> {"timestamp":"2025-01-28 13:49:29","node":"fv-az1370-267","message":"Compared to the previous example,\n    the 'time' field is renamed to 'timestamp'"}