7 releases

0.3.0 Jan 25, 2024
0.2.3 Jan 30, 2023
0.2.2 Dec 19, 2022
0.2.1 Apr 8, 2022
0.1.1 Oct 20, 2021

#119 in Debugging

Download history 1018/week @ 2023-12-20 862/week @ 2023-12-27 2093/week @ 2024-01-03 2103/week @ 2024-01-10 2720/week @ 2024-01-17 3638/week @ 2024-01-24 3261/week @ 2024-01-31 1521/week @ 2024-02-07 2647/week @ 2024-02-14 1602/week @ 2024-02-21 2122/week @ 2024-02-28 2720/week @ 2024-03-06 3069/week @ 2024-03-13 2558/week @ 2024-03-20 2556/week @ 2024-03-27 2773/week @ 2024-04-03

11,392 downloads per month

Apache-2.0

19KB
191 lines

slog-dtrace

Latest Version Documentation

Forward slog messages as DTrace USDT probes.

Overview

Logging is invaluable in production applications. However, it presents a bit of a quandary. Most of the time, only informational or error messages are useful. But when an application crashes or is misbehaving, it can be extremely useful to retrieve more verbose logging information. Unfortunately, this can normally only be accomplished by restarting the process with a new log level.

This crate allows applications to attach a slog::Drain, the Dtrace drain, to their loggers that forwards all messages to DTrace. This is done with a usdt probe function, with different probes indicating different log levels.

Note that the Dtrace drain will only send messages to DTrace, but in most situations, one is already sending log messages to some location (stdout, file, syslog, etc.). The with_drain constructor can be used to generate a Dtrace drain that will forward messages to an existing drain as well as to DTrace.

The DTrace probe that emits log messages is efficient. In particular, when the probe is disabled, it incurs no cost beyond that of any other drain(s) in the hierarchy. However, when the probe is enabled, every message, regardless of log-level, can be viewed in DTrace.

Example

$ cargo +nightly run --example simple

You can see that only warning messages are printed in the terminal. However, running a DTrace command in another shell should reveal more messages.

# dtrace -Z -n 'slog*::: { printf("%s\n", copyinstr(arg0)); }' -q
{"ok": {"location":{"module":"simple","file":"examples/simple.rs","line":15},"level":"WARN","timestamp":"2021-10-19T17:55:55.260393409Z","message":"a warning message for everyone","kv":{"cool":true,"key":"value"}}}
{"ok": {"location":{"module":"simple","file":"examples/simple.rs","line":16},"level":"INFO","timestamp":"2021-10-19T17:55:55.260531762Z","message":"info is just for dtrace","kv":{"cool":true,"hello":"from dtrace","key":"value"}}}
{"ok": {"location":{"module":"simple","file":"examples/simple.rs","line":17},"level":"DEBUG","timestamp":"2021-10-19T17:55:55.260579423Z","message":"only dtrace gets debug messages","kv":{"cool":true,"hello":"from dtrace","key":"value"}}}

We can see both the warning messages that the example's stdout prints, but also an info and debug message. There are specific probes for each logging level, allowing users to run DTrace actions in response to specific levels of messages. For example, this DTrace command receives just messages emitted via the debug! logging macro.

# dtrace -Z -n 'slog*:::debug { printf("%s\n", copyinstr(arg0)); }' -q
{"ok": {"location":{"module":"simple","file":"examples/simple.rs","line":17},"level":"DEBUG","timestamp":"2021-10-19T17:57:30.578681933Z","message":"only dtrace gets debug messages","kv":{"cool":true,"hello":"from dtrace","key":"value"}}}

Notes

This crate inherits a reliance on a nightly toolchain from the usdt crate.

Dependencies

~7MB
~128K SLoC