loxide 0.0.5

A fast, dependency-free structured logging library for Rust with pretty and JSON output, field redaction, and scoped sub-loggers.
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
//! The core [`Logger`] type.

use std::fmt;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};

use crate::config::{Config, Format, Level, WriterKind};
use crate::json::JsonValue;
use crate::time::UtcDateTime;
use crate::{pretty, redact};

/// Field names owned by the record envelope; user fields using these names are
/// dropped so they can never shadow or duplicate the built-in fields.
const RESERVED_FIELDS: [&str; 4] = ["time", "level", "message", "caller"];

/// A cheap-to-clone, thread-safe structured logger.
///
/// A `Logger` bundles a [`Config`] with a set of persistent fields and a shared
/// output sink. Cloning (including via the `with_*` sub-logger methods) is cheap:
/// the output sink is shared through an [`Arc`], while each clone keeps its own
/// copy of the accumulated fields.
///
/// ```
/// use loxide::{Config, Level, Logger, json};
///
/// let logger = Logger::new(Config::default().with_level(Level::Debug));
/// logger.info("service started", &[("port", json!(8080))]);
///
/// // A sub-logger carries extra context on every subsequent record.
/// let request = logger.with_request_id("req-42");
/// request.debug("handling request", &[("path", json!("/health"))]);
/// ```
#[derive(Clone)]
pub struct Logger {
    config: Config,
    fields: Vec<(String, JsonValue)>,
    writer: Arc<Mutex<Box<dyn Write + Send>>>,
    writer_kind: WriterKind,
}

impl Logger {
    /// Creates a logger that writes to standard error (the conventional
    /// destination for diagnostics).
    pub fn new(config: Config) -> Self {
        Self::with_writer_kind(config, Box::new(io::stderr()), WriterKind::Stderr)
    }

    /// Creates a logger that writes to standard output.
    pub fn stdout(config: Config) -> Self {
        Self::with_writer_kind(config, Box::new(io::stdout()), WriterKind::Stdout)
    }

    /// Creates a logger writing to an arbitrary sink. Useful for writing to a
    /// file, an in-memory buffer, or a socket — and for capturing output in
    /// tests.
    pub fn with_writer(config: Config, writer: Box<dyn Write + Send>) -> Self {
        Self::with_writer_kind(config, writer, WriterKind::Other)
    }

    fn with_writer_kind(
        config: Config,
        writer: Box<dyn Write + Send>,
        writer_kind: WriterKind,
    ) -> Self {
        Logger {
            config,
            fields: Vec::new(),
            writer: Arc::new(Mutex::new(writer)),
            writer_kind,
        }
    }

