tracing.rs

Overview

This file provides functionality to initialize application-wide logging and tracing using the tracing_subscriber crate. It configures a subscriber that formats logs in a compact style, enriching them with contextual information such as thread names, thread IDs, source file names, and line numbers. The initialization is designed to be idempotent; it prevents panics if tracing has already been initialized elsewhere in the application.

Functions

init_tracing()

pub fn init_tracing()

Initializes the global tracing subscriber with a layered configuration for structured logging.

Parameters

Returns

Description

This function sets up the tracing subscriber registry with a formatting layer (fmt::layer()) that provides:

The function calls try_init() to attempt setting this subscriber as the global default. If tracing has already been initialized, try_init() will return an error, which is ignored (let _ = ...) to prevent panics or crashes.

Usage Example

fn main() {
    tracing_rs::init_tracing();

    tracing::info!("Application started");
}

This example initializes the tracing system, then emits an informational log message that will be formatted and output according to the configured subscriber.

Implementation Details

Interaction with Other Parts of the System

Mermaid Diagram

flowchart TD
A["init_tracing()"] --> B["tracing_subscriber::registry()"]
B --> C["fmt::layer() - compact, thread info, source location, ANSI"]
B --> D[EnvFilter from RUST_LOG]
C & D --> E["try_init()"]
E -->|Success| F[Global subscriber set]
E -->|Error (already set)| G[Ignored]

This diagram illustrates the initialization workflow of the tracing subscriber within the init_tracing function, showing how layers are combined and the final attempt to set the global subscriber.