appender_async: Delays executing the actual appender function to the future...

Description Usage Arguments Value Note See Also Examples

View source: R/appenders.R

Description

Delays executing the actual appender function to the future in a background process to avoid blocking the main R session

Usage

1
2
3
4
5
6
appender_async(
  appender,
  batch = 1,
  namespace = "async_logger",
  init = function() log_info("Background process started")
)

Arguments

appender

a log_appender function with a generator attribute (TODO note not required, all fn will be passed if not)

batch

number of records to process from the queue at once

namespace

logger namespace to use for logging messages on starting up the background process

init

optional function to run in the background process that is useful to set up the environment required for logging, eg if the appender function requires some extra packages to be loaded or some environment variables to be set etc

Value

function taking lines argument

Note

This functionality depends on the txtq and callr packages. The R session's temp folder is used for staging files (message queue and other forms of communication between the parent and child processes).

See Also

This function is to be used with an actual log_appender, for example appender_console, appender_file, appender_tee, appender_pushbullet, appender_telegram, appender_syslog or appender_kinesis.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Not run: 
appender_file_slow <- function(file) {
  force(file)
  function(lines) {
    Sys.sleep(1)
    cat(lines, sep = '\n', file = file, append = TRUE)
  }
}

## log what's happening in the background
log_threshold(TRACE, namespace = 'async_logger')
log_appender(appender_console, namespace = 'async_logger')

## start async appender
t <- tempfile()
log_info('Logging in the background to {t}')
my_appender <- appender_async(appender_file_slow(file = t))

## use async appender
log_appender(my_appender)
log_info('Was this slow?')
system.time(for (i in 1:25) log_info(i))

readLines(t)
Sys.sleep(10)
readLines(t)

## check on the async appender (debugging, you will probably never need this)
attr(my_appender, 'async_writer_queue')$count()
attr(my_appender, 'async_writer_queue')$log()

attr(my_appender, 'async_writer_process')$get_pid()
attr(my_appender, 'async_writer_process')$get_state()
attr(my_appender, 'async_writer_process')$poll_process(1)
attr(my_appender, 'async_writer_process')$read()

attr(my_appender, 'async_writer_process')$is_alive()
attr(my_appender, 'async_writer_process')$read_error()

## End(Not run)

logger documentation built on Oct. 19, 2021, 9:07 a.m.