Print or Format Logging Data
Usage
# S3 method for class 'LogEvent'
print(
x,
fmt = "%L [%t] %m %f",
timestamp_fmt = "%Y-%m-%d %H:%M:%S",
colors = getOption("lgr.colors"),
log_levels = getOption("lgr.log_levels"),
pad_levels = "right",
excluded_fields = NULL,
...
)
# S3 method for class 'LogEvent'
format(
x,
fmt = "%L [%t] %m %f",
timestamp_fmt = "%Y-%m-%d %H:%M:%S",
colors = NULL,
log_levels = getOption("lgr.log_levels"),
pad_levels = "right",
excluded_fields = NULL,
...
)Arguments
- x
a LogEvent
- fmt
A
characterscalar that may contain any of the tokens listed bellow in the section Format Tokens.- timestamp_fmt
see
format.POSIXct()- colors
A
listoffunctionsthat will be used to color the log levels (likely from crayon::crayon).- log_levels
named
integervector of valid log levels- pad_levels
right,leftorNULL. Whether or not to pad the log level names to the same width on the left or right side, or not at all.- excluded_fields
a
charactervector of fields to exclude from%jand%f- ...
ignored
Format Tokens
%tThe timestamp of the message, formatted according to
timestamp_fmt)%lthe log level, lowercase
characterrepresentation%Lthe log level, uppercase
characterrepresentation%kthe log level, first letter of lowercase
characterrepresentation%Kthe log level, first letter of uppercase
characterrepresentation%nthe log level,
integerrepresentation%gthe name of the logger
%pthe PID (process ID). Useful when logging code that uses multiple threads.
%cthe calling function
%mthe log message
%rthe raw log message (without string interpolation)
%fall custom fields of
xin a pseudo-JSON like format that is optimized for human readability and console output%jall custom fields of
xin proper JSON. This requires that you have jsonlite installed and does not support colors as opposed to%f
Examples
# standard fields can be printed using special tokens
x <- LogEvent$new(
level = 300, msg = "a test event", caller = "testfun()", logger = lgr
)
print(x)
#> WARN [2025-07-23 06:40:37] a test event
print(x, fmt = c("%t (%p) %c: %n - %m"))
#> 2025-07-23 06:40:37 (7318) testfun(): 300 - a test event
print(x, colors = NULL)
#> WARN [2025-07-23 06:40:37] a test event
# custom values
y <- LogEvent$new(
level = 300, msg = "a gps track", logger = lgr,
waypoints = 10, location = "Austria"
)
# default output with %f
print(y)
#> WARN [2025-07-23 06:40:37] a gps track {waypoints: `10`, location: `Austria`}
# proper JSON output with %j
if (requireNamespace("jsonlite")){
print(y, fmt = "%L [%t] %m %j")
}
#> WARN [2025-07-23 06:40:37] a gps track {"waypoints":10,"location":"Austria"}