Expand description
Simplify logging from Rust to Elixir.
rustler provides fairly seamless integration between Rust and Elixir.
However, Rust console output is emitted without much concern for what Elixir
might be doing with the console. Notably, panic messages can be difficult to
read when using iex. The lack of integration with Elixir’s logging
infrastructure also leaves a hole in observability of tools built with
rustler.
This crate provides some of this missing integration. You get…
- the
log_to_elixirattribute that provides logging functionality in nifs - the
log!macro to actually emit log messages explicitly - a panic handler that routes panic message to Elixir
§Usage
- Use the
log_to_elixirattribute on nifs to enable logging. - Initialize logging with
rustler_logger::log_init()(optional). - Use logging macros!
§Example
use rustler::{nif, Env, Term};
use rustler_logger::{log, log_to_elixir};
#[log_to_elixir]
#[nif]
fn hello() {
log!(
"Hello, ~p! ~p items on your list labeled ~p", // erlang-style format string
"world", 42, "TODO", // format arguments
user="bob", answer=420/10 // log metadata
);
}
// This is necessary to load the panic hook.
fn load(_env: Env, _term: Term) -> bool {
rustler_logger::log_init();
true
}
rustler::init!("Elixir.Your.Module", load = load);§Formatting
The log! macro accepts a format string and a list of arguments, similar to
the format! macro, though the formatting is done by Elixir’s logger using
the notation provided by OTP. It handles arguments of different types
automatically and allows arbitrary expressions for values.
§Metadata
Assignment statements can be used to add metadata for structured logging.
The variable before the = will be turned into an atom to conform to Elixir’s
convention for log message metadata.
§Panic Handling
A panic hook is provided to emit a more detailed panic message to Elixir’s logger. It does not (currently) catch the panic, just resumes panicking.
Macros§
- alert
- send an
alertlevel log entry to the BEAM VM - critical
- send a
criticallevel log entry to the BEAM VM - debug
- send a
debuglevel log entry to the BEAM VM - emergency
- send a
emergencylevel log entry to the BEAM VM - error
- send an
errorlevel log entry to the BEAM VM - fatal
- send a
fatallevel log entry to the BEAM VM - info
- send an
infolevel log entry to the BEAM VM - log
- send a log entry to the BEAM VM
- notice
- send a
noticelevel log entry to the BEAM VM - trace
- send a
tracelevel log entry to the BEAM VM - warn
- send a
warninglevel log entry to the BEAM VM - warning
- send a
warninglevel log entry to the BEAM VM
Structs§
- Log
- A structure to represent log messages.
Enums§
- LogLevel
- An enumeration representing log levels.
Functions§
- log_
init - Initialize Elixir logging.
Attribute Macros§
- log_
to_ elixir - wrapper for
niffunctions needed to set up logging to BEAM