consola 0.0.0-alpha.0

🐨 Elegant Console Logger for Rust and Browser
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
use crate::clock::{Clock, SystemClock};
use crate::format::{
    FormatOptions, build_basic_segments, compute_line_width, detect_terminal_width,
};
use crate::levels::LogLevel;
use crate::record::{ArgValue, LogRecord, RecordDefaults};
use crate::throttling::{ThrottleConfig, Throttler};
use std::collections::VecDeque;
use std::io::{self, Write};
use std::sync::Arc;

pub trait Reporter: Send + Sync {
    fn emit(&self, record: &LogRecord, w: &mut dyn Write) -> io::Result<()>;
}

pub trait ReporterWithOptions {
    fn fmt_options(&self) -> &FormatOptions;
    fn fmt_options_mut(&mut self) -> &mut FormatOptions;
}

#[derive(Default)]
pub struct BasicReporter {
    pub opts: FormatOptions,
}

pub struct FancyReporter {
    pub opts: FormatOptions,
}

impl Reporter for BasicReporter {
    fn emit(&self, record: &LogRecord, w: &mut dyn Write) -> io::Result<()> {
        let segments = build_basic_segments(record, &self.opts);
        let cols = self.opts.columns.or_else(detect_terminal_width);
        // Build plain parts for potential future width computations (currently unused beyond join length logic)
        let mut plain_parts: Vec<String> = Vec::new();
        for (i, seg) in segments.iter().enumerate() {
            if i > 0 {
                plain_parts.push(" ".into());
            }
            plain_parts.push(seg.text.clone());
        }
        let width = cols.unwrap_or(usize::MAX);
        if width == usize::MAX || compute_line_width(&segments, &self.opts) <= width {
            // Single line output
            let mut out = String::new();
            for (i, seg) in segments.iter().enumerate() {
                if i > 0 {
                    out.push(' ');
                }
                if self.opts.colors {
                    out.push_str(&apply_style(&seg.text, seg.style.as_ref()));
                } else {
                    out.push_str(&seg.text);
                }
            }
            out.push('\n');
            w.write_all(out.as_bytes())
        } else {
            // Wrap naive by chars; future: width-aware segmentation
            let mut current = String::new();
            let mut current_len = 0usize;
            let mut first_segment = true;
            for seg in &segments {
                let raw = &seg.text;
                let styled = if self.opts.colors {
                    apply_style(raw, seg.style.as_ref())
                } else {
                    raw.clone()
                };
                let piece_len = raw.chars().count() + if !first_segment { 1 } else { 0 };
                if current_len + piece_len > width && !current.is_empty() {
                    current.push('\n');
                    w.write_all(current.as_bytes())?;
                    current.clear();
                    current_len = 0;
                    first_segment = true;
                }
                if !first_segment {
                    current.push(' ');
                    current_len += 1;
                }
                current.push_str(&styled);
                current_len += raw.chars().count();
                first_segment = false;
            }
            if !current.ends_with('\n') {
                current.push('\n');
            }
            w.write_all(current.as_bytes())
        }
    }
}

