A simple Appender that outputs to a file in the file system. If you plan
to log to text files, consider logging to JSON files and take a look at
AppenderJson, which is a shortcut for AppenderFile
preconfigured with
LayoutJson
.
Other Appenders:
AppenderBuffer
,
AppenderConsole
,
AppenderFileRotatingDate
,
AppenderFileRotatingTime
,
AppenderFileRotating
,
AppenderTable
,
Appender
Other Appenders:
AppenderBuffer
,
AppenderConsole
,
AppenderFileRotatingDate
,
AppenderFileRotatingTime
,
AppenderFileRotating
,
AppenderTable
,
Appender
lgr::Filterable
-> lgr::Appender
-> AppenderFile
file
character
scalar. path to the log file
data
data.frame
. Contents of file
parsed to a
data.frame
if used with a Layout that supports parsing of log
file data (notably LayoutJson). Will throw an error if Layout
does
not support parsing.
data
character
scalar. Like $data
, but returns a data.table
instead (requires the data.table package).
new()
AppenderFile$new(
file,
threshold = NA_integer_,
layout = LayoutFormat$new(),
filters = NULL
)
append()
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderFile
-> AppenderJson
new()
AppenderJson$new(
file,
threshold = NA_integer_,
layout = LayoutJson$new(),
filters = NULL
)
lg <- get_logger("test")
default <- tempfile()
fancy <- tempfile()
json <- tempfile()
lg$add_appender(AppenderFile$new(default), "default")
lg$add_appender(
AppenderFile$new(fancy, layout = LayoutFormat$new("[%t] %c(): %L %m")), "fancy"
)
lg$add_appender(
AppenderFile$new(json, layout = LayoutJson$new()), "json"
)
lg$info("A test message")
#> INFO [20:04:22.082] A test message
readLines(default)
#> [1] "INFO [2023-03-04 20:04:22.082] A test message"
readLines(fancy)
#> [1] "[2023-03-04 20:04:22.082] eval(): INFO A test message"
readLines(json)
#> [1] "{\"level\":400,\"timestamp\":\"2023-03-04 20:04:22\",\"logger\":\"test\",\"caller\":\"eval\",\"msg\":\"A test message\"}"
# cleanup
lg$config(NULL)
#> <Logger> [info] test
#>
#> inherited appenders:
#> console: <AppenderConsole> [all] -> console
unlink(default)
unlink(fancy)
unlink(json)
tf <- tempfile()
lg <- get_logger("test")$
set_appenders(AppenderJson$new(tf))$
set_propagate(FALSE)
lg$info("A test message")
lg$info("A test message %s strings", "with format strings", and = "custom_fields")
lg$appenders[[1]]$show()
#> {"level":400,"timestamp":"2023-03-04 20:04:22","logger":"test","caller":"eval","msg":"A test message"}
#> {"level":400,"timestamp":"2023-03-04 20:04:22","logger":"test","caller":"eval","msg":"A test message with format strings strings","and":"custom_fields"}
lg$appenders[[1]]$data
#> level timestamp logger caller
#> 1 400 2023-03-04 20:04:22 test eval
#> 2 400 2023-03-04 20:04:22 test eval
#> msg and
#> 1 A test message <NA>
#> 2 A test message with format strings strings custom_fields
# cleanup
lg$config(NULL)
#> <Logger> [info] test
#>
#> inherited appenders:
#> console: <AppenderConsole> [all] -> console
unlink(tf)