Ниже показан английский оригинал.
Logging
The logging feature prints the service's tracing events on the console, colored, at the verbosity RUST_LOG sets.
RustStream emits tracing events while it dispatches, publishes and starts or stops the service, and a handler emits its own with tracing::info! and its siblings.
Nothing is printed until a tracing subscriber is installed.
logging::init installs the bundled one.
It writes to stderr, so stdout stays clean for the output of a command such as asyncapi gen.
Levels are colored when stderr is a terminal.
RUST_LOG sets the filter in the usual directive syntax; without it, the filter is info.
The main that #[ruststream::app] generates calls init on run, so a service with the attribute needs no setup.
A hand-written main calls init itself, or builds a Logging to change the defaults: the fallback filter, colors, and whether each line names its target.
try_init installs it, and returns LoggingInitError when the filter does not parse or a subscriber is already installed.
A subscriber installed first stays in place: the generated main leaves a service's own stack alone, a JSON formatter or an OpenTelemetry bridge for one.
The events of each delivery and its outcome come from TracingLayer.
Example
The example runs with the logger on:
RUST_LOG=ruststream=debug,info cargo run --example logging --features testing,macros,memory,json,logging -- run
- Macros
- Manual
#[subscriber("orders")]
async fn accept(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "order accepted");
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(accept);
})
}
struct Accept;
impl Handle<Order> for Accept {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(order.id, "order accepted");
ready(Ok(()))
}
}
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(subscriber("orders", Accept).build());
})
}
fn main() -> Result<ExitCode, LoggingInitError> {
// `RUST_LOG` still wins; this filter applies when it is unset.
Logging::new()
.with_default_filter("ruststream=debug,info")
.try_init()?;
// `run_main` finds the logger installed and leaves it in place.
Ok(run_main(app))
}