LayoutDbi.Rd
LayoutDbi 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.
The $new()
method returns an R6::R6 that inherits from
lgr::Layout and can used as a Layout by an lgr::Appender.
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
as.data.table.LogEvent), as well as column- and table names.
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 databases
LayoutPostgres
: for Postgres databases
LayoutMySql
: for MySQL databases
LayoutDb2
: 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
.
select_dbi_layout()
, DBI::DBI,
Other Layout:
LayoutElasticSearch
lgr::Layout
-> lgr::LayoutFormat
-> LayoutDbi
format_table_name
a function
to format the table name before
inserting to the database. The function will be applied to the
$table_name
before 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_colnames
a function
to 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_data
a function
to format the data before
inserting into the database. The function will be applied to the whole
data frame.
names
of the columns that contain data that has been serialized to JSON strings
col_types
a named character
vector of column types supported by
the target database. If not NULL
this 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_types
is not used.
names
of the columns that contain data that has been serialized to JSON strings
col_names
column names of the target table (the same as
names(lo$col_types)
)
new()
LayoutDbi$new(
col_types = c(level = "integer", timestamp = "timestamp", logger = "varchar(256)",
caller = "varchar(256)", msg = "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
)
toString()