Skip to main content

Logging & tracing

Enables logging/tracing events via the tracing crate.

Quickstart

  1. Enable the tracing feature.
cargo add strut --features tracing
  1. Optionally, customize the configuration.
  2. Parts of the tracing crate API are available under strut::tracing::*.
use strut::tracing::*;

#[strut::main]
async fn main() {
info!("Running application logic");
}
$ cargo run
1969-10-29T22:30:00.010227Z  INFO ThreadId(01) strut::launchpad::wiring::preflight: Starting app-backend with profile 'dev' (default replica, lifetime ID 'xbr-dwxn-klp')
1969-10-29T22:30:00.010296Z  INFO ThreadId(01) demo_app: Running application logic
1969-10-29T22:30:00.010308Z  INFO ThreadId(01) strut_core::context: Terminating application context
1969-10-29T22:30:00.010325Z  INFO ThreadId(01) strut_core::spindown::registry: Spindown initiated
1969-10-29T22:30:00.010352Z  INFO ThreadId(01) strut_core::spindown::registry: Spindown completed

Cargo features

⛳︎ Feature tracing

This is the main gateway feature of this component.

⛳︎ Feature tracing-log

Automatically consumes log events as tracing events.

⛳︎ Feature tracing-json

Enables writing JSON-formatted structured events to stdout. JSON formatting needs to be selected in the tracing.flavor config key.

Component structure

☑︎ Configuration

Below is a full configuration example. All keys are optional.

config/tracing.yaml
tracing:
verbosity: info # off, error, warn, info (default), debug, trace
flavor: full # full (default), compact, pretty, json (with `tracing-json` feature)
show_color: true
show_timestamp: true
show_target: true
show_file: false
show_line_number: false
show_level: true
show_thread_id: true
show_thread_name: false
flatten_json: true # with `tracing-json` feature
targets:
my_crate: info # off, error, warn, info (default), debug, trace
my_crate::interesting_module: off # same as above

For in-code reference, see TracingConfig.

☑︎ Startup

The startup logic of this component is called from the ConfigurationWiring. It involves configuring a formatted subscriber and initializing it globally.

∅ Facade

This component does not expose a facade.

∅ Spindown

This component does not include any spindown logic.

Sentry integration

If you also use the Sentry component, you can easily create Sentry events from the tracing events. Simply add alert = true key-value field:

use strut::tracing::*;

#[strut::main]
async fn main() {
error!(alert = true, "This will be also sent to Sentry");
}