LayoutJson {lgr} | R Documentation |
Format LogEvents as JSON
Description
A format for formatting LogEvents as jsonlines log files. This provides a nice balance between human- an machine-readability.
Super class
lgr::Layout
-> LayoutJson
Active bindings
toJSON_args
a
list
timestamp_fmt
a
character
scalar or afunction
that accepts aPOSIXct
as its single argumenttransform_event
a
function
that accepts aLogEvent
as its single argumenttransform_event_names
a named
character
vector or a function that accepts acharacter
vector of field names as its single argument.
Methods
Public 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_event
butbefore transform_event_names
-
NULL
: 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()
.
-
transform_event
a
function
with a single argument that takes a LogEvent object and returns alist
of values.transform_event_names
-
-
NULL
: don't process names a named
character
vector of the formatnew_name = old_name
or a
function
with a single mandatory argument that accepts acharacter
vector of field names. Applied aftertransform_event
.
-
excluded_fields
A
character
vector of field names to exclude from the final output. Applied aftertransform_event_names
.
Method format_event()
Usage
LayoutJson$format_event(event)
Method set_toJSON_args()
Usage
LayoutJson$set_toJSON_args(x)
Arguments
x
a
list
Method set_timestamp_fmt()
Usage
LayoutJson$set_timestamp_fmt(x)
Arguments
x
a
character
scalar or afunction
that accepts aPOSIXct
as its single argument
Method set_transform_event()
Usage
LayoutJson$set_transform_event(x)
Arguments
x
a
function
that accepts aLogEvent
as its single argument
Method set_transform_event_names()
Usage
LayoutJson$set_transform_event_names(x)
Arguments
x
a named
character
vector or a function that accepts acharacter
vector of field names as its single argument.
Method toString()
Represent the LayoutJson
class as a string
Usage
LayoutJson$toString()
Method parse()
Read and parse file written using this Layout
This can be used by the $data
active binding of an Appender
Usage
LayoutJson$parse(file)
Arguments
file
character
scalar: path to a file
Method read()
Read a file written using this Layout (without parsing)
This can be used by the $show()
method of an Appender
Usage
LayoutJson$read(file, threshold = NA_integer_, n = 20L)
Arguments
file
character
scalar: path to a filethreshold
character
Minimum log level to show. Requires parsing of the log file (but will still display unparsed output)n
integer
number of lines to show
Method clone()
The objects of this class are cloneable with this method.
Usage
LayoutJson$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.
See Also
read_json_lines()
, https://jsonlines.org/
Other Layouts:
Layout
,
LayoutFormat
,
LayoutGlue
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)
lo <- LayoutJson$new(
transform_event_names = toupper,
excluded_fields = c("RAWMSG", "CALLER"))
lo$format_event(event)
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)