AppenderPool: Log to databases via pool

AppenderPoolR Documentation

Log to databases via pool

Description

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

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

Methods

Public methods

Inherited methods

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
)
Arguments
pool, table

see section Fields

threshold, flush_threshold, layout, buffer_size

see lgr::AppenderBuffer


Method set_close_on_exit()

Usage
AppenderPool$set_close_on_exit(x)

Method set_pool()

Usage
AppenderPool$set_pool(pool)

Method show()

Usage
AppenderPool$show(threshold = NA_integer_, n = 20)

Method flush()

Usage
AppenderPool$flush()

See Also

Other Appenders: AppenderDbi, AppenderDt, AppenderElasticSearch, AppenderGmail, AppenderPushbullet, AppenderSendmail, AppenderSyslog

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)
}

s-fleck/lgrExtra documentation built on June 15, 2025, 1:52 p.m.