logwise 0.5.1

an opinionated logging library for Rust
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Privacy-aware logging system for logwise.
//!
//! This module provides the foundation for logwise's dual-representation logging system,
//! which allows the same log statement to produce different output depending on privacy
//! requirements. This is crucial for maintaining user privacy while still enabling
//! effective debugging and monitoring.
//!
//! # Core Concepts
//!
//! The privacy system revolves around the [`Loggable`] trait, which defines two methods
//! for logging:
//!
//! - `log_redacting_private_info()`: Produces safe output for remote servers
//! - `log_all()`: Produces complete output for local/private logging
//!
//! # Privacy Philosophy
//!
//! The library takes an opinionated stance on what data types are considered potentially
//! private:
//!
//! - **Small integers** (`u8`, `i8`): Considered safe (often enum discriminants, counts)
//! - **Larger numeric types**: Considered potentially private (could be IDs, timestamps)
//! - **Strings**: Always considered potentially private
//! - **Booleans and chars**: Considered safe
//!
//! # Usage Patterns
//!
//! ## Basic Usage
//!
//! ```
//! # use logwise::privacy::Loggable;
//! # use logwise::LogRecord;
//! # use logwise::Level;
//! # let mut record = LogRecord::new(Level::Info);
//! // Numeric types have built-in privacy awareness
//! let count: u8 = 5;  // Safe to log
//! let user_id: u64 = 12345;  // Will be redacted
//!
//! count.log_redacting_private_info(&mut record);  // Logs "5"
//! user_id.log_redacting_private_info(&mut record); // Logs "<u64>"
//! ```
//!
//! ## Privacy Wrappers
//!
//! When you need explicit control over privacy:
//!
//! ```
//! # use logwise::privacy::{LogIt, IPromiseItsNotPrivate};
//! // Force private logging (never sent to remote servers)
//! let sensitive = LogIt("credit_card_number");
//!
//! // Promise data is safe for remote logging
//! let safe = IPromiseItsNotPrivate("public_event_name");
//! ```
//!
//! ## Integration with Logging Macros
//!
//! ```
//! logwise::declare_logging_domain!();
//! # fn main() {
//! # let user_id = "user123";
//! # let request_id = 42u32;
//! // The macros automatically use the Loggable trait
//! logwise::info_sync!("Processing request {id}", id=request_id);
//!
//! // Use wrappers for explicit control
//! logwise::info_sync!("User {user} logged in",
//!                     user=logwise::privacy::LogIt(user_id));
//! # }
//! ```

use crate::log_record::LogRecord;
use std::fmt::Debug;

/// Trait for types that can be logged with privacy awareness.
///
/// This trait enables dual-representation logging, where the same value can be
/// logged differently depending on whether it's being sent to a remote server
/// (where privacy matters) or being logged locally (where full details are useful).
///
/// # Implementation Guidelines
///
/// When implementing this trait:
/// - Use `#[inline]` on both methods for performance
/// - `log_redacting_private_info` should redact any potentially sensitive data
/// - `log_all` should provide complete information for debugging
/// - Consider whether your type contains user data, IDs, or other sensitive information
///
/// # Example Implementation
///
/// ```
/// use logwise::privacy::Loggable;
/// use logwise::LogRecord;
///
/// struct UserId(u64);
///
/// impl Loggable for UserId {
///     #[inline]
///     fn log_redacting_private_info(&self, record: &mut LogRecord) {
///         // Redact the actual ID for remote logging
///         record.log("<UserId>");
///     }
///     
///     #[inline]
///     fn log_all(&self, record: &mut LogRecord) {
///         // Log the full ID for local debugging
///         record.log_owned(format!("UserId({})", self.0));
///     }
/// }
/// ```
pub trait Loggable {
    /// Logs the object with privacy redaction for remote servers.
    ///
    /// This method should produce a representation that is safe to send to
    /// remote logging servers. Any potentially sensitive information should
    /// be redacted or replaced with type indicators.
    ///
    /// # Performance Note
    ///
    /// Implementations should use `#[inline]` for optimal performance.
    fn log_redacting_private_info(&self, record: &mut LogRecord);

    /// Logs the complete object for private/local logging.
    ///
    /// This method should produce a complete representation suitable for
    /// local debugging and troubleshooting. All information should be included.
    ///
    /// # Performance Note
    ///
    /// Implementations should use `#[inline]` for optimal performance.
    fn log_all(&self, record: &mut LogRecord);
}

