Learn R Programming

logger

A lightweight, modern and flexible logging utility for R – heavily inspired by the futile.logger R package and logging Python module.

Installation

install.packages("logger")

The most recent, development version of logger can also be installed from GitHub:

# install.packages("pak")
pak::pak("daroczig/logger")

Quick example

Setting the log level threshold to something low and logging various messages in ad-hoc and programmatic ways:

library(logger)
log_threshold(DEBUG)
log_info("Script starting up...")
#> INFO [2024-08-15 11:59:27] Script starting up...

pkgs <- available.packages()
log_info("There are {nrow(pkgs)} R packages hosted on CRAN!")
#> INFO [2024-08-15 11:59:28] There are 21131 R packages hosted on CRAN!

for (letter in letters) {
  lpkgs <- sum(grepl(letter, pkgs[, "Package"], ignore.case = TRUE))
  log_level(
    if (lpkgs < 5000) TRACE else DEBUG,
    "{lpkgs} R packages including the {shQuote(letter)} letter"
  )
}
#> DEBUG [2024-08-15 11:59:28] 10193 R packages including the 'a' letter
#> DEBUG [2024-08-15 11:59:28] 7016 R packages including the 'c' letter
#> DEBUG [2024-08-15 11:59:28] 5751 R packages including the 'd' letter
#> DEBUG [2024-08-15 11:59:28] 10907 R packages including the 'e' letter
#> DEBUG [2024-08-15 11:59:28] 8825 R packages including the 'i' letter
#> DEBUG [2024-08-15 11:59:28] 7059 R packages including the 'l' letter
#> DEBUG [2024-08-15 11:59:28] 7045 R packages including the 'm' letter
#> DEBUG [2024-08-15 11:59:28] 6665 R packages including the 'n' letter
#> DEBUG [2024-08-15 11:59:28] 7863 R packages including the 'o' letter
#> DEBUG [2024-08-15 11:59:28] 6581 R packages including the 'p' letter
#> DEBUG [2024-08-15 11:59:28] 11229 R packages including the 'r' letter
#> DEBUG [2024-08-15 11:59:28] 10296 R packages including the 's' letter
#> DEBUG [2024-08-15 11:59:28] 9531 R packages including the 't' letter

log_warn("There might be many, like {1:2} or more warnings!!!")
#> WARN [2024-08-15 11:59:28] There might be many, like 1 or more warnings!!!
#> WARN [2024-08-15 11:59:28] There might be many, like 2 or more warnings!!!

You can even use a custom log layout to render the log records with colors, as you can see in layout_glue_colors():

But you could set up any custom colors and layout, eg using custom colors only for the log levels, make it grayscale, include the calling function or R package namespace with specific colors etc. For more details, see vignette("write_custom_extensions").

Related work

There are many other logging packages available on CRAN:

  • futile.logger: probably the most popular log4j variant (and I’m a big fan)
  • logging: just like Python’s logging package
  • lgr: built on top of R6.
  • loggit: capture message, warning and stop function messages in a JSON file
  • log4r: log4j-based, object-oriented logger
  • rsyslog: logging to syslog on ‘POSIX’-compatible operating systems
  • lumberjack: provides a special operator to log changes in data

Why use logger? I decided to write the n+1th extensible log4j logger that fits my liking — and hopefully yours as well — with the aim to:

  • Keep it close to log4j.
  • Respect the modern function/variable naming conventions and general R coding style.
  • By default, rely on glue() when it comes to formatting / rendering log messages, but keep it flexible if others prefer sprintf() (e.g. for performance reasons) or other functions.
  • Support vectorization (eg passing a vector to be logged on multiple lines).
  • Make it easy to extend with new features (e.g. custom layouts, message formats and output).
  • Prepare for writing to various services, streams etc
  • Provide support for namespaces, preferably automatically finding and creating a custom namespace for all R packages writing log messages, each with optionally configurable log level threshold, message and output formats.
  • Allow stacking loggers to implement logger hierarchy – even within a namespace, so that the very same log call can write all the TRACE log messages to the console, while only pushing ERRORs to DataDog and eg INFO messages to CloudWatch.
  • Optionally colorize log message based on the log level.
  • Make logging fun!

