pub struct SystemdLayer { /* private fields */ }Expand description
A tracing-subscriber Layer that emits a pretty span-chain line per event.
Construct with SystemdLayer::stdout, then chain with_* methods to
configure formatting, and pass it to a tracing-subscriber::registry().
use tracing_subscriber::prelude::*;
use tracing_systemd::SystemdLayer;
tracing_subscriber::registry()
.with(SystemdLayer::stdout().with_target(true))
.init();Implementations§
Source§impl SystemdLayer
impl SystemdLayer
Sourcepub fn stdout() -> Self
pub fn stdout() -> Self
Construct a layer that writes formatted lines to standard output.
All formatting options have sensible defaults; chain with_*
methods to override them.
Examples found in repository?
12fn main() {
13 tracing_subscriber::registry()
14 .with(
15 SystemdLayer::stdout()
16 .with_target(true)
17 .with_thread_ids(true)
18 .with_level_prefix(false),
19 )
20 .init();
21
22 root_log_fn(true);
23}More examples
11fn main() {
12 // Stdout layer: pretty for humans.
13 let stdout_layer = SystemdLayer::stdout()
14 .with_target(true)
15 .with_level_prefix(false);
16
17 // Journald layer: optional. None if we're not running under systemd.
18 let journald_layer = tracing_systemd::journald::layer_with_identifier("combined-example").ok();
19
20 tracing_subscriber::registry()
21 .with(stdout_layer)
22 .with(journald_layer)
23 .init();
24
25 work(42);
26}Sourcepub fn unit_stdout() -> Self
pub fn unit_stdout() -> Self
Construct a layer that writes formatted lines to a journald-friendly
stream — i.e. stdout with syslog-priority prefixes (<3>–<7>).
journalctl understands these prefixes when no native journal field
is supplied, which is what happens for processes managed by a
systemd unit (their stdout is already routed to the journal).
For direct journal-protocol writes (without going through stdout),
enable the journald feature and use crate::journald::layer.
Sourcepub fn json() -> Self
Available on crate feature json only.
pub fn json() -> Self
json only.Construct a layer that emits a single-line JSON object per event to standard output.
Defaults differ from the pretty-mode constructors: target is on,
timestamps are on with TimestampFormat::Rfc3339, and the syslog
level prefix is off (so each line is a valid standalone JSON object).
Pretty-only builders (with_color_*, separators, brackets, thread-id
prefix/suffix) compile but have no effect in JSON mode.
Schema (per line):
{
"timestamp": "2026-05-05T14:23:45.123Z",
"level": "INFO",
"message": "served request",
"target": "my_app::handlers",
"span_chain": [
{"name": "request", "fields": {"method": "GET"}},
{"name": "handler", "fields": {}}
],
"fields": {"latency_ms": 4}
}thread_id is added at the top level when Self::with_thread_ids
is true. Non-finite f64 values become JSON null, matching
tracing-subscriber’s JSON formatter.
Source§impl SystemdLayer
impl SystemdLayer
Sourcepub fn with_output(self, output: Output) -> Self
pub fn with_output(self, output: Output) -> Self
Override the destination. Useful for Output::stderr() or
Output::writer(buf) (test capture).
Sourcepub fn with_target(self, show: bool) -> Self
pub fn with_target(self, show: bool) -> Self
Show the event’s target (typically the module path) before the span chain.
Default: false.
Examples found in repository?
12fn main() {
13 tracing_subscriber::registry()
14 .with(
15 SystemdLayer::stdout()
16 .with_target(true)
17 .with_thread_ids(true)
18 .with_level_prefix(false),
19 )
20 .init();
21
22 root_log_fn(true);
23}More examples
11fn main() {
12 // Stdout layer: pretty for humans.
13 let stdout_layer = SystemdLayer::stdout()
14 .with_target(true)
15 .with_level_prefix(false);
16
17 // Journald layer: optional. None if we're not running under systemd.
18 let journald_layer = tracing_systemd::journald::layer_with_identifier("combined-example").ok();
19
20 tracing_subscriber::registry()
21 .with(stdout_layer)
22 .with(journald_layer)
23 .init();
24
25 work(42);
26}Sourcepub fn with_thread_ids(self, show: bool) -> Self
pub fn with_thread_ids(self, show: bool) -> Self
Include the OS thread id in each line. Default: false.
Examples found in repository?
More examples
12fn main() {
13 tracing_subscriber::registry()
14 .with(
15 SystemdLayer::stdout()
16 .with_target(true)
17 .with_thread_ids(true)
18 .with_level_prefix(false),
19 )
20 .init();
21
22 root_log_fn(true);
23}Sourcepub fn with_timestamps(self, show: bool) -> Self
pub fn with_timestamps(self, show: bool) -> Self
Show a timestamp at the start of each line. Default: false.
See also SystemdLayer::with_timestamp_format.
Sourcepub fn with_timestamp_format(self, format: TimestampFormat) -> Self
pub fn with_timestamp_format(self, format: TimestampFormat) -> Self
Choose the timestamp format. Implies with_timestamps(true) for any
non-None value. Default: TimestampFormat::None.
Sourcepub fn with_level_prefix(self, use_prefix: bool) -> Self
pub fn with_level_prefix(self, use_prefix: bool) -> Self
Emit a syslog-priority prefix (<3> – <7>) before each line. This
is what journalctl uses to assign a PRIORITY when ingesting plain
stdout from a systemd unit. Default: true.
Examples found in repository?
12fn main() {
13 tracing_subscriber::registry()
14 .with(
15 SystemdLayer::stdout()
16 .with_target(true)
17 .with_thread_ids(true)
18 .with_level_prefix(false),
19 )
20 .init();
21
22 root_log_fn(true);
23}More examples
11fn main() {
12 // Stdout layer: pretty for humans.
13 let stdout_layer = SystemdLayer::stdout()
14 .with_target(true)
15 .with_level_prefix(false);
16
17 // Journald layer: optional. None if we're not running under systemd.
18 let journald_layer = tracing_systemd::journald::layer_with_identifier("combined-example").ok();
19
20 tracing_subscriber::registry()
21 .with(stdout_layer)
22 .with(journald_layer)
23 .init();
24
25 work(42);
26}Sourcepub fn with_span_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
pub fn with_span_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
Separator between spans in the chain. Default: "::".
Sourcepub fn with_message_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
pub fn with_message_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
Separator between the level/span chain and the event message. Default: ": ".
Sourcepub fn with_level_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
pub fn with_level_separator(self, sep: impl Into<Cow<'static, str>>) -> Self
Separator after the level. Default: " ".
Sourcepub fn with_function_bracket_left(self, s: impl Into<Cow<'static, str>>) -> Self
pub fn with_function_bracket_left(self, s: impl Into<Cow<'static, str>>) -> Self
String wrapping the opening of a span argument list. Default: "(".
Sourcepub fn with_function_bracket_right(
self,
s: impl Into<Cow<'static, str>>,
) -> Self
pub fn with_function_bracket_right( self, s: impl Into<Cow<'static, str>>, ) -> Self
String wrapping the closing of a span argument list. Default: ")".
Sourcepub fn with_arguments_equality(self, s: impl Into<Cow<'static, str>>) -> Self
pub fn with_arguments_equality(self, s: impl Into<Cow<'static, str>>) -> Self
String between an argument name and its value. Default: ": ".
Sourcepub fn with_arguments_separator(self, s: impl Into<Cow<'static, str>>) -> Self
pub fn with_arguments_separator(self, s: impl Into<Cow<'static, str>>) -> Self
String between consecutive arguments. Default: ", ".
Sourcepub fn with_thread_id_prefix(self, s: impl Into<Cow<'static, str>>) -> Self
pub fn with_thread_id_prefix(self, s: impl Into<Cow<'static, str>>) -> Self
Prefix for the thread id when with_thread_ids(true). Default: "[".
Sourcepub fn with_thread_id_suffix(self, s: impl Into<Cow<'static, str>>) -> Self
pub fn with_thread_id_suffix(self, s: impl Into<Cow<'static, str>>) -> Self
Suffix for the thread id when with_thread_ids(true). Default: "] ".
Sourcepub fn with_color_mode(self, mode: ColorMode) -> Self
Available on crate feature colors only.
pub fn with_color_mode(self, mode: ColorMode) -> Self
colors only.Set the ColorMode. Default: ColorMode::Auto (color iff TTY
and NO_COLOR is unset).
Sourcepub fn with_color_theme(self, theme: ColorTheme) -> Self
Available on crate feature colors only.
pub fn with_color_theme(self, theme: ColorTheme) -> Self
colors only.Set the ColorTheme. Default: ColorTheme::default (matches 0.1).
Trait Implementations§
Source§impl Debug for SystemdLayer
impl Debug for SystemdLayer
Source§impl Default for SystemdLayer
impl Default for SystemdLayer
Source§impl<S> Layer<S> for SystemdLayerwhere
S: Subscriber + for<'a> LookupSpan<'a>,
impl<S> Layer<S> for SystemdLayerwhere
S: Subscriber + for<'a> LookupSpan<'a>,
Source§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
Attributes and Id.Source§fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
Source§fn on_register_dispatch(&self, subscriber: &Dispatch)
fn on_register_dispatch(&self, subscriber: &Dispatch)
Subscriber. Read moreSource§fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
Subscriber::register_callsite. Read moreSource§fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
true if this layer is interested in a span or event with the
given metadata in the current Context, similarly to
Subscriber::enabled. Read moreSource§fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
Id recorded the given
values.Source§fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
span recorded that it
follows from the span with the ID follows.Source§fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
Source§fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
Source§fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
Layer, returning a Layered
struct implementing Layer. Read moreSource§fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
Layer with the given Subscriber, returning a
Layered struct that implements Subscriber. Read moreSource§fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S>
fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S>
registry and std only.