AppenderFile {lgr} | R Documentation |
Log to a file
Description
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
.
Super classes
lgr::Filterable
-> lgr::Appender
-> AppenderFile
Active bindings
file
character
scalar. path to the log filedata
data.frame
. Contents offile
parsed to adata.frame
if used with a Layout that supports parsing of log file data (notably LayoutJson). Will throw an error ifLayout
does not support parsing.data
character
scalar. Like$data
, but returns adata.table
instead (requires the data.table package).
Methods
Public methods
Inherited methods
Method new()
Usage
AppenderFile$new( file, threshold = NA_integer_, layout = LayoutFormat$new(), filters = NULL )
Method append()
Usage
AppenderFile$append(event)
Method set_file()
Set a log file
Usage
AppenderFile$set_file(file)
Arguments
file
character
scalar. Path to the log file. Iffile
does not exist it will be created.
Method show()
Display the contents of the log file.
Usage
AppenderFile$show(threshold = NA_integer_, n = 20L)
Arguments
threshold
character
orinteger
scalar. The minimum log level that should be displayed.n
integer
scalar. Show only the lastn
log entries that matchthreshold
.
Super classes
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderFile
-> AppenderJson
Methods
Public methods
Inherited methods
Method new()
Usage
AppenderJson$new( file, threshold = NA_integer_, layout = LayoutJson$new(), filters = NULL )
See Also
Other Appenders:
AppenderBuffer
,
AppenderConsole
,
AppenderFileRotatingDate
,
AppenderFileRotatingTime
,
AppenderFileRotating
,
AppenderTable
,
Appender
Other Appenders:
AppenderBuffer
,
AppenderConsole
,
AppenderFileRotatingDate
,
AppenderFileRotatingTime
,
AppenderFileRotating
,
AppenderTable
,
Appender
Examples
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")
readLines(default)
readLines(fancy)
readLines(json)
# cleanup
lg$config(NULL)
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()
lg$appenders[[1]]$data
# cleanup
lg$config(NULL)
unlink(tf)