/// Implementation for `u8` - considered safe to log.
///
/// Small integers like `u8` are typically used for counts, enum discriminants,
/// or other non-sensitive values, so they are logged in full even when redacting.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let count: u8 = 42;
/// count.log_redacting_private_info(&mut record); // Logs "42"
/// count.log_all(&mut record);                    // Also logs "42"
/// ```
impl Loggable for u8 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        self.log_redacting_private_info(record);
    }
}

/// Implementation for `u16` - considered potentially private.
///
/// Values like `u16` might contain IDs, ports, or other sensitive information,
/// so they are redacted when logging to remote servers.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let port: u16 = 8080;
/// port.log_redacting_private_info(&mut record); // Logs "<u16>"
/// port.log_all(&mut record);                    // Logs "8080"
/// ```
impl Loggable for u16 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<u16>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `u32` - considered potentially private.
///
/// Values like `u32` might contain IDs, timestamps, or other sensitive information.
impl Loggable for u32 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<u32>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `u64` - considered potentially private.
///
/// Large integers often represent IDs, timestamps, or other sensitive values.
impl Loggable for u64 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<u64>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `usize` - considered potentially private.
///
/// Size values might reveal information about data structures or memory usage.
impl Loggable for usize {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<usize>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `u128` - considered potentially private.
///
/// Very large integers are often UUIDs or other unique identifiers.
impl Loggable for u128 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<u128>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `i8` - considered safe to log.
///
/// Small signed integers are typically used for small counts or enum values.
impl Loggable for i8 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        self.log_redacting_private_info(record);
    }
}

/// Implementation for `i16` - considered potentially private.
impl Loggable for i16 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<i16>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `i32` - considered potentially private.
impl Loggable for i32 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<i32>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `i64` - considered potentially private.
impl Loggable for i64 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<i64>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `i128` - considered potentially private.
impl Loggable for i128 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<i128>")
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `f32` - considered potentially private.
///
/// Floating point values might contain coordinates, measurements, or calculations.
impl Loggable for f32 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<f32>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `f64` - considered potentially private.
///
/// Double precision floats often contain precise measurements or coordinates.
impl Loggable for f64 {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<f64>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `bool` - considered safe to log.
///
/// Boolean values are simple flags and are always safe to log.
impl Loggable for bool {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `char` - considered safe to log.
///
/// Individual characters are typically safe to log.
impl Loggable for char {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{}", self));
    }
}

/// Implementation for `String` - considered potentially private.
///
/// Strings often contain user data, names, or other sensitive information,
/// so they are always redacted when logging to remote servers.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let username = String::from("alice");
/// username.log_redacting_private_info(&mut record); // Logs "<String>"
/// username.log_all(&mut record);                    // Logs "alice"
/// ```
impl Loggable for String {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<String>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(self.clone());
    }
}

/// Implementation for `&str` - considered potentially private.
///
/// String slices often contain user data or other sensitive information,
/// so they are always redacted when logging to remote servers.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let message = "sensitive data";
/// message.log_redacting_private_info(&mut record); // Logs "<&str>"
/// message.log_all(&mut record);                    // Logs "sensitive data"
/// ```
impl Loggable for &str {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<&str>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(self.to_string());
    }
}