impl Reporter for FancyReporter {
    fn emit(&self, record: &LogRecord, w: &mut dyn Write) -> io::Result<()> {
        // Special handling for box type logs with colored frames
        if record.type_name == "box" {
            return self.emit_box(record, w);
        }

        let mut segs = build_basic_segments(record, &self.opts);
        // Prepend icon badge based on type with ASCII fallback
        let (unicode_icon, ascii_icon) = match record.type_name.as_str() {
            "info" => ("", "i"),
            "success" => ("", "+"),
            "error" | "fail" | "fatal" => ("", "x"),
            "warn" => ("", "!"),
            "debug" => ("🐛", "d"),
            "trace" => ("", ">"),
            _ => ("", ""),
        };
        let chosen_icon = if self.opts.unicode {
            unicode_icon
        } else {
            ascii_icon
        };
        if !chosen_icon.is_empty() {
            segs.insert(
                0,
                crate::format::Segment {
                    text: chosen_icon.to_string(),
                    style: Some(crate::format::SegmentStyle {
                        fg_color: Some(icon_color(record).to_string()),
                        bg_color: None,
                        bold: true,
                        dim: false,
                        italic: false,
                        underline: false,
                    }),
                },
            );
        }
        // Badge formatting: find type segment like "[type]" and uppercase inside with background color
        if self.opts.show_type {
            for s in &mut segs {
                if s.text.starts_with('[') && s.text.ends_with(']') && s.text.len() > 2 {
                    let inner = &s.text[1..s.text.len() - 1];
                    // heuristically ensure it matches record.type_name
                    if inner.eq_ignore_ascii_case(&record.type_name) {
                        s.text = format!(" {} ", inner.to_ascii_uppercase());
                        if let Some(style) = &mut s.style {
                            style.bold = true;
                            style.fg_color = Some("white".to_string());
                            style.bg_color = Some(badge_bg_color(record).to_string());
                        } else {
                            s.style = Some(crate::format::SegmentStyle {
                                fg_color: Some("white".to_string()),
                                bg_color: Some(badge_bg_color(record).to_string()),
                                bold: true,
                                dim: false,
                                italic: false,
                                underline: false,
                            });
                        }
                    }
                    break;
                }
            }
        }
        // Adjust repetition style to dim fully
        for s in &mut segs {
            if s.text.starts_with("(x") || s.text.starts_with(" (x") {
                if let Some(st) = &mut s.style {
                    st.dim = true;
                }
            }
        }
        // Width wrapping similar to BasicReporter
        let cols = self.opts.columns.or_else(detect_terminal_width);
        let width = cols.unwrap_or(usize::MAX);
        if width == usize::MAX || compute_line_width(&segs, &self.opts) <= width {
            let mut out = String::new();
            for (i, seg) in segs.iter().enumerate() {
                if i > 0 {
                    out.push(' ');
                }
                if self.opts.colors {
                    out.push_str(&apply_style(&seg.text, seg.style.as_ref()));
                } else {
                    out.push_str(&seg.text);
                }
            }
            out.push('\n');
            w.write_all(out.as_bytes())
        } else {
            let mut current = String::new();
            let mut current_len = 0usize;
            let mut first_segment = true;
            for seg in &segs {
                let raw = &seg.text;
                let styled = if self.opts.colors {
                    apply_style(raw, seg.style.as_ref())
                } else {
                    raw.clone()
                };
                let piece_len = raw.chars().count() + if !first_segment { 1 } else { 0 };
                if current_len + piece_len > width && !current.is_empty() {
                    current.push('\n');
                    w.write_all(current.as_bytes())?;
                    current.clear();
                    current_len = 0;
                    first_segment = true;
                }
                if !first_segment {
                    current.push(' ');
                    current_len += 1;
                }
                current.push_str(&styled);
                current_len += raw.chars().count();
                first_segment = false;
            }
            if !current.ends_with('\n') {
                current.push('\n');
            }
            w.write_all(current.as_bytes())
        }
    }
}

impl FancyReporter {
    /// Special emit for box-type logs with colored frames
    fn emit_box(&self, record: &LogRecord, w: &mut dyn Write) -> io::Result<()> {
        use crate::utils::BoxBuilder;

        // Extract title from message if present
        let title = record.message.as_deref().unwrap_or("");

        // Collect content lines from args
        let mut content_lines = Vec::new();
        for arg in &record.args {
            content_lines.push(arg.to_string());
        }

        // Build the box
        let width = self
            .opts
            .columns
            .or_else(detect_terminal_width)
            .unwrap_or(80);
        let box_builder = BoxBuilder::new(self.opts.unicode).with_width(width.saturating_sub(4));
        let box_lines = box_builder.build(title, &content_lines);

        // Apply colors if enabled
        for line in box_lines {
            if self.opts.colors {
                // Apply cyan color to box borders
                let styled = apply_style(
                    &line,
                    Some(&crate::format::SegmentStyle {
                        fg_color: Some("cyan".to_string()),
                        bg_color: None,
                        bold: false,
                        dim: false,
                        italic: false,
                        underline: false,
                    }),
                );
                writeln!(w, "{}", styled)?;
            } else {
                writeln!(w, "{}", line)?;
            }
        }

        Ok(())
    }
}

fn icon_color(record: &LogRecord) -> &'static str {
    match record.type_name.as_str() {
        "error" | "fail" | "fatal" => "red",
        "success" => "green",
        "warn" => "yellow",
        "info" => "cyan",
        "debug" => "magenta",
        "trace" => "blue",
        _ => "white",
    }
}

