Skip to contents

Decorator that logs when a call starts, and when it finishes or fails. Each event is written by logger, see logger_base. A failure is logged then signalled again unchanged.

Usage

log_call(f, logger = logger_base(), level = log_levels(), elapsed = TRUE)

Arguments

f

The function to decorate. Usually supplied by %@% rather than directly.

logger

A logger, such as logger_message(). See extending_log to use a logging package.

level

A log_levels() giving the level of the start, finish and error events.

elapsed

Include the time taken in the finish message?

Value

A decorator.

Details

The message holds the function's name, its arguments matched to the formals and, once finished, the time taken. Arguments are evaluated before the start is logged.

When a call fails, the calls from f down to the failure are logged as a separate trace event after the error. Whether it is shown is up to the logger: the built-in loggers hide it until threshold = "trace", see logger_base.

For a function that returns a promise, use async_log_call(). Here the finish would be logged when the promise is returned, not when it settles.

Examples

fn := log_call(logger = logger_message()) %@% function(a, b, c) a + b + c
fn(1, 2, 3)
#> INFO [2026-11-11T17:04:40.261Z]: Start fn(a=1,b=2,c=3)
#> INFO [2026-11-11T17:04:40.262Z]: Finished fn(a=1,b=2,c=3) Elapsed=00:00:00.001
#> [1] 6

# Log failures as warnings
g := log_call(
  logger = logger_message(),
  level = log_levels(error = "warn")
) %@% function(x) stop("boom")
try(g(1), silent = TRUE)
#> INFO [2026-11-11T17:04:40.264Z]: Start g(x=1)
#> WARN [2026-11-11T17:04:40.265Z]: g(x=1) 
#> Error in `g(x = x)`:
#> boom