logger for R

repository·main·Indexed 43 days ago

https://github.com/daroczig/logger

A lightweight, modern, and flexible logging utility for R inspired by Python's logging module and futile.logger. It features glue-style interpolation, vectorization, and extensible layouts and destinations. Key functionality includes log level thresholds via log_threshold(), level-specific functions like log_info() and log_warn(), and support for custom namespaces and logger hierarchies.

Tokens
820
Snippets
3
Records
4
Agent score
6%

What's inside logger

  1. How logger works: Core concepts

    main

    logger is a lightweight, extensible logging utility for R designed to be flexible and modern.

    Key architectural concepts include:

    • Glue-based formatting: By default, it uses glue() for rendering log messages, allowing for easy string interpolation.
    • Vectorization support: It supports passing vectors to be logged on multiple lines.
    • Extensibility: Users can create custom layouts (how a log looks), message formats, and destinations (where logs go).
    • Namespace support: It can automatically find and create custom namespaces for R packages, allowing per-package log level thresholds and formats.
    • Logger Hierarchy: It supports stacking loggers, allowing a single log call to route different levels to different destinations (e.g., TRACE to console, ERROR to DataDog).
  2. Customize log layouts and colors

    main

    You can change how log records are rendered using custom layouts. For example, layout_glue_colors() provides a layout that renders log records with colors. You can also build custom layouts to include specific information like the calling function or R package namespace with specific colors. For advanced customization, refer to the write_custom_extensions vignette.

    library(logger)
    # Use a colored layout
    # Note: layout_glue_colors() is an example of a built-in colored layout
    log_layout(layout_glue_colors())
    log_info("This message will be colored")
  3. Install the logger package

    main

    You can install the stable version of logger from CRAN using install.packages(). For the most recent development version, use pak::pak() from GitHub.

    # Install from CRAN
    install.packages("logger")
    
    # Install development version from GitHub
    # install.packages("pak")
    pak::pak("daroczig/logger")
  4. Basic usage of logger

    main

    The logger package allows you to set a global log level threshold and then log messages using various level-specific functions. It supports glue-style string interpolation for dynamic messages.

    Common log levels include TRACE, DEBUG, INFO, WARN, and ERROR.

    Key functions:

    • log_threshold(level): Sets the minimum log level to be processed.
    • log_info(msg): Logs an informational message.
    • log_warn(msg): Logs a warning message.
    • log_level(level, msg): Logs a message at a specific level programmatically.
    library(logger)
    
    # Set threshold to DEBUG
    log_threshold(DEBUG)
    
    # Log messages
    log_info("Script starting up...")
    
    # Using glue-style interpolation
    count <- 10
    log_info("There are {count} items.")
    
    # Programmatic level selection
    log_level(if (count < 5) TRACE else DEBUG, "Status check")
    
    # Logging warnings
    log_warn("Warning: {1:2} items found")