fn badge_bg_color(record: &LogRecord) -> &'static str {
    match record.type_name.as_str() {
        "error" | "fail" | "fatal" => "bg_red",
        "success" => "bg_green",
        "warn" => "bg_yellow",
        "info" => "bg_cyan",
        "debug" => "bg_magenta",
        "trace" => "bg_blue",
        _ => "bg_white",
    }
}

fn apply_style(text: &str, style: Option<&crate::format::SegmentStyle>) -> String {
    use std::fmt::Write as _;
    if style.is_none() {
        return text.to_string();
    }
    let s = style.unwrap();
    let mut out = String::new();
    let mut codes: Vec<&str> = Vec::new();
    if let Some(color) = &s.fg_color {
        if let Some(c) = map_color(color) {
            codes.push(c);
        }
    }
    if s.bold {
        codes.push("1");
    }
    if s.dim {
        codes.push("2");
    }
    if s.italic {
        codes.push("3");
    }
    if s.underline {
        codes.push("4");
    }
    if codes.is_empty() {
        return text.to_string();
    }
    write!(&mut out, "\x1b[{}m{}\x1b[0m", codes.join(";"), text).ok();
    out
}

fn map_color(name: &str) -> Option<&'static str> {
    match name {
        "gray" => Some("90"),
        "red" => Some("31"),
        "green" => Some("32"),
        "yellow" => Some("33"),
        "blue" => Some("34"),
        "magenta" => Some("35"),
        "cyan" => Some("36"),
        _ => None,
    }
}

struct Pending(LogRecord);

pub struct LoggerConfig {
    pub level: LogLevel,
    pub throttle: ThrottleConfig,
    pub queue_capacity: Option<usize>,
    pub clock: Option<Box<dyn Clock>>, // if None, SystemClock
}
impl Default for LoggerConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::VERBOSE,
            throttle: ThrottleConfig::default(),
            queue_capacity: None,
            clock: None,
        }
    }
}

type MockFn = Box<dyn Fn(&LogRecord) + Send + Sync>;

pub struct Logger<R: Reporter + 'static> {
    cfg: LoggerConfig,
    reporter: R,
    throttler: Throttler,
    paused: bool,
    queue: VecDeque<Pending>,
    system_clock: SystemClock,
    mock_fn: Option<MockFn>,
}

