Format log events for output to databases
LayoutDbi.RdLayoutDbi can contain col_types that AppenderDbi can use to create new
database tables; however, it is safer and more flexible to set up the log
table up manually with an SQL CREATE TABLE statement instead.
Value
The $new() method returns an R6::R6 that inherits from
lgr::Layout and can used as a Layout by an lgr::Appender.
Details
The LayoutDbi parameters fmt, timestamp_fmt, colors and pad_levels
are only applied for for console output via the $show() method and do not
influence database inserts in any way. The inserts are pre-processed by
the methods $format_data(), $format_colnames and $format_tablenames.
It does not format
LogEvents directly, but their data.table representations (see
lgr::as.data.table.LogEvent), as well as column- and table names.
Database Specific Layouts
Different databases have different data types and features. Currently the
following LayoutDbi subclasses exist that deal with specific databases,
but this list is expected to grow as lgrExtra matures:
LayoutSqlite: For SQLite databasesLayoutPostgres: for Postgres databasesLayoutMySql: for MySQL databasesLayoutDb2: for DB2 databases
The utility function select_dbi_layout() tries returns the appropriate
Layout for a DBI connection, but this does not work for odbc and JDBC
connections where you have to specify the layout manually.
For creating custom DB-specific layouts it should usually be enough to create
an R6::R6 class that inherits from LayoutDbi and choosing different
defaults for $format_table_name, $format_colnames and $format_data.
See also
select_dbi_layout(), DBI::DBI,
Other Layout:
LayoutElasticSearch
Super classes
lgr::Layout -> lgr::LayoutFormat -> LayoutDbi
Public fields
format_table_namea
functionto format the table name before inserting to the database. The function will be applied to the$table_namebefore inserting into the database. For example some, databases prefer all lowercase names, some uppercase. SQL updates should be case-agnostic, but sadly in practice not all DBI backends behave consistently in this regard.format_colnamesa
functionto format the column names before inserting to the database. The function will be applied to the column names of the data frame to be inserted into the database.format_dataa
functionto format the data before inserting into the database. The function will be applied to the whole data frame.namesof the columns that contain data that has been serialized to JSON strings
Active bindings
col_typesa named
charactervector of column types supported by the target database. If notNULLthis is used by AppenderDbi or similar Appenders to create a new database table on instantiation of the Appender. If the target database table already exists,col_typesis not used.namesof the columns that contain data that has been serialized to JSON strings
col_namescolumn names of the target table (the same as
names(lo$col_types))
Methods
Method new()
Usage
LayoutDbi$new(
col_types = c(level = "integer", timestamp = "timestamp", logger = "varchar(256)",
caller = "varchar(256)", msg = "varchar(2048)", rawMsg = "varchar(2048)"),
serialized_cols = NULL,
fmt = "%L [%t] %m %f",
timestamp_fmt = "%Y-%m-%d %H:%M:%S",
colors = getOption("lgr.colors", list()),
pad_levels = "right",
format_table_name = identity,
format_colnames = identity,
format_data = data.table::as.data.table
)