| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/simple_logging.R |
| 3 |
\name{simple_logging} |
| 4 |
\alias{simple_logging} |
| 5 |
\alias{log_exception} |
| 6 |
\alias{threshold} |
| 7 |
\alias{console_threshold} |
| 8 |
\alias{add_appender} |
| 9 |
\alias{remove_appender} |
| 10 |
\alias{show_log} |
| 11 |
\alias{show_dt} |
| 12 |
\alias{show_data} |
| 13 |
\title{Simple Logging} |
| 14 |
\usage{ |
| 15 |
log_exception(code, logfun = lgr$fatal, caller = get_caller(-3)) |
| 16 |
|
| 17 |
threshold(level, target = lgr::lgr) |
| 18 |
|
| 19 |
console_threshold(level, target = lgr::lgr$appenders$console) |
| 20 |
|
| 21 |
add_appender(appender, name = NULL, target = lgr::lgr) |
| 22 |
|
| 23 |
remove_appender(pos, target = lgr::lgr) |
| 24 |
|
| 25 |
show_log(threshold = NA_integer_, n = 20L, target = lgr::lgr) |
| 26 |
|
| 27 |
show_dt(target = lgr::lgr) |
| 28 |
|
| 29 |
show_data(target = lgr::lgr) |
| 30 |
} |
| 31 |
\arguments{ |
| 32 |
\item{code}{Any \R code} |
| 33 |
|
| 34 |
\item{logfun}{a \code{function} for processing the log request, usually |
| 35 |
\code{lgr$info()}, \code{lgr$debug()}, etc... .} |
| 36 |
|
| 37 |
\item{caller}{a \code{character} scalar. The name of the calling function} |
| 38 |
|
| 39 |
\item{level}{\code{integer} or \code{character} scalar: the desired log level} |
| 40 |
|
| 41 |
\item{target}{a \link{Logger} or \link{Appender} or the name of a Logger as \code{character} |
| 42 |
scalar} |
| 43 |
|
| 44 |
\item{appender}{an \code{Appender}} |
| 45 |
|
| 46 |
\item{name}{\code{character} scalar. An optional name for the new Appender.} |
| 47 |
|
| 48 |
\item{pos}{\code{integer} index or \code{character} names of the appenders to remove} |
| 49 |
|
| 50 |
\item{threshold}{\code{character} or \code{integer} scalar. The minimum \link[=log_levels]{log level} that should be processed by the root logger.} |
| 51 |
|
| 52 |
\item{n}{\code{integer} scalar. Show only the last \code{n} log entries that match |
| 53 |
\code{threshold}} |
| 54 |
} |
| 55 |
\value{ |
| 56 |
\code{threshold()} and \code{console_threshold()} return the \link{log_level} of \code{target} |
| 57 |
as \code{integer} (invisibly) |
| 58 |
|
| 59 |
\code{add_appender()} and \code{remove_appender()} return \code{target}. |
| 60 |
|
| 61 |
\code{show_log()} prints to the console and returns whatever the target |
| 62 |
Appender's \verb{$show()} method returns, usually a \code{character} vector, |
| 63 |
\code{data.frame} or \code{data.table} (invisibly). |
| 64 |
|
| 65 |
\code{show_data()} always returns a \code{data.frame} and \code{show_dt()} always returns |
| 66 |
a \code{data.table}. |
| 67 |
} |
| 68 |
\description{ |
| 69 |
lgr provides convenience functions managing the root Logger. These are |
| 70 |
designed chiefly for interactive use and are less verbose than their |
| 71 |
R6 method counterparts. |
| 72 |
|
| 73 |
\code{threshold()} sets or retrieves the threshold for an \link{Appender} or \link{Logger} |
| 74 |
(the minimum level of log messages it processes). It's \code{target} defaults to |
| 75 |
the root logger. (equivalent to \code{lgr::lgr$threshold} and |
| 76 |
\code{lgr::lgr$set_threshold}) |
| 77 |
|
| 78 |
\code{console_threshold()} is a shortcut to set the threshold of the root |
| 79 |
loggers \link{AppenderConsole}, which is usually the only Appender that manages |
| 80 |
console output for a given \R session. (equivalent to |
| 81 |
\code{lgr::lgr$appenders$console$threshold} and |
| 82 |
\code{lgr::lgr$appenders$console$set_threshold}) |
| 83 |
|
| 84 |
\code{add_appender()} and \code{remove_appender()} add Appenders to Loggers and other |
| 85 |
Appenders. (equivalent to \code{lgr::lgr$add_appender} and |
| 86 |
\code{lgr::lgr$remove_appender}) |
| 87 |
|
| 88 |
\code{show_log()} displays the last \code{n} log entries of an Appender (or a Logger |
| 89 |
with such an Appender attached) with a \verb{$show()} method. Most, but not all |
| 90 |
Appenders support this function (try \link{AppenderFile} or \link{AppenderBuffer}). |
| 91 |
|
| 92 |
\code{show_data()} and \code{show_dt()} work similar to \code{show_log()}, except that |
| 93 |
they return the log as \code{data.frame} or \code{data.table} respectively. Only |
| 94 |
Appenders that log to formats that can easily be converted to \code{data.frames} |
| 95 |
are supported (try \link{AppenderJson} or \link{AppenderBuffer}). |
| 96 |
|
| 97 |
The easiest way to try out this features is by adding an AppenderBuffer |
| 98 |
to the root logger with \code{\link[=basic_config]{basic_config(memory = TRUE)}}. |
| 99 |
} |
| 100 |
\examples{ |
| 101 |
# Get and set the threshold of the root logger |
| 102 |
threshold("error") |
| 103 |
threshold() |
| 104 |
lgr$info("this will be supressed") |
| 105 |
lgr$error("an important error message") |
| 106 |
|
| 107 |
# you can also specify a target to modify other loggers |
| 108 |
lg <- get_logger("test") |
| 109 |
threshold("fatal", target = lg) |
| 110 |
threshold(target = lg) |
| 111 |
|
| 112 |
# If a Logger's threshold is not set, the threshold is inherited from |
| 113 |
# its parent, in this case the root logger (that we set to error/200 before) |
| 114 |
threshold(NULL, target = lg) |
| 115 |
threshold(target = lg) |
| 116 |
|
| 117 |
# Alternative R6 API for getting/setting thresholds |
| 118 |
lg$set_threshold("info") |
| 119 |
lg$threshold |
| 120 |
lg$set_threshold(300) |
| 121 |
lg$threshold |
| 122 |
lg$set_threshold(NULL) |
| 123 |
lg$threshold |
| 124 |
|
| 125 |
# cleanup |
| 126 |
lgr$config(NULL) |
| 127 |
lg$config(NULL) |
| 128 |
|
| 129 |
|
| 130 |
# add Appenders to a Logger |
| 131 |
add_appender(AppenderConsole$new(), "second_console_appender") |
| 132 |
lgr$fatal("Multiple console appenders are a bad idea") |
| 133 |
remove_appender("second_console_appender") |
| 134 |
lgr$info("Good that we defined an appender name, so it's easy to remove") |
| 135 |
|
| 136 |
# Reconfigure the root logger |
| 137 |
basic_config(memory = TRUE) |
| 138 |
|
| 139 |
# log some messages |
| 140 |
lgr$info("a log message") |
| 141 |
lgr$info("another message with data", data = 1:3) |
| 142 |
|
| 143 |
show_log() |
| 144 |
show_data() |
| 145 |
|
| 146 |
# cleanup |
| 147 |
lgr$config(NULL) |
| 148 |
} |