impl<R: Reporter + 'static> Logger<R> {
    pub fn new(reporter: R) -> Self {
        Self {
            cfg: LoggerConfig::default(),
            reporter,
            throttler: Throttler::new(ThrottleConfig::default()),
            paused: false,
            queue: VecDeque::new(),
            system_clock: SystemClock,
            mock_fn: None,
        }
    }

    pub fn with_config(mut self, cfg: LoggerConfig) -> Self {
        self.throttler = Throttler::new(cfg.throttle.clone());
        self.cfg = cfg;
        self
    }

    pub fn set_level(&mut self, level: LogLevel) {
        self.cfg.level = level;
    }

    // Temporary accessor needed by tests; future: expose builder pattern.
    pub fn opts_mut(&mut self) -> &mut FormatOptions
    where
        R: ReporterWithOptions,
    {
        self.reporter.fmt_options_mut()
    }

    pub fn level(&self) -> LogLevel {
        self.cfg.level
    }

    pub fn reporter(&self) -> &R {
        &self.reporter
    }

    pub fn log<I, A>(&mut self, type_name: &str, tag: Option<String>, args: I)
    where
        I: IntoIterator<Item = A>,
        A: Into<ArgValue>,
    {
        let args_vec: Vec<ArgValue> = args.into_iter().map(Into::into).collect();
        let now = self
            .cfg
            .clock
            .as_ref()
            .map(|c| c.now())
            .unwrap_or_else(|| self.system_clock.now());
        let record = LogRecord::new_with_timestamp(type_name, tag, args_vec, now);
        if !self.passes_level(&record) {
            return;
        }
        if self.paused {
            self.enqueue(record);
            return;
        }
        self.process_record(record);
    }

    pub fn log_raw(&mut self, type_name: &str, tag: Option<String>, message: &str) {
        let now = self
            .cfg
            .clock
            .as_ref()
            .map(|c| c.now())
            .unwrap_or_else(|| self.system_clock.now());
        let record = LogRecord::raw(type_name, tag, message, now);
        if !self.passes_level(&record) {
            return;
        }
        if self.paused {
            self.enqueue(record);
            return;
        }
        self.process_record(record);
    }

    // Per-type raw logging methods for common types
    pub fn info_raw(&mut self, message: &str) {
        self.log_raw("info", None, message);
    }

    pub fn warn_raw(&mut self, message: &str) {
        self.log_raw("warn", None, message);
    }

    pub fn error_raw(&mut self, message: &str) {
        self.log_raw("error", None, message);
    }

    pub fn debug_raw(&mut self, message: &str) {
        self.log_raw("debug", None, message);
    }

    pub fn trace_raw(&mut self, message: &str) {
        self.log_raw("trace", None, message);
    }

    pub fn success_raw(&mut self, message: &str) {
        self.log_raw("success", None, message);
    }

    pub fn fail_raw(&mut self, message: &str) {
        self.log_raw("fail", None, message);
    }

    pub fn fatal_raw(&mut self, message: &str) {
        self.log_raw("fatal", None, message);
    }

    // Generic raw method with custom type
    pub fn log_type_raw(&mut self, type_name: &str, message: &str) {
        self.log_raw(type_name, None, message);
    }

    // Convenience methods for formatted logging
    pub fn info<T: ToString>(&mut self, message: T) {
        self.log("info", None, [message.to_string()]);
    }

    pub fn warn<T: ToString>(&mut self, message: T) {
        self.log("warn", None, [message.to_string()]);
    }

    pub fn error<T: ToString>(&mut self, message: T) {
        self.log("error", None, [message.to_string()]);
    }

    pub fn success<T: ToString>(&mut self, message: T) {
        self.log("success", None, [message.to_string()]);
    }

    pub fn debug<T: ToString>(&mut self, message: T) {
        self.log("debug", None, [message.to_string()]);
    }

    pub fn trace<T: ToString>(&mut self, message: T) {
        self.log("trace", None, [message.to_string()]);
    }

    /// Set a mock callback function that will be called before the reporter emits each record.
    /// This is useful for testing and debugging. The mock function receives a reference to the
    /// LogRecord before it is emitted to the reporter.
    pub fn set_mock<F>(&mut self, mock_fn: F)
    where
        F: Fn(&LogRecord) + Send + Sync + 'static,
    {
        self.mock_fn = Some(Box::new(mock_fn));
    }

    /// Clear the mock callback function.
    pub fn clear_mock(&mut self) {
        self.mock_fn = None;
    }

    fn passes_level(&self, record: &LogRecord) -> bool {
        record.level <= self.cfg.level
    }

    fn enqueue(&mut self, record: LogRecord) {
        if let Some(cap) = self.cfg.queue_capacity {
            if self.queue.len() >= cap {
                self.queue.pop_front();
            }
        }
        self.queue.push_back(Pending(record));
    }

    fn process_record(&mut self, record: LogRecord) {
        let mut to_emit: Vec<LogRecord> = Vec::new();
        self.throttler
            .on_record(record, |r| to_emit.push(r.clone()));
        for rec in to_emit {
            self.emit(&rec);
        }
    }

    fn emit(&self, record: &LogRecord) {
        // Call mock function if set (before reporter emission)
        if let Some(ref mock) = self.mock_fn {
            mock(record);
        }

        let is_err = record.level <= LogLevel::ERROR;
        let mut handle: Box<dyn Write> = if is_err {
            Box::new(io::stderr())
        } else {
            Box::new(io::stdout())
        };
        let _ = self.reporter.emit(record, &mut *handle);
    }

    pub fn flush(&mut self) {
        let mut to_emit: Vec<LogRecord> = Vec::new();
        self.throttler.flush(|r| to_emit.push(r.clone()));
        for rec in to_emit {
            self.emit(&rec);
        }
    }

    pub fn pause(&mut self) {
        if !self.paused {
            self.flush(); // flush throttled group on pause
            self.paused = true;
        }
    }

    pub fn resume(&mut self) {
        if !self.paused {
            return;
        }
        self.paused = false;
        self.flush(); // flush any stale before draining queued
        while let Some(Pending(rec)) = self.queue.pop_front() {
            if !self.passes_level(&rec) {
                continue;
            }
            self.process_record(rec);
        }
    }
}

impl<R: Reporter + 'static> Drop for Logger<R> {
    fn drop(&mut self) {
        self.flush();
    }
}

pub type BasicLogger = Logger<BasicReporter>;
pub type FancyLogger = Logger<FancyReporter>;

impl Default for BasicLogger {
    fn default() -> Self {
        Logger::new(BasicReporter::default())
    }
}