/// Implementation for slices - privacy depends on the element type.
///
/// Slices respect the privacy settings of their contained type.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let safe_values: &[u8] = &[1, 2, 3];
/// let private_values: &[u32] = &[100, 200, 300];
///
/// safe_values.log_redacting_private_info(&mut record);    // Logs "<[1, 2, 3, ]>"
/// private_values.log_redacting_private_info(&mut record); // Logs "<[<u32>, <u32>, <u32>, ]>"
/// ```
impl<T: Loggable> Loggable for &[T] {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<[");
        for item in self.iter() {
            item.log_redacting_private_info(record);
            record.log(", ");
        }
        record.log("]>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log("<[");
        for item in self.iter() {
            item.log_all(record);
            record.log(", ");
        }
        record.log("]>");
    }
}

/// Implementation for `Option<T>` - privacy depends on the inner type.
///
/// Options respect the privacy settings of their contained type.
///
/// # Example
///
/// ```
/// # use logwise::privacy::Loggable;
/// # use logwise::LogRecord;
/// # use logwise::Level;
/// # let mut record = LogRecord::new(Level::Info);
/// let maybe_id: Option<u64> = Some(12345);
/// let nothing: Option<String> = None;
///
/// maybe_id.log_redacting_private_info(&mut record); // Logs "Some(<u64>)"
/// nothing.log_redacting_private_info(&mut record);  // Logs "None"
/// ```
impl<T: Loggable> Loggable for Option<T> {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        match self {
            Some(t) => {
                record.log("Some(");
                t.log_redacting_private_info(record);
                record.log(")");
            }
            None => record.log("None"),
        }
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        match self {
            Some(t) => {
                record.log("Some(");
                t.log_all(record);
                record.log(")");
            }
            None => record.log("None"),
        }
    }
}

/// A wrapper that ensures data is never sent to remote servers.
///
/// `LogIt` is used when you have data that should be logged locally for debugging
/// but must never be sent to remote logging servers. The wrapped value will be
/// logged in full detail locally but replaced with `<LogIt>` for remote logging.
///
/// # When to Use
///
/// Use `LogIt` when:
/// - The data contains sensitive information (passwords, tokens, PII)
/// - You need the data for local debugging but it's not safe for remote storage
/// - The type doesn't implement `Loggable` but implements `Debug`
///
/// # Example
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::privacy::LogIt;
///
/// #[derive(Debug)]
/// struct CreditCard {
///     number: String,
///     cvv: String,
/// }
///
/// let card = CreditCard {
///     number: "1234-5678-9012-3456".to_string(),
///     cvv: "123".to_string(),
/// };
///
/// // This will log the full credit card locally but "<LogIt>" remotely
/// logwise::info_sync!("Processing payment with {card}",
///                     card=LogIt(card));
/// # }
/// ```
///
/// # Integration with Complex Types
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::privacy::LogIt;
/// use std::collections::HashMap;
///
/// let mut sensitive_data = HashMap::new();
/// sensitive_data.insert("ssn", "123-45-6789");
/// sensitive_data.insert("dob", "1990-01-01");
///
/// // Wrap the entire HashMap to protect all contents
/// logwise::info_sync!("User data: {data}",
///                              data=LogIt(&sensitive_data));
/// # }
/// ```
pub struct LogIt<T>(pub T);

impl<T: Debug> Loggable for LogIt<T> {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        record.log("<LogIt>");
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{:?}", self.0));
    }
}

/// A wrapper that explicitly marks data as safe for remote logging.
///
/// `IPromiseItsNotPrivate` is used when you have data that would normally be
/// redacted (like strings or large integers) but you know it's safe to log
/// to remote servers. This is a promise to the logging system that the wrapped
/// value contains no sensitive information.
///
/// # When to Use
///
/// Use `IPromiseItsNotPrivate` when:
/// - You have public identifiers that happen to be strings
/// - You have non-sensitive numeric IDs stored in normally-redacted types
/// - You need to log metadata that the privacy system would normally redact
///
/// # Safety Contract
///
/// By using this wrapper, you are making an explicit promise that the data
/// contains no private or sensitive information. Use with caution!
///
/// # Example
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::privacy::IPromiseItsNotPrivate;
///
/// // These would normally be redacted as strings
/// let event_type = "user_login";
/// let server_name = "api-server-01";
///
/// // But we know these are safe metadata, not user data
/// logwise::info_sync!("Event {event} on {server}",
///                     event=IPromiseItsNotPrivate(event_type),
///                     server=IPromiseItsNotPrivate(server_name));
/// # }
/// ```
///
/// # Example with IDs
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::privacy::IPromiseItsNotPrivate;
///
/// // This would normally be redacted as a u64
/// let public_product_id: u64 = 789456;
///
/// // But we know product IDs are public information
/// logwise::warn_sync!("Product {id} is out of stock",
///                     id=IPromiseItsNotPrivate(public_product_id));
/// # }
/// ```
///
/// # Contrast with Private Data
///
/// ```
/// logwise::declare_logging_domain!();
/// # fn main() {
/// use logwise::privacy::{IPromiseItsNotPrivate, LogIt};
///
/// let public_error_code = "ERR_404";  // Safe to log
/// let user_email = "user@example.com"; // Private data
///
/// // Correct usage:
/// logwise::error_sync!("Error {code} for user {user}",
///                      code=IPromiseItsNotPrivate(public_error_code),
///                      user=LogIt(user_email));
///
/// // WRONG - Don't do this! User email is private:
/// // code=IPromiseItsNotPrivate(user_email)  // DON'T DO THIS!
/// # }
/// ```
pub struct IPromiseItsNotPrivate<T>(pub T);

impl<T: Debug> Loggable for IPromiseItsNotPrivate<T> {
    #[inline]
    fn log_redacting_private_info(&self, record: &mut LogRecord) {
        Self::log_all(self, record);
    }
    #[inline]
    fn log_all(&self, record: &mut LogRecord) {
        record.log_owned(format!("{:?}", self.0));
    }
}