    /// Returns the logger's configuration.
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Returns `true` if a record at `level` would be emitted (i.e. it meets the
    /// configured minimum level). Use this to guard expensive field computation.
    pub fn enabled(&self, level: Level) -> bool {
        level >= self.config.level
    }

    /// Logs `msg` at `level` with the given extra `fields`.
    pub fn log(&self, level: Level, msg: &str, fields: &[(&str, JsonValue)]) {
        if self.enabled(level) {
            self.emit(level, msg, fields, None);
        }
    }

    /// Logs `msg` at `level`, recording an explicit caller location. This is the
    /// entry point used by the `log_*!` macros; the location is only rendered
    /// when [`Config::caller`] is enabled.
    pub fn log_with_caller(
        &self,
        level: Level,
        msg: &str,
        fields: &[(&str, JsonValue)],
        file: &str,
        line: u32,
    ) {
        if self.enabled(level) {
            self.emit(level, msg, fields, Some((file, line)));
        }
    }

    /// Renders and writes a single record.
    fn emit(
        &self,
        level: Level,
        msg: &str,
        extra: &[(&str, JsonValue)],
        caller: Option<(&str, u32)>,
    ) {
        let now = UtcDateTime::now();
        let fields = self.merged_fields(extra);
        let caller = if self.config.caller { caller } else { None };

        let line = match self.config.resolved_format(self.writer_kind) {
            Format::Pretty => self.render_pretty(level, msg, &fields, caller, &now),
            // `Auto` is resolved before we get here; anything non-pretty is JSON.
            _ => Self::render_json(level, msg, &fields, caller, &now),
        };

        // A single locked `writeln!` keeps concurrent records from interleaving.
        if let Ok(mut writer) = self.writer.lock() {
            let _ = writeln!(writer, "{line}");
        }
    }

    /// Merges persistent and per-call fields (per-call wins), strips reserved
    /// names, and applies redaction once so both formats stay consistent.
    fn merged_fields(&self, extra: &[(&str, JsonValue)]) -> Vec<(String, JsonValue)> {
        let mut merged = self.fields.clone();
        for (key, value) in extra {
            merged.retain(|(k, _)| k != *key);
            merged.push(((*key).to_string(), value.clone()));
        }
        merged.retain(|(k, _)| !RESERVED_FIELDS.contains(&k.as_str()));
        merged
            .into_iter()
            .map(|(key, value)| {
                let value = redact::redact_field(&key, value);
                (key, value)
            })
            .collect()
    }

    /// Renders a record as one compact JSON object.
    fn render_json(
        level: Level,
        msg: &str,
        fields: &[(String, JsonValue)],
        caller: Option<(&str, u32)>,
        now: &UtcDateTime,
    ) -> String {
        let mut object: Vec<(String, JsonValue)> = Vec::with_capacity(fields.len() + 4);
        object.push(("time".to_string(), JsonValue::from(now.to_iso8601())));
        object.push(("level".to_string(), JsonValue::from(level.as_str())));
        object.push(("message".to_string(), JsonValue::from(msg)));
        for (key, value) in fields {
            object.push((key.clone(), value.clone()));
        }
        if let Some((file, line)) = caller {
            object.push((
                "caller".to_string(),
                JsonValue::from(format!("{file}:{line}")),
            ));
        }
        JsonValue::Object(object).to_string()
    }

    /// Renders a record as a colored, human-readable line.
    fn render_pretty(
        &self,
        level: Level,
        msg: &str,
        fields: &[(String, JsonValue)],
        caller: Option<(&str, u32)>,
        now: &UtcDateTime,
    ) -> String {
        let nc = self.config.no_color;
        let mut parts = Vec::with_capacity(fields.len() + 4);

        parts.push(pretty::format_timestamp(&now.to_pretty(), nc));
        parts.push(pretty::format_level(level, nc));
        parts.push(msg.to_string());

        // Surface any `error` field prominently, before the rest.
        let mut remaining = fields.to_vec();
        if let Some(idx) = remaining.iter().position(|(k, _)| k == "error") {
            let (_, error) = remaining.remove(idx);
            parts.push(pretty::format_error_field(&scalar_text(&error), nc));
        }
        for (key, value) in &remaining {
            parts.push(pretty::format_field(key, &scalar_text(value), nc));
        }

        if let Some((file, line)) = caller {
            parts.push(pretty::format_caller(&pretty::short_caller(file, line), nc));
        }

        parts.join(" ")
    }

    // --- Sub-loggers ------------------------------------------------------

    /// Returns a sub-logger that tags every record with a `component` field.
    #[must_use]
    pub fn with_component(&self, name: &str) -> Logger {
        self.with_field("component", JsonValue::from(name))
    }

    /// Returns a sub-logger that tags every record with a `request_id` field.
    #[must_use]
    pub fn with_request_id(&self, id: &str) -> Logger {
        self.with_field("request_id", JsonValue::from(id))
    }

    /// Returns a new logger with the given trace ID attached to all records.
    #[must_use]
    pub fn with_trace_id(&self, id: &str) -> Logger {
        self.with_field("trace_id", JsonValue::from(id))
    }

    /// Generates a new UUIDv7 trace ID and attaches it to the logger.
    ///
    /// Requires the `uuid` feature.
    #[cfg(feature = "uuid")]
    #[must_use]
    pub fn with_new_trace_id(&self) -> Logger {
        self.with_field(
            "trace_id",
            JsonValue::from(uuid::Uuid::now_v7().to_string()),
        )
    }

    /// Returns a sub-logger carrying one additional persistent field. The new
    /// logger shares this logger's output sink.
    #[must_use]
    pub fn with_field(&self, key: &str, value: JsonValue) -> Logger {
        let mut fields = self.fields.clone();
        fields.retain(|(k, _)| k != key);
        fields.push((key.to_string(), value));
        Logger {
            config: self.config.clone(),
            fields,
            writer: Arc::clone(&self.writer),
            writer_kind: self.writer_kind,
        }
    }

    // --- Level shortcuts --------------------------------------------------

    /// Logs at [`Level::Trace`].
    pub fn trace(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Trace, msg, fields);
    }

