1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Provides utilities for setting up structured tracing in tests.
//!
//! This module configures a `tracing` subscriber to capture logs in tests, allowing
//! better debugging and structured log output. It uses `tracing_bunyan_formatter`
//! to format logs as JSON and `tracing_subscriber` to handle filtering and layering.
//!
//! # Features
//! - **Conditional Logging in Tests**: Enables logging if `TEST_LOG` is set.
//! - **Structured Logging**: Uses Bunyan-style JSON formatting.
//! - **Thread-safe Global Subscriber**: Ensures tracing is initialized only once.
//!
//! # Usage
//!
//! To enable logging in your tests, set the `TEST_LOG` environment variable:
//!
//! ```sh
//! TEST_LOG=1 cargo test
//! ```
//!
//! Otherwise, logging will be disabled by default to keep test output clean.
//!
//! # Example
//!
//! ```rust, ignore
//! use tracing::info;
//! use crate::tracing::TEST_TRACING;
//!
//! fn example_test() {
//! Lazy::force(&TEST_TRACING); // Ensure tracing is initialized
//! info!("This will appear in logs if TEST_LOG is set");
//! }
//! ```
//!
//! # Components
//! - [`TEST_TRACING`]: Ensures tracing is initialized once per test suite.
//! - [`get_subscriber`]: Creates a new tracing subscriber with JSON formatting.
//! - [`init_subscriber`]: Sets the created subscriber as the global default.
use Lazy;
use ;
use ;
use LogTracer;
use MakeWriter;
use ;
/// Ensures tracing is initialized once for the test suite.
///
/// This static initializer sets up a global tracing subscriber only if the `TEST_LOG`
/// environment variable is set. If `TEST_LOG` is missing, logs are suppressed.
///
/// # Behavior:
/// - **Enabled (TEST_LOG is set)**: Logs are written to `stdout`.
/// - **Disabled (TEST_LOG is unset)**: Logs are suppressed using `std::io::sink()`.
pub static TEST_TRACING: = new;
/// Creates a `tracing` subscriber with structured JSON logging.
///
/// This function configures a `tracing` subscriber with the following features:
/// - **Environment-based filtering**: Reads from `RUST_LOG`, defaults to provided `env_filter`.
/// - **JSON-formatted output**: Uses `tracing_bunyan_formatter` for structured logging.
/// - **Storage layer**: Adds `JsonStorageLayer` for capturing context values.
///
/// # Arguments
/// - `name`: A name for the logging layer.
/// - `env_filter`: The log level filter (e.g., `"info"`).
/// - `sink`: A writer (e.g., `std::io::stdout` or `std::io::sink()`).
///
/// # Returns
/// A configured `tracing` subscriber.
///
/// # Example
/// ```rust, ignore
/// let subscriber = subscriber("test", "debug", std::io::stdout);
/// init_subscriber(subscriber);
/// ```
+ Sync + Send
where
S0: ,
S1: ,
W: for<'a> + Send + Sync + 'static,
/// Initializes the given tracing subscriber as the global default.
///
/// This function:
/// - **Redirects logs to `tracing`** using `tracing_log::LogTracer::init()`.
/// - **Sets the global subscriber** using `tracing::subscriber::set_global_default()`.
///
/// # Panics
/// Panics if the subscriber cannot be set (e.g., if tracing was already initialized).
///
/// # Example
/// ```rust, ignore
/// let subscriber = subscriber("test", "info", std::io::stdout);
/// init_subscriber(subscriber);
/// ```