Log to databases via pool
AppenderPool.Rd
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).
Value
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
Benefits of Pooled Connections
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
Buffered Logging
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
).
Creating a New Appender
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.
Super classes
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> AppenderPool
Active bindings
pool
close_on_exit
TRUE
orFALSE
. 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 tabletable_name
character
scalar. Like$table
, but always returns acharacter
scalartable_id
Methods
Inherited methods
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()
Method new()
Usage
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
)
Method flush()
Examples
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)
}
#> Loading required namespace: pool
#> creating 'log' with columns: level integer, timestamp TEXT, logger TEXT, caller TEXT, msg TEXT
#> level timestamp logger caller msg
#> 1 400 2025-07-23 12:24:46 test/pool eval test