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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright 2025-2026 Colliery Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! # Logging Configuration
//!
//! This module provides structured logging setup for Cloacina using the `tracing` ecosystem.
//! It supports both production and test environments with configurable log levels.
//!
//! ## Dependencies
//!
//! - `tracing`: Core logging framework
//! - `tracing-subscriber`: Subscriber implementation and formatting
//!
//! ## Features
//!
//! - **Structured Logging**: Uses `tracing` for structured, contextual logging
//! - **Environment Configuration**: Respects `RUST_LOG` environment variable
//! - **Test Support**: Special test logging that doesn't interfere with test output
//! - **Flexible Levels**: Support for all standard log levels (error, warn, info, debug, trace)
//! - **Thread Safety**: All logging operations are thread-safe
//! - **Zero Cost**: When logging is disabled, the compiler eliminates the logging code
//!
//! ## Usage
//!
//! ### Production
//!
//! ```rust,ignore
//! use cloacina::init_logging;
//! use tracing::Level;
//!
//! // Initialize with default level (respects RUST_LOG env var)
//! init_logging(None);
//!
//! // Or specify a level explicitly
//! init_logging(Some(Level::DEBUG));
//!
//! // Log messages with context
//! tracing::info!(target: "my_module", "Processing request", request_id = "123");
//! ```
//!
//! ### Testing
//!
//! In your test functions, call `init_test_logging()` at the start:
//!
//! ```rust,ignore
//! use cloacina::init_test_logging;
//!
//! #[test]
//! fn my_test() {
//! init_test_logging();
//! // Your test code with logging
//! tracing::debug!("Test debug message");
//! }
//! ```
//!
//! ## Log Levels
//!
//! - `ERROR`: Critical errors that may cause system failure
//! - `WARN`: Warning conditions that don't stop execution
//! - `INFO`: General information about system operation
//! - `DEBUG`: Detailed diagnostic information
//! - `TRACE`: Very detailed diagnostic information
//!
//! ## Environment Variables
//!
//! The `RUST_LOG` environment variable supports various formats:
//!
//! - `RUST_LOG=debug` - Enable debug logging for all modules
//! - `RUST_LOG=myapp=trace,other_crate=warn` - Fine-grained control per module
//! - `RUST_LOG=info,myapp::module=debug` - Mix of global and module-specific levels
//!
//! ## Performance Considerations
//!
//! - Logging is disabled at compile time for levels below the configured threshold
//! - Structured fields are only evaluated if the log level is enabled
//! - Test logging uses a special writer that minimizes impact on test performance
//!
//! ## Integration
//!
//! This logging system integrates with:
//!
//! - Standard Rust logging macros (`tracing::info!`, `tracing::error!`, etc.)
//! - Structured field support for JSON logging
//! - Span-based context tracking
//! - Test frameworks for log verification
//!
//! ## Error Handling
//!
//! - Initialization errors are handled gracefully with fallback to default settings
//! - Invalid log levels in environment variables default to "info"
//! - Test logging initialization is idempotent and safe to call multiple times
use Level;
use ;
/// Initializes the logging system with the specified log level.
///
/// If no level is provided, it will use the `RUST_LOG` environment variable
/// or default to "info" if the environment variable is not set.
///
/// # Arguments
///
/// * `level` - Optional log level to use. If `None`, uses environment configuration.
///
/// # Examples
///
/// ```rust,ignore
/// use cloacina::init_logging;
/// use tracing::Level;
///
/// // Use environment variable or default to INFO
/// init_logging(None);
///
/// // Set specific level
/// init_logging(Some(Level::DEBUG));
/// ```
///
/// # Environment Variables
///
/// The `RUST_LOG` environment variable can be used to control logging:
/// - `RUST_LOG=debug` - Enable debug logging
/// - `RUST_LOG=myapp=trace,other_crate=warn` - Fine-grained control
/// Initializes the logging system for test environments.
///
/// This sets up a test-specific subscriber that:
/// - Captures logs for verification in tests
/// - Uses debug level by default
/// - Writes to test output that doesn't interfere with test results
/// - Can be called multiple times safely (subsequent calls are ignored)
///
/// # Examples
///
/// ```rust,ignore
/// use cloacina::init_test_logging;
///
/// #[test]
/// fn test_with_logging() {
/// init_test_logging();
///
/// // Your test code here
/// // Logs will be captured and can be verified if needed
/// }
/// ```
/// Mask the password in a database URL for safe logging.
///
/// Replaces the password portion (between the last `:` before `@` and the `@`)
/// with `****`. If the URL does not contain credentials, returns it unchanged.
///
/// # Examples
///
/// ```
/// use cloacina::logging::mask_db_url;
/// assert_eq!(
/// mask_db_url("postgres://user:secret@localhost/db"),
/// "postgres://user:****@localhost/db"
/// );
/// assert_eq!(
/// mask_db_url("sqlite:///path/to/db"),
/// "sqlite:///path/to/db"
/// );
/// ```