Welcome to the Bazaar! If you already use any of the above packages for logging, you might find vignette("migration") useful.

Interested in more details?

Check out the main documentation site at https://daroczig.github.io/logger/ or the vignettes on the below topics:

If you prefer visual content, you can watch the video recording of the “Getting things logged” talk at RStudio::conf(2020):

Copy Link

Version

Install

install.packages('logger')

Monthly Downloads

68,592

Version

0.4.3

License

MIT + file LICENSE

Maintainer

Gergely Daróczi

Last Published

August 24th, 2026

Functions in logger (0.4.3)

appender_syslognet

Send log messages to a network syslog server
formatter_glue_or_sprintf

formatter_glue_safe

Apply glue::glue_safe() to convert R objects into a character vector
deparse_to_one_line

Deparse and join all lines into a single line
fail_on_missing_package

Check if R package can be loaded and fails loudly otherwise
layout_json

Generate log layout function rendering JSON
layout_syslognet

Format a log record for syslognet
formatter_sprintf

Apply sprintf() to convert R objects into a character vector
get_logger_meta_variables

Collect useful information about the logging environment to be used in log messages
log_appender

Get or set log record appender function
appender_file

Append log messages to a file
layout_json_parser

Generate log layout function rendering JSON after merging meta fields with parsed list from JSON message
appender_pushbullet

Send log messages to Pushbullet
log_warnings

Injects a logger call to standard warnings
logger

Generate logging utility
appender_tee

Append log messages to a file and stdout as well
log_chunk_time

Automatically log execution time of knitr chunks
log_elapsed

Log cumulative running time
skip_formatter

Skip the formatter function
layout_logging

Format a log record as the logging package does by default
appender_telegram

Send log messages to a Telegram chat
formatter_cli

Apply cli::cli_text() to format string with cli syntax
formatter_glue

Apply glue::glue() to convert R objects into a character vector
log_errors

Injects a logger call to standard errors
appender_void

Dummy appender not delivering the log record to anywhere
layout_simple

Format a log record by concatenating the log level, timestamp and message
log_eval

Evaluate an expression and log results
log_messages

Injects a logger call to standard messages
appender_stdout

Append log record to stdout
formatter_paste

Concatenate R objects into a character vector via paste
formatter_pander

Formats R objects with pander
log_indices

Returns number of currently active indices
log_layout

Get or set log record layout
appender_slack

Send log messages to a Slack channel
log_with_separator

Logs a message in a very visible way
appender_kinesis

Send log messages to a Amazon Kinesis stream
layout_blank

Format a log record by including the raw message without anything added or modified
%except%

Try to evaluate an expressions and evaluate another expression on exception
log_namespaces

Looks up logger namespaces
as.loglevel

Convert R object into a logger log-level
log_level

Log a message with given log level
layout_gha

Format a log record for github actions
layout_glue

appender_async

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

Evaluate R expression with a temporarily updated log level threshold
logger-package

logger: A Lightweight, Modern and Flexible Logging Utility
log_levels

Log levels
log_shiny_input_changes

Auto logging input changes in Shiny app
log_separator

Logs a long line to stand out from the console
formatter_logging

Mimic the default formatter used in the logging package
appender_console

Append log record to stderr
layout_glue_generator

Generate log layout function using common variables available via glue syntax
log_formatter

Get or set log message formatter
log_failure

Logs the error message to console before failing
formatter_json

Transforms all passed R objects into a JSON list
layout_glue_colors

Format a log message with glue::glue() and ANSI escape codes to add colors
log_threshold

Get or set log level threshold
log_tictoc

Tic-toc logging
appender_syslog

Send log messages to the POSIX system log
appender_ntfy

Send log messages to ntfy
delete_logger_index

Delete an index from a logger namespace
colorize_by_log_level

Color string by the related log level