AppenderPool | R Documentation |
Log to a database table using a connection pool from the pool package.
This provides better performance and connection management compared to
direct DBI connections, especially for applications with concurrent users.
Like AppenderDbi, it does not support case sensitive / quoted column
names, and you are advised to only use all-lowercase names for
custom fields (see ...
argument of lgr::LogEvent).
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
Using connection pools instead of direct DBI connections provides several advantages:
Connections are reused rather than created for each query
Connection management is automated (creation, validation, destruction)
Better handles concurrent requests in multi-user applications
Improves overall performance by reducing connection overhead
Like AppenderDbi, AppenderPool supports buffered logging by setting buffer_size
to something greater than 0
. This buffer is written to the database whenever it is full
(buffer_size
), whenever a LogEvent with a level of fatal
or error
is
encountered (flush_threshold
), or when the Appender is garbage collected
(flush_on_exit
).
An AppenderPool is linked to a database table via its table
argument. If the
table does not exist it is created either when the Appender is first
instantiated or when the first LogEvent would be written to that table.
It is recommended to create the target table first using an SQL CREATE TABLE
statement for more control and safety.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> AppenderPool
pool
a pool connection
close_on_exit
TRUE
or FALSE
. Close the pool connection
when the Logger is removed? Usually not necessary as pools manage their own lifecycle.
col_types
a named character
vector providing information about the
column types in the database.
table
a character
scalar or a DBI::Id specifying the target
database table
table_name
character
scalar. Like $table
, but always returns a
character
scalar
table_id
DBI::Id
. Like $table
, but always returns a DBI::Id
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$format()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
new()
AppenderPool$new( pool, table, threshold = NA_integer_, layout = select_dbi_layout(pool::poolCheckout(pool), table), close_on_exit = FALSE, buffer_size = 0, flush_threshold = "error", flush_on_exit = TRUE, flush_on_rotate = TRUE, should_flush = NULL, filters = NULL )
pool, table
see section Fields
threshold, flush_threshold, layout, buffer_size
see lgr::AppenderBuffer
set_close_on_exit()
AppenderPool$set_close_on_exit(x)
set_pool()
AppenderPool$set_pool(pool)
show()
AppenderPool$show(threshold = NA_integer_, n = 20)
flush()
AppenderPool$flush()
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSendmail
,
AppenderSyslog
if (requireNamespace("RSQLite") && requireNamespace("pool")){
pool <- pool::dbPool(
drv = RSQLite::SQLite(),
dbname = ":memory:"
)
app <- AppenderPool$new(
pool = pool,
table = "log"
)
lg <- lgr::get_logger("test/pool")$
add_appender(app, "db")$
set_propagate(FALSE)
lg$info("test")
print(lg$appenders[[1]]$data)
invisible(lg$config(NULL)) # cleanup
pool::poolClose(pool)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.