impl BasicReporter {
    pub fn adaptive() -> Self {
        Self {
            opts: FormatOptions::adaptive(),
        }
    }
}

impl FancyReporter {
    pub fn adaptive() -> Self {
        Self {
            opts: FormatOptions::adaptive(),
        }
    }
}

impl Default for FancyReporter {
    fn default() -> Self {
        Self::adaptive()
    }
}

impl ReporterWithOptions for BasicReporter {
    fn fmt_options(&self) -> &FormatOptions {
        &self.opts
    }
    fn fmt_options_mut(&mut self) -> &mut FormatOptions {
        &mut self.opts
    }
}

impl ReporterWithOptions for FancyReporter {
    fn fmt_options(&self) -> &FormatOptions {
        &self.opts
    }
    fn fmt_options_mut(&mut self) -> &mut FormatOptions {
        &mut self.opts
    }
}

#[cfg(feature = "json")]
#[derive(Default)]
pub struct JsonReporter {
    opts: FormatOptions,
}

#[cfg(feature = "json")]
impl JsonReporter {
    pub fn new() -> Self {
        Self {
            opts: FormatOptions::default(),
        }
    }

    pub fn adaptive() -> Self {
        Self {
            opts: FormatOptions::adaptive(),
        }
    }
}

#[cfg(feature = "json")]
impl Reporter for JsonReporter {
    fn emit(&self, record: &LogRecord, w: &mut dyn Write) -> io::Result<()> {
        use serde_json::{Map, Value, json};

        // Build JSON object with deterministic key order
        let mut obj = Map::new();

        // Schema identifier first
        obj.insert("schema".to_string(), json!("consola-rs/v1"));

        // Time (if enabled)
        if self.opts.date {
            let ts = jiff::Zoned::now().to_string();
            obj.insert("time".to_string(), json!(ts));
        }

        // Core fields
        obj.insert("level".to_string(), json!(record.level.0));
        obj.insert("level_name".to_string(), json!(record.type_name));
        obj.insert("type".to_string(), json!(record.type_name));

        if let Some(tag) = &record.tag {
            obj.insert("tag".to_string(), json!(tag));
        }

        if let Some(msg) = &record.message {
            obj.insert("message".to_string(), json!(msg));
        }

        // Arguments
        if !record.args.is_empty() {
            let args_json: Vec<Value> = record
                .args
                .iter()
                .map(|arg| match arg {
                    ArgValue::String(s) => json!(s),
                    ArgValue::Number(n) => json!(n),
                    ArgValue::Bool(b) => json!(b),
                    ArgValue::Error(e) => json!(e),
                    ArgValue::OtherDebug(d) => json!(d),
                    #[cfg(feature = "json")]
                    ArgValue::Json(v) => v.clone(),
                })
                .collect();
            obj.insert("args".to_string(), json!(args_json));
        }

        // Additional structured args
        if let Some(additional) = &record.additional {
            let add_json: Vec<Value> = additional
                .iter()
                .map(|arg| match arg {
                    ArgValue::String(s) => json!(s),
                    ArgValue::Number(n) => json!(n),
                    ArgValue::Bool(b) => json!(b),
                    ArgValue::Error(e) => json!(e),
                    ArgValue::OtherDebug(d) => json!(d),
                    #[cfg(feature = "json")]
                    ArgValue::Json(v) => v.clone(),
                })
                .collect();
            obj.insert("additional".to_string(), json!(add_json));
        }

        // Repetition count
        if record.repetition_count > 1 {
            obj.insert("repeat".to_string(), json!(record.repetition_count));
        }

        // Stack trace
        if let Some(stack) = &record.stack {
            obj.insert("stack".to_string(), json!(stack));
        }

        // Error chain (structured array)
        if let Some(chain) = &record.error_chain {
            obj.insert("causes".to_string(), json!(chain));
        }

        // Metadata
        if let Some(meta) = &record.meta {
            let meta_obj: Map<String, Value> = meta
                .iter()
                .map(|(k, v)| {
                    let val = match v {
                        ArgValue::String(s) => json!(s),
                        ArgValue::Number(n) => json!(n),
                        ArgValue::Bool(b) => json!(b),
                        ArgValue::Error(e) => json!(e),
                        ArgValue::OtherDebug(d) => json!(d),
                        #[cfg(feature = "json")]
                        ArgValue::Json(jv) => jv.clone(),
                    };
                    (k.clone(), val)
                })
                .collect();
            obj.insert("meta".to_string(), json!(meta_obj));
        }

        // Serialize to single line (compact)
        let json_str = serde_json::to_string(&Value::Object(obj)).map_err(io::Error::other)?;

        writeln!(w, "{}", json_str)
    }
}

