LayoutDbi: Format Log Events for Output to Databases

Description Details Usage Creating a New Layout Fields Methods Database Specific Layouts See Also

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.

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
x <- x$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")

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

x$col_names
x$col_types
x$colors
x$fmt
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:

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.

See Also

select_dbi_layout(), DBI::DBI,

Other Layouts: LayoutFormat, LayoutGlue, LayoutJson, Layout


s-fleck/yog documentation built on Jan. 17, 2019, 5:56 a.m.