Title: | Extra Appenders for 'lgr' |
---|---|
Description: | Additional appenders for the logging package 'lgr' that support logging to databases, email and push notifications. |
Authors: | Stefan Fleck [aut, cre] |
Maintainer: | Stefan Fleck <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.9 |
Built: | 2024-11-02 05:13:18 UTC |
Source: | https://github.com/s-fleck/lgrextra |
Log to a database table with any DBI compatible backend. Please be
aware that AppenderDbi does not support case sensitive / quoted column
names, and you advised to only use all-lowercase names for
custom fields (see ...
argument of lgr::LogEvent).
When appending to a database table all LogEvent values for which a column
exists in the target table will be appended, all others are ignored.
NOTE: AppenderDbi works reliable for most databases, but is still considered experimental, especially because the configuration is excessively complicated. Expect breaking changes to AppenderDbi in the future.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
By default, AppenderDbi writes each LogEvent directly to the target database
which can be relatively slow. To improve performance it is possible to tell
AppenderDbi to buffer db writes 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
), i.e. when you close the R session or shortly after you
remove the Appender object via rm()
.
An AppenderDbi 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 (more likely) when the first LogEvent would be written to
that table. Rather than to rely on this feature, it is recommended that you
create the target table first using an SQL CREATE TABLE
statement as this
is safer and more flexible. See also LayoutDbi.
Layouts for relational database tables are tricky as they have very strict column types and further restrictions. On top of that implementation details vary between database backends.
To make setting up AppenderDbi
as painless as possible, the helper function
select_dbi_layout()
tries to automatically determine sensible LayoutDbi
settings based on conn
and - if it exists in the database already -
table
. If table
does not exist in the database and you start logging, a
new table will be created with the col_types
from layout
.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> AppenderDbi
conn
close_on_exit
TRUE
or FALSE
. Close the Database connection
when the Logger is removed?
col_types
a named character
vector providing information about the
column types in the database. How the column types are reported
depends on the database driver. For example, SQLite returns human
readable data types (character, double, ...) while IBM DB2 returns
numeric codes representing the data type.
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
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$format()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
new()
AppenderDbi$new( conn, table, threshold = NA_integer_, layout = select_dbi_layout(conn, table), close_on_exit = TRUE, buffer_size = 0, flush_threshold = "error", flush_on_exit = TRUE, flush_on_rotate = TRUE, should_flush = NULL, filters = NULL )
conn, table
see section Fields
threshold, flush_threshold, layout, buffer_size
set_close_on_exit()
AppenderDbi$set_close_on_exit(x)
set_conn()
AppenderDbi$set_conn(conn)
show()
AppenderDbi$show(threshold = NA_integer_, n = 20)
flush()
AppenderDbi$flush()
Other Appenders:
AppenderDt
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSendmail
,
AppenderSyslog
if (requireNamespace("RSQLite")){ app <- AppenderDbi$new( conn = DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:"), table = "log" ) lg <- lgr::get_logger("test/dbi")$ add_appender(app, "db")$ set_propagate(FALSE) lg$info("test") print(lg$appenders[[1]]$data) invisible(lg$config(NULL)) # cleanup }
if (requireNamespace("RSQLite")){ app <- AppenderDbi$new( conn = DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:"), table = "log" ) lg <- lgr::get_logger("test/dbi")$ add_appender(app, "db")$ set_propagate(FALSE) lg$info("test") print(lg$appenders[[1]]$data) invisible(lg$config(NULL)) # cleanup }
Digests is an abstract class for report-like output that contain several log messages and a title; e.g. an E-mail containing the last 10 log messages before an error was encountered or a push notification.
Abstract classes, only exported for package developers.
Abstract classes cannot be instantiated with $new()
and therefore
do not return anything. They are solely for developers that want to write
their own extension to lgr.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> AppenderDigest
subject_layout
A lgr::Layout used to format the last lgr::LogEvent in this Appenders buffer when it is flushed. The result will be used as the subject of the digest (for example, the E-mail subject).
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$flush()
lgr::AppenderMemory$format()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
lgr::AppenderMemory$show()
new()
AppenderDigest$new(...)
set_subject_layout()
AppenderDigest$set_subject_layout(layout)
lgr::LayoutFormat, lgr::LayoutGlue
Other abstract classes:
AppenderMail
Other Digest Appenders:
AppenderMail
,
AppenderPushbullet
,
AppenderSendmail
An Appender that outputs to an in-memory data.table
. It fulfill a similar
purpose as the more flexible lgr::AppenderBuffer and is mainly included for
historical reasons/backwards compatibility with older version of lgr.
NOTE: AppenderDt has been superseded by lgr::AppenderBuffer and is kept mainly for archival purposes.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
AppenderDt
supports lgr::custom fields, but they have to be
pre-allocated in the prototype
argument. Custom fields that are not
part of the prototype are inserted in the list-column .fields
if it
exists.
In addition to the usual fields, AppenderDt$new()
requires that you supply
a buffer_size
and a prototype
. These determine the structure of the
data.table
used to store the log this appender creates and cannot be
modified anymore after the instantiation of the appender.
The lgr::Layout for this Appender is used only to format console output of
its $show()
method.
Both lgr::AppenderBuffer and AppenderDt do in memory buffering of events.
AppenderBuffer retains a copies of the events it processes and has the
ability to pass the buffered events on to other Appenders. AppenderDt
converts the events to rows in a data.table
and is a bit harder to
configure. Used inside loops (several hundred iterations),
AppenderDt has much less overhead than AppenderBuffer. For single logging
calls and small loops, AppenderBuffer is more performant. This is related to
how memory pre-allocation is handled by the appenders.
lgr::Filterable
-> lgr::Appender
-> AppenderDt
new()
Creating a new AppenderDt
AppenderDt$new( threshold = NA_integer_, layout = LayoutFormat$new(fmt = "%L [%t] %m %f", timestamp_fmt = "%H:%M:%OS3", colors = getOption("lgr.colors", list())), prototype = data.table::data.table(.id = NA_integer_, level = NA_integer_, timestamp = Sys.time(), logger = NA_character_, caller = NA_character_, msg = NA_character_, .fields = list(list())), buffer_size = 1e+05, filters = NULL )
prototype
A prototype data.table
. The prototype must be a
data.table
with the same columns and column types as the data
you want to log. The actual content of the columns is irrelevant.
There are a few reserved column names that have special meaning:
* .id
: integer
(mandatory). Must always be the first column
and is used internally by the Appender
* .fields
: list
(optional). If present all custom values of the
event (that are not already part of the prototype) are stored in
this list column.
buffer_size
integer
scalar. Number of rows of the in-memory data.table
append()
AppenderDt$append(event)
show()
AppenderDt$show(threshold = NA_integer_, n = 20L)
set_layout()
AppenderDt$set_layout(layout)
Other Appenders:
AppenderDbi
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSendmail
,
AppenderSyslog
lg <- lgr::get_logger("test") lg$config(list( appenders = list(memory = AppenderDt$new()), threshold = NA, propagate = FALSE # to prevent routing to root logger for this example )) lg$debug("test") lg$error("test") # Displaying the log lg$appenders$memory$data lg$appenders$memory$show() lgr::show_log(target = lg$appenders$memory) # If you pass a Logger to show_log(), it looks for the first AppenderDt # that it can find. lgr::show_log(target = lg) # Custom fields are stored in the list column .fields by default lg$info("the iris data frame", caps = LETTERS[1:5]) lg$appenders$memory$data lg$appenders$memory$data$.fields[[3]]$caps lg$config(NULL)
lg <- lgr::get_logger("test") lg$config(list( appenders = list(memory = AppenderDt$new()), threshold = NA, propagate = FALSE # to prevent routing to root logger for this example )) lg$debug("test") lg$error("test") # Displaying the log lg$appenders$memory$data lg$appenders$memory$show() lgr::show_log(target = lg$appenders$memory) # If you pass a Logger to show_log(), it looks for the first AppenderDt # that it can find. lgr::show_log(target = lg) # Custom fields are stored in the list column .fields by default lg$info("the iris data frame", caps = LETTERS[1:5]) lg$appenders$memory$data lg$appenders$memory$data$.fields[[3]]$caps lg$config(NULL)
Log to ElasticSearch via HTTP
NOTE: Experimental; not yet fully documented and and details are subject to change
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> AppenderElasticSearch
conn
index
target ElasticSearch index. May either be:
a character
scalar, or
a function
returning a character
scalar
index_create_body
character
scalar json string (or NULL
).
a function
returning a character
scalar json string (or NULL
)
Optional settings,
mappings, aliases, etc... in case the target index has to be created
by the logger. See https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$format()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
new()
AppenderElasticSearch$new( conn, index, threshold = NA_integer_, layout = LayoutElasticSearch$new(), index_create_body = NULL, buffer_size = 0, flush_threshold = "error", flush_on_exit = TRUE, flush_on_rotate = TRUE, should_flush = NULL, filters = NULL )
conn, index
see section Fields
threshold, flush_threshold, layout, buffer_size
see lgr::AppenderBuffer
A data data.frame
. content of index
set_conn()
AppenderElasticSearch$set_conn(conn)
get_data()
AppenderElasticSearch$get_data( n = 20L, threshold = NA, result_type = "data.frame" )
n
integer
scalar. Retrieve only the last n
log entries that match
threshold
threshold
character
or integer
scalar. The minimum log level
that should be displayed
result_type
character
scalar. Any of:
data.frame
data.table
(shortcut: dt
)
list
(unprocessed list with ElasticSearch metadata)
json
(raw ElasticSearch JSON)
see result_type
show()
AppenderElasticSearch$show(threshold = NA_integer_, n = 20)
flush()
AppenderElasticSearch$flush()
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSendmail
,
AppenderSyslog
Send mails via gmailr::gm_send_message()
. This
Appender keeps an in-memory buffer like lgr::AppenderBuffer. If the buffer is
flushed, usually because an event of specified magnitude is encountered, all
buffered events are concatenated to a single message. The default behavior
is to push the last 30 log events in case a fatal
event is encountered.
NOTE: This Appender requires that you set up google API authorization, please refer to the documentation of gmailr for details.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> lgrExtra::AppenderDigest
-> lgrExtra::AppenderMail
-> AppenderGmail
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
lgr::AppenderMemory$show()
lgrExtra::AppenderDigest$set_subject_layout()
lgrExtra::AppenderMail$format()
lgrExtra::AppenderMail$set_bcc()
lgrExtra::AppenderMail$set_cc()
lgrExtra::AppenderMail$set_from()
lgrExtra::AppenderMail$set_html()
lgrExtra::AppenderMail$set_to()
new()
see AppenderMail for details
AppenderGmail$new( to, threshold = NA_integer_, flush_threshold = "fatal", layout = LayoutFormat$new(fmt = "%L [%t] %m %f", timestamp_fmt = "%H:%M:%S"), subject_layout = LayoutFormat$new(fmt = "[LGR] %L: %m"), buffer_size = 30, from = get_user(), cc = NULL, bcc = NULL, html = FALSE, filters = NULL )
flush()
AppenderGmail$flush()
lgr::LayoutFormat, lgr::LayoutGlue
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderElasticSearch
,
AppenderPushbullet
,
AppenderSendmail
,
AppenderSyslog
Abstract classes, only exported for package developers.
Abstract classes cannot be instantiated with $new()
and therefore
do not return anything. They are solely for developers that want to write
their own extension to lgr.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> lgrExtra::AppenderDigest
-> AppenderMail
to
character
vector. The email addresses of the recipient
from
character
vector. The email address of the sender
cc
character
vector. The email addresses of the cc-recipients (carbon copy)
bcc
character
vector. The email addresses of bcc-recipients (blind carbon copy)
html
logical
scalar. Send a html email message?
This does currently only format the log contents as monospace verbatim
text.
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$flush()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
lgr::AppenderMemory$show()
lgrExtra::AppenderDigest$set_subject_layout()
new()
AppenderMail$new(...)
set_to()
AppenderMail$set_to(x)
set_from()
AppenderMail$set_from(x)
set_cc()
AppenderMail$set_cc(x)
set_bcc()
AppenderMail$set_bcc(x)
set_html()
AppenderMail$set_html(x)
format()
AppenderMail$format(color = FALSE, ...)
Other abstract classes:
AppenderDigest
Other Digest Appenders:
AppenderDigest
,
AppenderPushbullet
,
AppenderSendmail
Send push notifications via Pushbullet. This
Appender keeps an in-memory buffer like lgr::AppenderBuffer. If the buffer is
flushed, usually because an event of specified magnitude is encountered, all
buffered events are concatenated to a single message that is sent to
RPushbullet::pbPost()
. The default behavior is to push the last 7 log
events in case a fatal
event is encountered.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> lgrExtra::AppenderDigest
-> AppenderPushbullet
apikey
recipients
email
channel
devices
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$format()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
lgr::AppenderMemory$show()
lgrExtra::AppenderDigest$set_subject_layout()
new()
AppenderPushbullet$new( threshold = NA_integer_, flush_threshold = "fatal", layout = LayoutFormat$new(fmt = "%K %t> %m %f", timestamp_fmt = "%H:%M:%S"), subject_layout = LayoutFormat$new(fmt = "[LGR] %L: %m"), buffer_size = 6, recipients = NULL, email = NULL, channel = NULL, devices = NULL, apikey = getOption("rpushbullet.key"), filters = NULL )
threshold, flush_threshold, layout, buffer_size
subject_layout
A lgr::LayoutFormat object.
recipients, email, channel, devices, apikey
flush()
AppenderPushbullet$flush()
set_apikey()
AppenderPushbullet$set_apikey(x)
set_recipients()
AppenderPushbullet$set_recipients(x)
set_email()
AppenderPushbullet$set_email(x)
set_channel()
AppenderPushbullet$set_channel(x)
set_devices()
AppenderPushbullet$set_devices(x)
lgr::LayoutFormat, lgr::LayoutGlue
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderSendmail
,
AppenderSyslog
Other Digest Appenders:
AppenderDigest
,
AppenderMail
,
AppenderSendmail
if (requireNamespace("RPushbullet") && !is.null(getOption("rpushbullet.key")) ){ app <- AppenderPushbullet$new() lg <- lgr::get_logger("test/dbi")$ add_appender(app, "pb")$ set_propagate(FALSE) lg$fatal("info") lg$fatal("test") invisible(lg$config(NULL)) }
if (requireNamespace("RPushbullet") && !is.null(getOption("rpushbullet.key")) ){ app <- AppenderPushbullet$new() lg <- lgr::get_logger("test/dbi")$ add_appender(app, "pb")$ set_propagate(FALSE) lg$fatal("info") lg$fatal("test") invisible(lg$config(NULL)) }
Send mails via sendmailR::sendmail()
, which requires that you have access
to an SMTP server that does not require authentication. This
Appender keeps an in-memory buffer like lgr::AppenderBuffer. If the buffer is
flushed, usually because an event of specified magnitude is encountered, all
buffered events are concatenated to a single message. The default behavior
is to push the last 30 log events in case a fatal
event is encountered.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
lgr::Filterable
-> lgr::Appender
-> lgr::AppenderMemory
-> lgrExtra::AppenderDigest
-> lgrExtra::AppenderMail
-> AppenderSendmail
control
headers
lgr::Filterable$add_filter()
lgr::Filterable$filter()
lgr::Filterable$remove_filter()
lgr::Filterable$set_filters()
lgr::Appender$set_layout()
lgr::Appender$set_threshold()
lgr::AppenderMemory$append()
lgr::AppenderMemory$clear()
lgr::AppenderMemory$set_buffer_size()
lgr::AppenderMemory$set_flush_on_exit()
lgr::AppenderMemory$set_flush_on_rotate()
lgr::AppenderMemory$set_flush_threshold()
lgr::AppenderMemory$set_should_flush()
lgr::AppenderMemory$show()
lgrExtra::AppenderDigest$set_subject_layout()
lgrExtra::AppenderMail$format()
lgrExtra::AppenderMail$set_bcc()
lgrExtra::AppenderMail$set_cc()
lgrExtra::AppenderMail$set_from()
lgrExtra::AppenderMail$set_html()
lgrExtra::AppenderMail$set_to()
new()
see AppenderMail for details
AppenderSendmail$new( to, control, threshold = NA_integer_, flush_threshold = "fatal", layout = LayoutFormat$new(fmt = " %L [%t] %m %f", timestamp_fmt = "%H:%M:%S"), subject_layout = LayoutFormat$new(fmt = "[LGR] %L: %m"), buffer_size = 29, from = get_user(), cc = NULL, bcc = NULL, html = FALSE, headers = NULL, filters = NULL )
flush()
AppenderSendmail$flush()
set_control()
AppenderSendmail$set_control(x)
set_headers()
AppenderSendmail$set_headers(x)
The default Layout's fmt
indents each log entry with 3 blanks. This
is a workaround so that Microsoft Outlook does not mess up the line breaks.
lgr::LayoutFormat, lgr::LayoutGlue
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSyslog
Other Digest Appenders:
AppenderDigest
,
AppenderMail
,
AppenderPushbullet
## Not run: lgr::AppenderSendmail$new( to = "[email protected]", control = list(smtpServer = "mail.ecorp.com"), from = "[email protected]" ) ## End(Not run) if (requireNamespace("sendmailR")){ # requires that you have access to an SMTP server lg <- lgr::get_logger("lgrExtra/test/mail")$ set_propagate(FALSE)$ add_appender(AppenderSendmail$new( from = "[email protected]", to = "[email protected]", control = list(smtpServer = "mail.somesmptserver.com") )) # cleanup invisible(lg$config(NULL)) }
## Not run: lgr::AppenderSendmail$new( to = "[email protected]", control = list(smtpServer = "mail.ecorp.com"), from = "[email protected]" ) ## End(Not run) if (requireNamespace("sendmailR")){ # requires that you have access to an SMTP server lg <- lgr::get_logger("lgrExtra/test/mail")$ set_propagate(FALSE)$ add_appender(AppenderSendmail$new( from = "[email protected]", to = "[email protected]", control = list(smtpServer = "mail.somesmptserver.com") )) # cleanup invisible(lg$config(NULL)) }
An Appender that writes to the syslog on supported POSIX platforms. Requires the rsyslog package.
The $new()
method returns an R6::R6 that inherits from
lgr::Appender and can be uses as an appender by a lgr::Logger.
lgr::Filterable
-> lgr::Appender
-> AppenderSyslog
syslog_levels.
Either a named character
vector or a function
mapping lgr lgr::log_levels to rsyslog log levels. See
$set_syslog_levels()
.
identifier
character
scalar. A string identifying the process;
if NULL
defaults to the logger name
syslog_levels.
Either a named character
vector or a function
mapping lgr lgr::log_levels to rsyslog log levels. See
$set_syslog_levels()
.
new()
AppenderSyslog$new( identifier = NULL, threshold = NA_integer_, layout = LayoutFormat$new("%m"), filters = NULL, syslog_levels = c(CRITICAL = "fatal", ERR = "error", WARNING = "warn", INFO = "info", DEBUG = "debug", DEBUG = "trace") )
append()
AppenderSyslog$append(event)
set_syslog_levels()
Define conversion between lgr and syslog log levels
AppenderSyslog$set_syslog_levels(x)
x
a named character
vector mapping whose names are log
levels as understood by rsyslog::syslog()
and whose values are lgr log levels (either character
or numeric
)
a function
that takes a vector of lgr log levels as input and
returns a character
vector of log levels for rsyslog::syslog()
.
set_identifier()
Set a string to identify the process.
AppenderSyslog$set_identifier(x)
lgr::LayoutFormat, lgr::LayoutGlue
Other Appenders:
AppenderDbi
,
AppenderDt
,
AppenderElasticSearch
,
AppenderGmail
,
AppenderPushbullet
,
AppenderSendmail
if (requireNamespace("rsyslog", quietly = TRUE) && Sys.info()[["sysname"]] == "Linux") { lg <- lgr::get_logger("rsyslog/test") lg$add_appender(AppenderSyslog$new(), "syslog") lg$info("A test message") print(system("journalctl -t 'rsyslog/test'")) invisible(lg$config(NULL)) # cleanup }
if (requireNamespace("rsyslog", quietly = TRUE) && Sys.info()[["sysname"]] == "Linux") { lg <- lgr::get_logger("rsyslog/test") lg$add_appender(AppenderSyslog$new(), "syslog") lg$info("A test message") print(system("journalctl -t 'rsyslog/test'")) invisible(lg$config(NULL)) # cleanup }
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 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.
The $new()
method returns an R6::R6 that inherits from
lgr::Layout and can used as a Layout by an lgr::Appender.
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
.
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 )
set_col_types()
LayoutDbi$set_col_types(x)
set_serialized_cols()
LayoutDbi$set_serialized_cols(x)
sql_create_table()
LayoutDbi$sql_create_table(table)
toString()
LayoutDbi$toString()
clone()
The objects of this class are cloneable with this method.
LayoutDbi$clone(deep = FALSE)
deep
Whether to make a deep clone.
select_dbi_layout()
, DBI::DBI,
Other Layout:
LayoutElasticSearch
Similar to lgr::LayoutJson, but with some modifications to prepare data for ElasticSearch.
The $new()
method returns an R6::R6 that inherits from
lgr::Layout and can used as a Layout by an lgr::Appender.
lgr::Layout
-> LayoutElasticSearch
toJSON_args
a list of values passed on to jsonlite::toJSON()
transform_event
a function
with a single argument event
that
takes a lgr::LogEvent and returns a list
.
new()
LayoutElasticSearch$new( toJSON_args = list(auto_unbox = TRUE), transform_event = function(event) get("values", event) )
format_event()
LayoutElasticSearch$format_event(event)
set_toJSON_args()
LayoutElasticSearch$set_toJSON_args(x)
set_transform_event()
LayoutElasticSearch$set_transform_event(x)
clone()
The objects of this class are cloneable with this method.
LayoutElasticSearch$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other Layout:
LayoutDbi
Selects an appropriate Layout for a database table based on a DBI connection and - if it already exists in the database - the table itself.
select_dbi_layout(conn, table, ...)
select_dbi_layout(conn, table, ...)
conn |
|
table |
a |
... |
passed on to the appropriate |
Serializers are used by AppenderDbi to store multiple values in a single
text column in a Database table. Usually you just want to use the default
SerializerJson
. Please not that AppenderDbi
as well as Serializers
are still experimental.
a Serializer
R6::R6 object for AppenderDbi.
clone()
The objects of this class are cloneable with this method.
Serializer$clone(deep = FALSE)
deep
Whether to make a deep clone.
lgrExtra::Serializer
-> SerializerJson
new()
SerializerJson$new( cols = "*", cols_exclude = c("level", "timestamp", "logger", "caller", "msg"), col_filter = is.atomic, max_nchar = 2048L, auto_unbox = TRUE )
serialize()
SerializerJson$serialize(event)
clone()
The objects of this class are cloneable with this method.
SerializerJson$clone(deep = FALSE)
deep
Whether to make a deep clone.
# The defaul Serializer for 'custom fields' columns SerializerJson$new()
# The defaul Serializer for 'custom fields' columns SerializerJson$new()
Unserialize data frame columns that contain JSON
unpack_json_cols(x, cols) ## S3 method for class 'data.table' unpack_json_cols(x, cols) ## S3 method for class 'data.frame' unpack_json_cols(x, cols)
unpack_json_cols(x, cols) ## S3 method for class 'data.table' unpack_json_cols(x, cols) ## S3 method for class 'data.frame' unpack_json_cols(x, cols)
x |
a |
cols |
|
a data.frame
with additional columns expanded from the columns
containing JSON
x <- data.frame( name = "example data", fields = '{"letters":["a","b","c"], "LETTERS":["A","B","C"]}', stringsAsFactors = FALSE ) res <- unpack_json_cols(x, "fields") res res$letters[[1]]
x <- data.frame( name = "example data", fields = '{"letters":["a","b","c"], "LETTERS":["A","B","C"]}', stringsAsFactors = FALSE ) res <- unpack_json_cols(x, "fields") res res$letters[[1]]