#[cfg(feature = "json")]
impl ReporterWithOptions for JsonReporter {
    fn fmt_options(&self) -> &FormatOptions {
        &self.opts
    }

    fn fmt_options_mut(&mut self) -> &mut FormatOptions {
        &mut self.opts
    }
}

#[cfg(feature = "json")]
pub type JsonLogger = Logger<JsonReporter>;

/// MemoryReporter captures full LogRecords in memory for testing and inspection.
/// This is useful for writing tests that need to verify log output without checking stdout/stderr.
#[derive(Default)]
pub struct MemoryReporter {
    records: std::sync::Arc<std::sync::Mutex<Vec<LogRecord>>>,
    opts: FormatOptions,
}

impl MemoryReporter {
    pub fn new() -> Self {
        Self {
            records: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
            opts: FormatOptions::default(),
        }
    }

    /// Get a clone of all captured records.
    pub fn get_records(&self) -> Vec<LogRecord> {
        self.records.lock().unwrap().clone()
    }

    /// Clear all captured records.
    pub fn clear(&self) {
        self.records.lock().unwrap().clear();
    }

    /// Get the number of captured records.
    pub fn len(&self) -> usize {
        self.records.lock().unwrap().len()
    }

    /// Check if any records have been captured.
    pub fn is_empty(&self) -> bool {
        self.records.lock().unwrap().is_empty()
    }

    /// Get a shared reference to the records Arc for sharing between threads.
    pub fn records_arc(&self) -> std::sync::Arc<std::sync::Mutex<Vec<LogRecord>>> {
        Arc::clone(&self.records)
    }
}

impl Reporter for MemoryReporter {
    fn emit(&self, record: &LogRecord, _w: &mut dyn Write) -> io::Result<()> {
        self.records.lock().unwrap().push(record.clone());
        Ok(())
    }
}

impl ReporterWithOptions for MemoryReporter {
    fn fmt_options(&self) -> &FormatOptions {
        &self.opts
    }

    fn fmt_options_mut(&mut self) -> &mut FormatOptions {
        &mut self.opts
    }
}

pub type MemoryLogger = Logger<MemoryReporter>;

/// Builder for configuring Logger instances with environment variable support
pub struct LoggerBuilder<R: Reporter + 'static> {
    reporter: Option<R>,
    config: LoggerConfig,
    defaults: RecordDefaults,
}

impl<R: Reporter + 'static> LoggerBuilder<R> {
    pub fn new() -> Self
    where
        R: Default,
    {
        Self {
            reporter: None,
            config: LoggerConfig::default(),
            defaults: RecordDefaults::default(),
        }
    }

    pub fn with_reporter(mut self, reporter: R) -> Self {
        self.reporter = Some(reporter);
        self
    }

    pub fn with_level(mut self, level: LogLevel) -> Self {
        self.config.level = level;
        self
    }

    pub fn with_throttle_config(mut self, throttle: ThrottleConfig) -> Self {
        self.config.throttle = throttle;
        self
    }

    pub fn with_defaults(mut self, defaults: RecordDefaults) -> Self {
        self.defaults = defaults;
        self
    }

    /// Apply environment variables: CONSOLA_LEVEL, NO_COLOR, CONSOLA_COMPACT
    /// Precedence: builder > env > defaults
    pub fn from_env(mut self) -> Self {
        use std::env;

        // CONSOLA_LEVEL overrides level if not explicitly set by builder
        if let Ok(level_str) = env::var("CONSOLA_LEVEL") {
            if let Ok(level_num) = level_str.parse::<i16>() {
                self.config.level = LogLevel(level_num);
            } else {
                // Try to parse as type name
                if let Some(level) = crate::levels::level_for_type(&level_str) {
                    self.config.level = level;
                }
            }
        }

        self
    }

    pub fn build(self) -> Logger<R>
    where
        R: Default,
    {
        let reporter = self.reporter.unwrap_or_default();
        Logger::new(reporter).with_config(self.config)
        // Note: defaults would need to be stored in Logger to be used during log calls
    }
}

impl Default for LoggerBuilder<BasicReporter> {
    fn default() -> Self {
        Self::new()
    }
}