Skip to main content

SystemdLayer

Struct SystemdLayer 

Source
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

Source

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?
examples/stdout.rs (line 15)
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
Hide additional examples
examples/combined.rs (line 13)
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}
Source

pub fn stderr() -> Self

Construct a layer that writes formatted lines to standard error.

Source

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.

Source

pub fn json() -> Self

Available on crate feature 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.

Examples found in repository?
examples/json.rs (line 14)
12fn main() {
13    tracing_subscriber::registry()
14        .with(SystemdLayer::json().with_thread_ids(true))
15        .init();
16
17    handle_request("GET", "/api/users");
18    handle_request("POST", "/api/orders");
19}
Source§

impl SystemdLayer

Source

pub fn with_output(self, output: Output) -> Self

Override the destination. Useful for Output::stderr() or Output::writer(buf) (test capture).

Source

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?
examples/stdout.rs (line 16)
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
Hide additional examples
examples/combined.rs (line 14)
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}
Source

pub fn with_thread_ids(self, show: bool) -> Self

Include the OS thread id in each line. Default: false.

Examples found in repository?
examples/json.rs (line 14)
12fn main() {
13    tracing_subscriber::registry()
14        .with(SystemdLayer::json().with_thread_ids(true))
15        .init();
16
17    handle_request("GET", "/api/users");
18    handle_request("POST", "/api/orders");
19}
More examples
Hide additional examples
examples/stdout.rs (line 17)
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}
Source

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.

Source

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.

Source

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?
examples/stdout.rs (line 18)
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
Hide additional examples
examples/combined.rs (line 15)
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}
Source

pub fn with_span_separator(self, sep: impl Into<Cow<'static, str>>) -> Self

Separator between spans in the chain. Default: "::".

Source

pub fn with_message_separator(self, sep: impl Into<Cow<'static, str>>) -> Self

Separator between the level/span chain and the event message. Default: ": ".

Source

pub fn with_level_separator(self, sep: impl Into<Cow<'static, str>>) -> Self

Separator after the level. Default: " ".

Source

pub fn with_function_bracket_left(self, s: impl Into<Cow<'static, str>>) -> Self

String wrapping the opening of a span argument list. Default: "(".

Source

pub fn with_function_bracket_right( self, s: impl Into<Cow<'static, str>>, ) -> Self

String wrapping the closing of a span argument list. Default: ")".

Source

pub fn with_arguments_equality(self, s: impl Into<Cow<'static, str>>) -> Self

String between an argument name and its value. Default: ": ".

Source

pub fn with_arguments_separator(self, s: impl Into<Cow<'static, str>>) -> Self

String between consecutive arguments. Default: ", ".

Source

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: "[".

Source

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: "] ".

Source

pub fn with_color_mode(self, mode: ColorMode) -> Self

Available on crate feature colors only.

Set the ColorMode. Default: ColorMode::Auto (color iff TTY and NO_COLOR is unset).

Source

pub fn with_color_theme(self, theme: ColorTheme) -> Self

Available on crate feature colors only.

Set the ColorTheme. Default: ColorTheme::default (matches 0.1).

Trait Implementations§

Source§

impl Debug for SystemdLayer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SystemdLayer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S> Layer<S> for SystemdLayer
where S: Subscriber + for<'a> LookupSpan<'a>,

Source§

fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)

Notifies this layer that a new span was constructed with the given Attributes and Id.
Source§

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)

Notifies this layer that an event has occurred.
Source§

fn on_register_dispatch(&self, subscriber: &Dispatch)

Performs late initialization when installing this layer as a Subscriber. Read more
Source§

fn on_layer(&mut self, subscriber: &mut S)

Performs late initialization when attaching a Layer to a Subscriber. Read more
Source§

fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest

Registers a new callsite with this layer, returning whether or not the layer is interested in being notified about the callsite, similarly to Subscriber::register_callsite. Read more
Source§

fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool

Returns true if this layer is interested in a span or event with the given metadata in the current Context, similarly to Subscriber::enabled. Read more
Source§

fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)

Notifies this layer that a span with the given Id recorded the given values.
Source§

fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span with the ID span recorded that it follows from the span with the ID follows.
Source§

fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, S>) -> bool

Called before on_event, to determine if on_event should be called. Read more
Source§

fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span with the given ID was entered.
Source§

fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)

Notifies this layer that the span with the given ID was exited.
Source§

fn on_close(&self, _id: Id, _ctx: Context<'_, S>)

Notifies this layer that the span with the given ID has been closed.
Source§

fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span ID has been cloned, and that the subscriber returned a different ID.
Source§

fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
where L: Layer<S>, Self: Sized,

Composes this layer around the given Layer, returning a Layered struct implementing Layer. Read more
Source§

fn with_subscriber(self, inner: S) -> Layered<Self, S>
where Self: Sized,

Composes this Layer with the given Subscriber, returning a Layered struct that implements Subscriber. Read more
Source§

fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S>
where Self: Sized, F: Filter<S>,

Available on crate features registry and std only.
Combines self with a Filter, returning a Filtered layer. Read more
Source§

fn boxed(self) -> Box<dyn Layer<S> + Send + Sync>
where Self: Sized + Layer<S> + Send + Sync + 'static, S: Subscriber,

Available on crate features alloc or std only.
Erases the type of this Layer, returning a Boxed dyn Layer trait object. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more