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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// SPDX-License-Identifier: MIT OR Apache-2.0
/*!
# logwise

An opinionated logging library for Rust with structured logging, privacy-aware data handling,
and hierarchical task-based context management.
# Development Status
logwise is experimental and the API may change.
# The Problem
Traditional logging crates like [`log`](https://crates.io/crates/log) offer generic log levels
(`error`, `warn`, `info`, `debug`, `trace`) that are often too vague for real-world use cases.
This leads to several problems:
* **Ambiguous levels**: Is `debug` for print-style debugging in your library, or for users
debugging their own code?
* **Build control**: How do you compile out expensive logs by default but enable them when
debugging user-reported issues?
* **Missing use cases**: What level is appropriate for "this is slow and should be optimized"?
logwise solves these problems with opinionated, use-case-specific log levels.
# Core Philosophy
Think of log levels like module visibility. You have `pub`, `pub(crate)`, `pub(super)`, and
private visibility for different use cases. logwise provides similar granular control for logging.
# Log Levels
logwise provides specific log levels for defined use cases:
| Level | Use Case | Build Type | Thread Control |
|-------|----------|------------|----------------|
| `trace` | Detailed debugging | debug only | Must enable per-thread via [`Context::begin_trace()`](context::Context::begin_trace) |
| `debuginternal` | Print-style debugging | debug only | On by default in current crate, per-thread in downstream |
| `info` | Supporting downstream crates | debug only | On by default |
| `mandatory` | Printf-style debugging | all builds | Always on |
| `profile` | Profiling output | all builds | Always on |
| `perfwarn` | Performance problems with analysis | all builds | Always on |
| `warning` | Suspicious conditions | all builds | Always on |
| `error` | Logging errors in Results | all builds | Always on |
| `panic` | Programmer errors | all builds | Always on |
# Quick Start
## Basic Logging
```rust
logwise::declare_logging_domain!();
# fn main() {
// Simple structured logging
logwise::info_sync!("User logged in", user_id=42);
// With multiple parameters
logwise::warn_sync!("Request failed",
status=404,
path="/api/users"
);
# }
```
```rust
logwise::declare_logging_domain!();
# fn main() {
// Async logging for better performance
async fn handle_request() {
logwise::info_async!("Processing request",
method="GET",
endpoint="/health"
);
}
# }
```
## Privacy-Aware Logging
logwise's privacy system ensures sensitive data is handled appropriately:
```rust
logwise::declare_logging_domain!();
# fn main() {
use logwise::privacy::{LogIt, IPromiseItsNotPrivate};
#[derive(Debug)]
struct User {
id: u64,
name: String,
email: String,
}
// Complex types require explicit privacy handling
let user = User {
id: 123,
name: "Alice".into(),
email: "alice@example.com".into()
};
// Use LogIt wrapper for complex types
logwise::info_sync!("User created", user=LogIt(&user));
// Mark explicitly non-private data when it's safe
let public_id = "PUBLIC-123";
logwise::info_sync!("Processing {id}",
id=IPromiseItsNotPrivate(public_id)
);
# }
```
## Context and Task Management
Track hierarchical tasks with automatic performance monitoring:
```rust
# fn main() {
use logwise::context::Context;
// Create a new task
let ctx = Context::new_task(
Some(Context::current()),
"data_processing".to_string(),
logwise::Level::Info,
true,
);
ctx.clone().set_current();
// Enable detailed tracing for debugging
Context::begin_trace();
// Nested tasks automatically track hierarchy
let child_ctx = Context::new_task(
Some(Context::current()),
"parse_csv".to_string(),
logwise::Level::Info,
true,
);
child_ctx.clone().set_current();
// Task completion is logged automatically when dropped
# }
```
## Performance Tracking
Use the `perfwarn!` macro to track and log slow operations:
```rust
logwise::declare_logging_domain!();
# fn main() {
# fn perform_database_query() {}
// Tracks execution time automatically
logwise::perfwarn!("database_query", {
// Your expensive operation here
perform_database_query()
});
// Logs warning if operation exceeds threshold
# }
```
For conditional performance warnings that only log when a threshold is exceeded:
```rust
logwise::declare_logging_domain!();
# fn main() {
# use std::time::Duration;
# fn fetch_data() {}
// Only logs if operation takes longer than 100ms
let _interval = logwise::perfwarn_begin_if!(
Duration::from_millis(100),
"slow_operation"
);
fetch_data();
// Warning logged only if threshold exceeded
# }
```
## Heartbeat Monitoring
Use [`heartbeat`] to monitor that operations complete within a deadline:
```rust
# #[cfg(target_arch = "wasm32")]
# wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
# use std::time::Duration;
# fn critical_task() {}
// Create a heartbeat that warns if not completed within 5 seconds
let _guard = logwise::heartbeat("critical_task", Duration::from_secs(5));
critical_task();
// Warning logged if guard is dropped after deadline
```
On `wasm32`, this example uses browser execution because wasm-bindgen
thread spawning is not yet supported in Node.
## Checking Log Level Enablement
Use [`log_enabled!`] to check if a log level is enabled before doing expensive work:
```rust
logwise::declare_logging_domain!();
# fn main() {
use logwise::{Level, log_enabled};
// Skip expensive computation if the log level is disabled
if log_enabled!(Level::Trace) {
let expensive_data = expensive_debug_computation();
logwise::trace_sync!("Debug data: {data}", data=expensive_data);
}
# fn expensive_debug_computation() -> i32 { 42 }
# }
```
## Mandatory Logging
Use [`mandatory_sync!`] or [`mandatory_async!`] for printf-style debugging that is always enabled, even in release builds:
```rust
logwise::declare_logging_domain!();
# fn main() {
// This log will appear in all builds
logwise::mandatory_sync!("Debugging value: {value}", value=42);
# }
```
## Profiling
Use profiling macros to track execution time and performance characteristics:
```rust
logwise::declare_logging_domain!();
# fn main() {
# fn process_data() {}
// Logs duration when the interval is dropped
let _interval = logwise::profile_begin!("data_processing");
process_data();
// Automatically logs completion time
# }
```
For explicit profiling messages:
```rust
logwise::declare_logging_domain!();
# fn main() {
# let elapsed_ms = 42;
logwise::profile_sync!("Computation took {ms} ms", ms=elapsed_ms);
# }
```
# Architecture Overview
## Core Components
* **[`Logger`]**: The trait defining logging backends
* **[`LogRecord`]**: Structured log entry with metadata
* **[`Level`]**: Enumeration of available log levels
* **[`context`] module**: Thread-local hierarchical task management
* **[`privacy`] module**: Privacy-aware data handling system
* **[`global_logger`] module**: Global logger registration and management
* **[`interval`] module**: Performance interval tracking ([`interval::PerfwarnInterval`], [`interval::PerfwarnIntervalIf`], [`interval::ProfileInterval`])
* **[`HeartbeatGuard`]**: Deadline monitoring for operations
## Logging Flow
1. Macros generate a [`LogRecord`] with source location metadata
2. The [`privacy::Loggable`] trait determines how values are logged
3. Records are dispatched to registered [`Logger`] implementations
4. Loggers can process records synchronously or asynchronously
# Examples
## Complete Application Example
```rust
logwise::declare_logging_domain!();
# fn main() {
use logwise::{context::Context, privacy::LogIt};
#[derive(Debug)]
struct Config {
database_url: String,
port: u16,
}
// Initialize root context
Context::reset("application".to_string());
// Log application startup
logwise::info_sync!("Starting application", version="1.0.0");
// Load configuration
let config = Config {
database_url: "postgres://localhost/myapp".into(),
port: 8080,
};
// Use LogIt for complex types
logwise::info_sync!("Configuration loaded",
config=LogIt(&config)
);
// Track performance-critical operations
logwise::perfwarn!("database_connection", {
// connect_to_database(&config.database_url)
});
logwise::info_sync!("Application ready", port=config.port);
# }
```
## Custom Logger Implementation
```rust
use logwise::{Logger, LogRecord};
use std::sync::Arc;
use std::fmt::Debug;
use std::pin::Pin;
use std::future::Future;
#[derive(Debug)]
struct CustomLogger;
impl Logger for CustomLogger {
fn finish_log_record(&self, record: LogRecord) {
// Custom logging logic
eprintln!("[CUSTOM] {}", record);
}
fn finish_log_record_async<'s>(&'s self, record: LogRecord) -> Pin<Box<dyn Future<Output = ()> + Send + 's>> {
Box::pin(async move {
// Async logging logic
eprintln!("[CUSTOM ASYNC] {}", record);
})
}
fn prepare_to_die(&self) {
// Cleanup logic
}
}
// Register the logger
logwise::add_global_logger(Arc::new(CustomLogger));
```
# Thread Safety and Async Support
logwise is designed for concurrent and async environments:
* All [`Logger`] implementations must be `Send + Sync`
* Context is thread-local with parent inheritance
* [`context::ApplyContext`] preserves context across async boundaries
* Both sync and async logging variants are available
# Performance Considerations
* Use async variants (`*_async!`) in async contexts for better performance
* The `perfwarn!` macro includes automatic interval tracking
* Debug-only levels are compiled out in release builds
* Thread-local caching reduces synchronization overhead
# WASM Support
logwise includes WebAssembly support with browser-specific features:
* Uses `web-time` for time operations
* Console output via `web-sys`
* Batched logging with [`InMemoryLogger::periodic_drain_to_console`](InMemoryLogger::periodic_drain_to_console)
*/
declare_logging_domain!;
// Re-export core types and functions for public API
pub use ;
pub use ;
pub use InMemoryLogger;
pub use Level;
pub use LogRecord;
pub use Logger;
// Re-export logging macros from the procedural macro crate.
// See individual macro documentation for usage details.
pub use ;
pub use ;
extern crate self as logwise;
pub use Duration;