    /// Logs at [`Level::Debug`].
    pub fn debug(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Debug, msg, fields);
    }

    /// Logs at [`Level::Info`].
    pub fn info(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Info, msg, fields);
    }

    /// Logs at [`Level::Success`].
    pub fn success(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Success, msg, fields);
    }

    /// Logs at [`Level::Warn`].
    pub fn warn(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Warn, msg, fields);
    }

    /// Logs at [`Level::Error`].
    pub fn error(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Error, msg, fields);
    }

    /// Logs at [`Level::Fatal`].
    pub fn fatal(&self, msg: &str, fields: &[(&str, JsonValue)]) {
        self.log(Level::Fatal, msg, fields);
    }
}

impl fmt::Debug for Logger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Logger")
            .field("config", &self.config)
            .field("fields", &self.fields)
            .finish_non_exhaustive()
    }
}

/// Renders a [`JsonValue`] for pretty output: strings are shown bare, everything
/// else falls back to its JSON representation.
fn scalar_text(value: &JsonValue) -> String {
    match value {
        JsonValue::String(s) => s.clone(),
        other => other.to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::json;
    use crate::json::from_json_str;

    /// A writer backed by a shared buffer, so tests can inspect the output.
    #[derive(Clone)]
    struct SharedBuf(Arc<Mutex<Vec<u8>>>);

    impl Write for SharedBuf {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.0.lock().unwrap().write(buf)
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    fn logger(level: Level, format: Format) -> (Logger, Arc<Mutex<Vec<u8>>>) {
        let buf = Arc::new(Mutex::new(Vec::new()));
        let config = Config::default()
            .with_level(level)
            .with_format(format)
            .with_no_color(true);
        (
            Logger::with_writer(config, Box::new(SharedBuf(buf.clone()))),
            buf,
        )
    }

    fn text(buf: &Arc<Mutex<Vec<u8>>>) -> String {
        String::from_utf8(buf.lock().unwrap().clone()).unwrap()
    }

    fn last_json(buf: &Arc<Mutex<Vec<u8>>>) -> JsonValue {
        from_json_str(text(buf).trim().lines().last().unwrap()).unwrap()
    }

    #[test]
    fn json_output_carries_fields() {
        let (l, buf) = logger(Level::Info, Format::Json);
        l.info("hello", &[("key", json!("value"))]);
        let data = last_json(&buf);
        assert_eq!(data["message"], "hello");
        assert_eq!(data["level"], "info");
        assert_eq!(data["key"], "value");
        assert!(data["time"].as_str().unwrap().ends_with('Z'));
    }

    #[test]
    fn level_filtering_drops_low_records() {
        let (l, buf) = logger(Level::Warn, Format::Json);
        l.info("filtered", &[]);
        assert!(text(&buf).is_empty());
        assert!(!l.enabled(Level::Info));
        l.warn("kept", &[]);
        assert!(!text(&buf).is_empty());
    }

    #[test]
    fn pretty_output_has_badge_and_fields() {
        let (l, buf) = logger(Level::Info, Format::Pretty);
        l.info("Server started", &[("port", json!(8080))]);
        let out = text(&buf);
        assert!(out.contains("INF"));
        assert!(out.contains("Server started"));
        assert!(out.contains("port=8080"));
    }

    #[test]
    fn pretty_output_has_no_ansi_when_disabled() {
        let (l, buf) = logger(Level::Info, Format::Pretty);
        l.info("test", &[]);
        assert!(!text(&buf).contains('\x1b'));
    }

    #[test]
    fn sub_loggers_accumulate_context() {
        let (l, buf) = logger(Level::Info, Format::Json);
        let sub = l.with_component("auth").with_request_id("req-1");
        sub.info("test", &[]);
        let data = last_json(&buf);
        assert_eq!(data["component"], "auth");
        assert_eq!(data["request_id"], "req-1");
    }

    #[test]
    fn sub_logger_context_persists_across_calls() {
        let (l, buf) = logger(Level::Info, Format::Json);
        let sub = l.with_component("db");
        sub.info("first", &[]);
        sub.warn("second", &[]);
        let out = text(&buf);
        let lines: Vec<&str> = out.trim().lines().collect();
        assert_eq!(from_json_str(lines[0]).unwrap()["component"], "db");
        assert_eq!(from_json_str(lines[1]).unwrap()["component"], "db");
    }

    #[test]
    fn caller_is_recorded_when_enabled() {
        let buf = Arc::new(Mutex::new(Vec::new()));
        let config = Config::default()
            .with_format(Format::Json)
            .with_caller(true)
            .with_no_color(true);
        let l = Logger::with_writer(config, Box::new(SharedBuf(buf.clone())));
        l.log_with_caller(Level::Info, "test", &[], file!(), line!());
        assert!(
            last_json(&buf)["caller"]
                .as_str()
                .unwrap()
                .contains("logger.rs")
        );
    }

    #[test]
    fn per_call_fields_override_persistent_ones() {
        let (l, buf) = logger(Level::Info, Format::Json);
        l.with_field("stage", json!("a"))
            .info("m", &[("stage", json!("b"))]);
        assert_eq!(last_json(&buf)["stage"], "b");
    }

    #[test]
    fn sensitive_fields_are_redacted_in_json() {
        // This is the important guarantee: secrets never reach JSON output raw.
        let (l, buf) = logger(Level::Info, Format::Json);
        l.info("login", &[("password", json!("super_secret"))]);
        let data = last_json(&buf);
        assert_ne!(data["password"], "super_secret");
        assert_eq!(data["password"], "s**********t");
    }

    #[test]
    fn sensitive_fields_are_redacted_in_pretty() {
        let (l, buf) = logger(Level::Info, Format::Pretty);
        l.info("login", &[("api_key", json!("abcdefgh"))]);
        let out = text(&buf);
        assert!(!out.contains("abcdefgh"));
        assert!(out.contains("api_key=a******h"));
    }

    #[test]
    fn reserved_field_names_cannot_be_overridden() {
        let (l, buf) = logger(Level::Info, Format::Json);
        l.info("real message", &[("message", json!("spoofed"))]);
        assert_eq!(last_json(&buf)["message"], "real message");
    }

    #[test]
    fn success_level_emits_suc_badge() {
        let (l, buf) = logger(Level::Trace, Format::Pretty);
        l.success("operation complete", &[]);
        let out = text(&buf);
        assert!(out.contains("SUC"));
        assert!(out.contains("operation complete"));
    }

    #[test]
    fn success_level_emits_correct_json_level() {
        let (l, buf) = logger(Level::Trace, Format::Json);
        l.success("all good", &[("count", json!(7))]);
        let data = last_json(&buf);
        assert_eq!(data["level"], "success");
        assert_eq!(data["message"], "all good");
        assert_eq!(data["count"], 7);
    }

    #[test]
    fn success_is_filtered_below_warn() {
        let (l, buf) = logger(Level::Warn, Format::Json);
        l.success("should be dropped", &[]);
        assert!(text(&buf).is_empty());
    }

    #[test]
    fn trace_id_propagates_to_all_records() {
        let (l, buf) = logger(Level::Info, Format::Json);
        let tl = l.with_trace_id("trace-abc-123");
        tl.info("first", &[]);
        tl.warn("second", &[]);
        let out = text(&buf);
        for line in out.trim().lines() {
            let record = from_json_str(line).unwrap();
            assert_eq!(record["trace_id"], "trace-abc-123");
        }
    }

    #[test]
    #[cfg(feature = "uuid")]
    fn with_new_trace_id_generates_unique_ids() {
        let (base, buf1) = logger(Level::Info, Format::Json);
        let buf2 = Arc::new(Mutex::new(Vec::<u8>::new()));
        let config = Config::default()
            .with_level(Level::Info)
            .with_format(Format::Json)
            .with_no_color(true);
        let base2 = Logger::with_writer(config, Box::new(SharedBuf(buf2.clone())));

        base.with_new_trace_id().info("a", &[]);
        base2.with_new_trace_id().info("b", &[]);

        let id1 = last_json(&buf1)["trace_id"].as_str().unwrap().to_string();
        let id2 = last_json(&buf2)["trace_id"].as_str().unwrap().to_string();

        // Both must be non-empty and distinct.
        assert!(!id1.is_empty(), "trace_id must be set");
        assert_ne!(id1, id2, "each call must produce a unique UUIDv7");
    }
}