lgr (version 0.3.3)

LayoutDbi: Format Log Events for Output to Databases

Description

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.

Arguments

Usage

x <- LayoutDbi$new(col_types = 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 = identity)

x$clone(deep = FALSE) x$format_event(event) x$set_col_types(x) x$set_colors(x) x$set_fmt(x) x$set_pad_levels(x) x$set_timestamp_fmt(x) x$sql_create_table(table) x$toString()

x$col_names x$col_types x$colors x$fmt x$format_colnames x$format_data x$format_table_name x$pad_levels x$timestamp_fmt

Creating a New Layout

Layouts are instantiated with <LayoutSubclass>$new(). For a description of the arguments to this function please refer to the Fields section.

Fields

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.

col_names

Convenience method to get the names of the col_types vector

Methods

format_table_name(x)

Format table names 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

Format column names before inserting into the database. See $format_table_name for more info

format_data

Format the input data.table before inserting into the database. Usually this function does nothing, but for example for SQLite it has to apply formatting to the timestamp.

col_names

Convenience method to get the names of the col_types vector

format_event(event)

format a LogEvent

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 lgr 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.

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 as.data.table.LogEvent), as well as column- and table names.

See Also

select_dbi_layout(), DBI::DBI,

Other Layouts: LayoutFormat, LayoutGlue, LayoutJson, Layout