Title: | A Fast and Lightweight Logging System for R, Based on 'log4j' |
---|---|
Description: | The log4r package is meant to provide a fast, lightweight, object-oriented approach to logging in R based on the widely-emulated 'log4j' system and etymology. |
Authors: | John Myles White [aut, cph], Kenton White [ctb], Kirill Müller [ctb], Aaron Jacobs [aut, cre] |
Maintainer: | Aaron Jacobs <[email protected]> |
License: | Artistic-2.0 |
Version: | 0.4.4.9000 |
Built: | 2024-11-17 06:11:27 UTC |
Source: | https://github.com/r-lib/log4r |
In log4j etymology, Appenders are destinations where logs are written. Appenders have no control over formatting; this is controlled by the Layout.
The most basic appenders write logs to the console or to a file; these are described below.
For implementing your own appenders, see Details.
console_appender(layout = default_log_layout()) file_appender(file, append = TRUE, layout = default_log_layout())
console_appender(layout = default_log_layout()) file_appender(file, append = TRUE, layout = default_log_layout())
layout |
A layout function taking a |
file |
The file to write messages to. |
append |
When |
Appenders are implemented as functions with the interface function(level, ...)
. These functions are expected to write their arguments to a destination
and return invisible(NULL)
.
tcp_appender()
, http_appender()
, syslog_appender()
# The behaviour of an appender can be seen by using them directly; the # following snippet will write the message to the console. appender <- console_appender() appender("INFO", "Input has length ", 0, ".")
# The behaviour of an appender can be seen by using them directly; the # following snippet will write the message to the console. appender <- console_appender() appender("INFO", "Input has length ", 0, ".")
Send logs in the body of HTTP requests. Responses with status code 400 or above will trigger errors.
Requires the httr
package.
http_appender(url, method = "POST", layout = default_log_layout(), ...)
http_appender(url, method = "POST", layout = default_log_layout(), ...)
url |
The URL to submit messages to. |
method |
The HTTP method to use, usually |
layout |
A layout function taking a |
... |
Further arguments passed on to |
appenders for more information on Appenders.
## Not run: # POST messages to localhost. appender <- http_appender("localhost") appender("INFO", "Message.") # POST JSON-encoded messages. appender <- http_appender( "localhost", method = "POST", layout = default_log_layout(), httr::content_type_json() ) appender("INFO", "Message.") ## End(Not run)
## Not run: # POST messages to localhost. appender <- http_appender("localhost") appender("INFO", "Message.") # POST JSON-encoded messages. appender <- http_appender( "localhost", method = "POST", layout = default_log_layout(), httr::content_type_json() ) appender("INFO", "Message.") ## End(Not run)
In log4j etymology, Layouts are how Appenders control the format of messages. Most users will use one of the general-purpose layouts provided by the package:
default_log_layout()
formats messages much like the original log4j
library. simple_log_layout()
does the same, but omits the timestamp.
bare_log_layout()
emits only the log message, with no level or timestamp
fields.
logfmt_log_layout()
and json_log_layout()
format structured logs in the
two most popular machine-readable formats.
For implementing your own layouts, see Details.
default_log_layout(time_format = "%Y-%m-%d %H:%M:%S") simple_log_layout() bare_log_layout() logfmt_log_layout() json_log_layout()
default_log_layout(time_format = "%Y-%m-%d %H:%M:%S") simple_log_layout() bare_log_layout() logfmt_log_layout() json_log_layout()
time_format |
A valid format string for timestamps. See
|
Layouts return a function with the signature function(level, ...)
that
itself returns a single newline-terminated string. Anything that meets this
interface can be passed as a layout to one of the existing appenders.
json_log_layout
requires the jsonlite
package.
# The behaviour of a layout can be seen by using them directly: simple <- simple_log_layout() simple("INFO", "Input has length ", 0, ".") with_timestamp <- default_log_layout() with_timestamp("INFO", "Input has length ", 0, ".") logfmt <- logfmt_log_layout() logfmt("INFO", msg = "got input", length = 24)
# The behaviour of a layout can be seen by using them directly: simple <- simple_log_layout() simple("INFO", "Input has length ", 0, ".") with_timestamp <- default_log_layout() with_timestamp("INFO", "Input has length ", 0, ".") logfmt <- logfmt_log_layout() logfmt("INFO", msg = "got input", length = 24)
It can sometimes be useful to change the logging threshold level at runtime.
The level()
accessor allows doing so.
level(x) level(x) <- value ## S3 method for class 'logger' level(x) ## S3 replacement method for class 'logger' level(x) <- value available.loglevels()
level(x) level(x) <- value ## S3 method for class 'logger' level(x) ## S3 replacement method for class 'logger' level(x) <- value available.loglevels()
x |
An object of class |
value |
One of |
lgr <- logger() level(lgr) # Prints "INFO". info(lgr, "This message is shown.") level(lgr) <- "FATAL" info(lgr, "This message is now suppressed.")
lgr <- logger() level(lgr) # Prints "INFO". info(lgr, "This message is shown.") level(lgr) <- "FATAL" info(lgr, "This message is now suppressed.")
Write logs at a given level
log_at(logger, level, ...) log_debug(logger, ...) log_info(logger, ...) log_warn(logger, ...) log_error(logger, ...) log_fatal(logger, ...)
log_at(logger, level, ...) log_debug(logger, ...) log_info(logger, ...) log_warn(logger, ...) log_error(logger, ...) log_fatal(logger, ...)
logger |
An object of class |
level |
The desired severity, one of |
... |
One or more items to log. |
logger <- logger() log_at(logger, "WARN", "First warning from our code") log_debug(logger, "Debugging our code") log_info(logger, "Information about our code") log_warn(logger, "Another warning from our code") log_error(logger, "An error from our code") log_fatal(logger, "I'm outta here")
logger <- logger() log_at(logger, "WARN", "First warning from our code") log_debug(logger, "Debugging our code") log_info(logger, "Information about our code") log_warn(logger, "Another warning from our code") log_error(logger, "An error from our code") log_fatal(logger, "I'm outta here")
This is the main interface for configuring logging behaviour. We adopt the well-known log4j etymology: Appenders are destinations (e.g. the console or a file) where logs are written, and the Layout is the format of these logs.
logger(threshold = "INFO", appenders = console_appender())
logger(threshold = "INFO", appenders = console_appender())
threshold |
The logging threshold, one of |
appenders |
The logging appenders; both single appenders and a |
An object of class "logger"
.
Appenders and Layouts for information on controlling the behaviour of the logger object.
# By default, logs are written to the console at the "INFO" threshold. logger <- logger() log_info(logger, "Located nearest gas station.") log_warn(logger, "Ez-Gas sensor network is not available.") log_debug(logger, "Debug messages are suppressed by default.")
# By default, logs are written to the console at the "INFO" threshold. logger <- logger() log_info(logger, "Located nearest gas station.") log_warn(logger, "Ez-Gas sensor network is not available.") log_debug(logger, "Debug messages are suppressed by default.")
Send messages to the local syslog. Requires the rsyslog
package.
syslog_appender(identifier, layout = bare_log_layout(), ...)
syslog_appender(identifier, layout = bare_log_layout(), ...)
identifier |
A string identifying the application. |
layout |
A layout function taking a |
... |
Further arguments passed on to |
appenders for more information on Appenders.
Append messages to arbitrary TCP destinations.
tcp_appender( host, port, layout = default_log_layout(), timeout = getOption("timeout") )
tcp_appender( host, port, layout = default_log_layout(), timeout = getOption("timeout") )
host |
Hostname for the socket connection. |
port |
Port number for the socket connection. |
layout |
A layout function taking a |
timeout |
Timeout for the connection. |
appenders for more information on Appenders, and
base::socketConnection()
for the underlying connection object
used by tcp_appender()
.