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
//! Plan 05-05 OPS-08 — structured logging defaults to JSON in production,
//! pretty in dev (CONTEXT.md D-26 verbatim).
//!
//! ## Mode selection (D-26)
//!
//! - `LUNARIS_ENV=production` OR `!std::io::stdout().is_terminal()` → JSON
//! formatter via `tracing_subscriber::fmt().json().with_current_span(true)
//! .with_span_list(true)`.
//! - Otherwise → pretty formatter via `tracing_subscriber::fmt().pretty()`.
//!
//! ## Idempotency
//!
//! Uses `try_init().ok()` so test code with its own subscriber doesn't panic
//! on duplicate-init. Calling [`init`] multiple times is safe — subsequent
//! calls become no-ops once a global subscriber is installed.
//!
//! ## Public callers
//!
//! - `lunaris-server::main` (REPLACES the Plan 05-01 minimal `tracing_subscriber::fmt()` init).
//! - Embedded callers may invoke `lunaris::init_logging()` (re-export) at process
//! start to opt in to the same JSON / pretty selector.
use IsTerminal;
use EnvFilter;
use fmt;
/// Env var that flips the formatter to JSON unconditionally per CONTEXT.md D-26.
const LUNARIS_ENV: &str = "LUNARIS_ENV";
/// Value treated as "production" for the [`LUNARIS_ENV`] check.
const PRODUCTION: &str = "production";
/// Initialize the global tracing subscriber.
///
/// Picks JSON vs pretty per CONTEXT.md D-26:
/// - `LUNARIS_ENV=production` → JSON.
/// - stdout is NOT a TTY (e.g., piped, systemd journal, container log) → JSON.
/// - else → pretty.
///
/// Honors `RUST_LOG` (or any `EnvFilter::try_from_default_env`-recognized var)
/// for level filtering; defaults to `info` when unset.
///
/// Idempotent — `try_init` swallows the duplicate-init error so tests / other
/// callers that already installed a subscriber are not disrupted.