Raxol.Core.Runtime.Log (Raxol Core v2.8.0)

Copy Markdown View Source

Centralized logging system for Raxol with structured context, performance tracking, and consistent formatting across all modules.

Features

  • Structured logging with automatic context enrichment
  • Module-aware logging with automatic module detection
  • Performance and timing utilities
  • Standardized error handling with stacktraces
  • Debug mode support with conditional logging
  • Automatic metadata collection (node, environment, version)
  • IO.puts/inspect migration helpers

Usage

One call style per module: a module that can require Logger uses the Logger macros for every plain level call in that file (the macro skips building the message when the level is disabled), and every other module uses this one.

The functions here are FUNCTIONS: their arguments, inspect/1 included, are evaluated before any level check, at every level. So never pass them inspect of an unbounded term (an app model, a view, an event, the emulator, a GenServer state) or build a message in a per-byte or per-event loop. Log a bounded summary (a module, an event type, a size), or use the Logger macros where the full term is only wanted at :debug. Input a remote peer sends reaches these paths, so an eager dump there is both a CPU amplifier (#1031) and a way for that input to land in the logs.

# Basic logging
Log.info("User authenticated successfully")
Log.error("Database connection failed")

# With context
Log.info("Processing request", %{user_id: 123, action: :login})
Log.error("Validation failed", %{errors: errors, input: input})

# Performance timing
Log.time_info("Database query", fn ->
  expensive_operation()
end)

# Module-aware logging (automatically detects calling module)
Log.info("Operation completed")

# Migration from IO.puts
Log.console("Debug output for development")

Summary

Functions

Log with automatic error classification and severity detection.

Console logging for development - replacement for IO.puts. Logs at :info in development/test environments and at :debug otherwise.

Logs a debug message.

Conditional debug logging based on module configuration.

Logs an error with stacktrace and context.

Structured event logging with automatic metadata enrichment.

Structured inspect logging - replacement for IO.inspect.

Module-aware logging that automatically detects the calling module.

Performance timing logger that measures and logs execution time.

Logs a warning with context.

Types

context()

@type context() :: map() | keyword() | nil

log_level()

@type log_level() :: :debug | :info | :warn | :error

metadata()

@type metadata() :: map()

Functions

auto_log(msg, data \\ nil)

Log with automatic error classification and severity detection.

console(msg, context \\ nil)

Console logging for development - replacement for IO.puts. Logs at :info in development/test environments and at :debug otherwise.

debug(msg)

@spec debug(iodata()) :: :ok

Logs a debug message.

debug(msg, context)

debug_if(condition, msg, context \\ nil)

Conditional debug logging based on module configuration.

debug_with_context(msg, context)

error(msg)

error(msg, context)

error_with_context(msg, context)

error_with_stacktrace(msg, error, stacktrace, context \\ nil)

Logs an error with stacktrace and context.

event(event_type, msg, context \\ %{})

Structured event logging with automatic metadata enrichment.

info(msg)

info(msg, context)

info_with_context(msg)

info_with_context(msg, context)

log_inspect(data, label \\ nil, opts \\ [])

Structured inspect logging - replacement for IO.inspect.

module_debug(msg, context \\ nil)

Module-aware logging that automatically detects the calling module.

module_error(msg, context \\ nil)

module_info(msg, context \\ nil)

module_warning(msg, context \\ nil)

time_debug(msg, func)

Performance timing logger that measures and logs execution time.

time_info(msg, func)

time_warning(msg, func)

warning(msg)

warning(msg, context)

warning_with_context(msg, context)

Logs a warning with context.