cortexkit-log 0.3.7

Fleet logging format, filtering, redaction, and retention for CortexKit modules
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
use std::fmt;
use std::time::SystemTime;

use chrono::{DateTime, SecondsFormat, Utc};
use tracing::Level;

/// Renders one r2 fleet log line: `<ts> <LEVEL> <logger>: [<bound>] <message> <fields>`.
///
/// `logger` is a full dotted name such as `module.component.action`. Bound fields render
/// in order inside a bracket; an empty `bound` slice renders no bracket.
pub(crate) fn render_line(
    at: SystemTime,
    level: &Level,
    logger: &str,
    bound: &[(String, String)],
    message: &str,
    fields: &[(String, String)],
) -> String {
    let timestamp = DateTime::<Utc>::from(at).to_rfc3339_opts(SecondsFormat::Millis, true);
    let mut line = format!("{timestamp} {:<5} {logger}:", level.as_str());

    if !bound.is_empty() {
        line.push_str(" [");
        for (index, (key, value)) in bound.iter().enumerate() {
            if index > 0 {
                line.push(' ');
            }
            line.push_str(key);
            line.push('=');
            line.push_str(&format_value(value));
        }
        line.push(']');
    }
    if !message.is_empty() {
        line.push(' ');
        line.push_str(&escape_message(message));
    }
    for (key, value) in fields {
        line.push(' ');
        line.push_str(key);
        line.push('=');
        line.push_str(&format_value(value));
    }

    line
}

#[derive(Clone, Copy)]
pub(crate) enum LoggerPath<'a> {
    Bare,
    Component(&'a str),
    Dependency(&'a str),
}

impl<'a> LoggerPath<'a> {
    pub(crate) fn from_target(module_id: &str, target: &'a str) -> Self {
        if target.is_empty() || target == module_id || target.contains("::") {
            return Self::Bare;
        }
        if target.split('.').all(is_segment) {
            Self::Component(target)
        } else {
            Self::Bare
        }
    }

    pub(crate) fn dependency(crate_name: &'a str) -> Self {
        Self::Dependency(crate_name)
    }

    pub(crate) fn dependency_name(self) -> Option<&'a str> {
        match self {
            Self::Dependency(crate_name) => Some(crate_name),
            Self::Bare | Self::Component(_) => None,
        }
    }

    pub(crate) fn logger_name(self, module_id: &str) -> String {
        match self {
            Self::Bare => module_id.to_owned(),
            Self::Component(target) => format!("{module_id}.{target}"),
            Self::Dependency(crate_name) if valid_crate_segment(crate_name) => {
                let mut logger = String::with_capacity(module_id.len() + crate_name.len() + 5);
                logger.push_str(module_id);
                logger.push_str(".dep.");
                for character in crate_name.chars() {
                    logger.push(if character == '_' { '-' } else { character });
                }
                logger
            }
            Self::Dependency(_) => module_id.to_owned(),
        }
    }

    pub(crate) fn matches_prefix(self, module_id: &str, prefix: &str) -> bool {
        let mut directive = prefix.split('.');
        for segment in module_id.split('.') {
            if let Some(matches) = match_segment(&mut directive, segment) {
                return matches;
            }
        }

        match self {
            Self::Bare => directive.next().is_none(),
            Self::Component(target) => {
                for segment in target.split('.') {
                    if let Some(matches) = match_segment(&mut directive, segment) {
                        return matches;
                    }
                }
                directive.next().is_none()
            }
            Self::Dependency(crate_name) if valid_crate_segment(crate_name) => {
                if let Some(matches) = match_segment(&mut directive, "dep") {
                    return matches;
                }
                let Some(crate_segment) = directive.next() else {
                    return true;
                };
                crate_segment_matches(crate_segment, crate_name) && directive.next().is_none()
            }
            Self::Dependency(_) => directive.next().is_none(),
        }
    }
}

fn match_segment<'a>(directive: &mut std::str::Split<'a, char>, segment: &str) -> Option<bool> {
    match directive.next() {
        None => Some(true),
        Some(expected) if expected == segment => None,
        Some(_) => Some(false),
    }
}

fn crate_segment_matches(expected: &str, crate_name: &str) -> bool {
    let mut actual = crate_name.chars();
    expected.chars().all(|expected| {
        actual.next().is_some_and(|actual| {
            if actual == '_' {
                expected == '-'
            } else {
                actual == expected
            }
        })
    }) && actual.next().is_none()
}

fn valid_crate_segment(crate_name: &str) -> bool {
    let mut characters = crate_name.chars();
    matches!(characters.next(), Some('a'..='z'))
        && characters.all(|character| matches!(character, 'a'..='z' | '0'..='9' | '_'))
}

pub(crate) fn is_segment(segment: &str) -> bool {
    let mut chars = segment.chars();
    matches!(chars.next(), Some('a'..='z'))
        && chars.all(|character| matches!(character, 'a'..='z' | '0'..='9' | '-'))
}

// Control characters are handled per field, never across the rendered line.
// A complete terminal escape sequence inside one message or value is removed;
// every control character left is written as `\uXXXX`. Stripping escape
// sequences over the whole rendered line, as this crate did before 0.3.3, let
// a stray `ESC ]` in one value open an OSC that consumed the value's closing
// quote and every field after it. The TypeScript twin (`@cortexkit/log`)
// follows the same rule, and the golden fixture shared by both pins it.

/// Whether `character` is written as a `\uXXXX` escape: C0 controls other than
/// `\n` and `\r` (which keep their own escapes), DEL, and the C1 range.
fn is_escaped_control(character: char) -> bool {
    matches!(character as u32, 0x00..=0x1f | 0x7f..=0x9f) && !matches!(character, '\n' | '\r')
}

fn push_control_escape(output: &mut String, character: char) {
    output.push_str(&format!("\\u{:04x}", character as u32));
}

/// Removes CSI and OSC sequences that start and end inside `field`, in their
/// 7-bit (`ESC [`, `ESC ]`) and C1 (`U+009B`, `U+009D`) forms. A sequence that
/// does not end inside the field is not a sequence and is left for escaping.
fn strip_complete_sequences(field: &str) -> std::borrow::Cow<'_, str> {
    if !field
        .chars()
        .any(|character| matches!(character, '\u{1b}' | '\u{9b}' | '\u{9d}'))
    {
        return std::borrow::Cow::Borrowed(field);
    }
    let characters: Vec<char> = field.chars().collect();
    let mut output = String::with_capacity(field.len());
    let mut index = 0;
    while index < characters.len() {
        let character = characters[index];
        let next = characters.get(index + 1).copied();
        let csi_body = match (character, next) {
            ('\u{1b}', Some('[')) => Some(index + 2),
            ('\u{9b}', _) => Some(index + 1),
            _ => None,
        };
        let osc_body = match (character, next) {
            ('\u{1b}', Some(']')) => Some(index + 2),
            ('\u{9d}', _) => Some(index + 1),
            _ => None,
        };
        let end = if let Some(body) = csi_body {
            csi_end(&characters, body)
        } else if let Some(body) = osc_body {
            osc_end(&characters, body)
        } else {
            None
        };
        match end {
            Some(end) => index = end,
            None => {
                output.push(character);
                index += 1;
            }
        }
    }
    std::borrow::Cow::Owned(output)
}

/// A CSI body is parameter and intermediate bytes (`0x20`-`0x3f`) ended by a
/// final byte (`0x40`-`0x7e`). Anything else before the final byte means it
/// was never a sequence.
fn csi_end(characters: &[char], body: usize) -> Option<usize> {
    for (offset, character) in characters[body..].iter().enumerate() {
        match *character as u32 {
            0x40..=0x7e => return Some(body + offset + 1),
            0x20..=0x3f => {}
            _ => return None,
        }
    }
    None
}

/// An OSC ends at `BEL`, `ESC \` or C1 `ST` (`U+009C`).
fn osc_end(characters: &[char], body: usize) -> Option<usize> {
    let mut index = body;
    while index < characters.len() {
        match characters[index] {
            '\u{7}' | '\u{9c}' => return Some(index + 1),
            '\u{1b}' if characters.get(index + 1) == Some(&'\\') => return Some(index + 2),
            _ => index += 1,
        }
    }
    None
}

// Backslash is escaped first so a literal `\n` or `\u0041` in the input
// survives as `\\n` or `\\u0041` and cannot be read back as an escape.
fn escape_message(message: &str) -> String {
    let cleaned = strip_complete_sequences(message);
    let mut output = String::with_capacity(cleaned.len());
    for character in cleaned.chars() {
        match character {
            '\\' => output.push_str("\\\\"),
            '\r' => output.push_str("\\r"),
            '\n' => output.push_str("\\n"),
            control if is_escaped_control(control) => push_control_escape(&mut output, control),
            other => output.push(other),
        }
    }
    output
}

fn format_value(value: &str) -> String {
    // Stripped before the quoting decision, so a colored plain word stays unquoted.
    let cleaned = strip_complete_sequences(value);
    let needs_quotes = cleaned.is_empty()
        || cleaned.chars().any(|character| {
            matches!(character, ' ' | '"' | '\n' | '\r' | ']') || is_escaped_control(character)
        });
    if !needs_quotes {
        // An unquoted value is verbatim, backslashes included: only the quoted
        // form has an escape grammar, and a reader decodes only quoted values.
        return cleaned.into_owned();
    }
    let mut output = String::with_capacity(cleaned.len() + 2);
    output.push('"');
    for character in cleaned.chars() {
        match character {
            '\\' => output.push_str("\\\\"),
            '"' => output.push_str("\\\""),
            '\r' => output.push_str("\\r"),
            '\n' => output.push_str("\\n"),
            control if is_escaped_control(control) => push_control_escape(&mut output, control),
            other => output.push(other),
        }
    }
    output.push('"');
    output
}

/// The guard after a module redactor: writes any raw control character the
/// redactor put into an already-rendered line as an escape, and never strips.
/// Backslashes are left alone, since the line's own escapes are already there.
pub(crate) fn escape_raw_controls(line: &str) -> std::borrow::Cow<'_, str> {
    if !line
        .chars()
        .any(|character| matches!(character, '\n' | '\r') || is_escaped_control(character))
    {
        return std::borrow::Cow::Borrowed(line);
    }
    let mut output = String::with_capacity(line.len());
    for character in line.chars() {
        match character {
            '\r' => output.push_str("\\r"),
            '\n' => output.push_str("\\n"),
            control if is_escaped_control(control) => push_control_escape(&mut output, control),
            other => output.push(other),
        }
    }
    std::borrow::Cow::Owned(output)
}

/// A level parsed from a fleet log line.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParsedLevel {
    /// A trace event.
    Trace,
    /// A debug event.
    Debug,
    /// An informational event.
    Info,
    /// A warning event.
    Warn,
    /// An error event.
    Error,
}

/// The fields needed to merge and filter a fleet log line.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParsedLine<'a> {
    /// Event time decoded from the UTC timestamp.
    pub timestamp: SystemTime,
    /// Event severity.
    pub level: ParsedLevel,
    /// The full dotted logger name, rooted at the module id.
    pub logger: &'a str,
    /// The module id: the logger's first segment.
    pub module_id: &'a str,
    /// The raw text inside the bound bracket, absent when there was none.
    pub bound: Option<&'a str>,
    /// The message and ordered event fields after the bracket.
    pub body: &'a str,
}

impl ParsedLine<'_> {
    /// The `session=` bound value when one is present, whole, issuer included.
    pub fn session(&self) -> Option<&str> {
        self.bound.and_then(|bound| bound_value(bound, "session"))
    }
}

fn bound_value<'a>(bound: &'a str, key: &str) -> Option<&'a str> {
    bound
        .split(' ')
        .filter_map(|pair| pair.split_once('='))
        .find(|(candidate, _)| *candidate == key)
        .map(|(_, value)| value)
}

/// A stable parse failure returned by [`parse_line`](crate::parse_line).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseError {
    reason: &'static str,
}

impl ParseError {
    pub(crate) const fn new(reason: &'static str) -> Self {
        Self { reason }
    }

    /// Returns the stable reason used by conformance fixtures and CLI diagnostics.
    pub const fn reason(self) -> &'static str {
        self.reason
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.reason)
    }
}

impl std::error::Error for ParseError {}

pub(crate) fn parse(line: &str) -> Result<ParsedLine<'_>, ParseError> {
    if line.contains('\u{1b}') || line.contains('\u{9b}') {
        return Err(ParseError::new("ansi_forbidden"));
    }
    if line.contains(['\n', '\r']) {
        return Err(ParseError::new("line_break"));
    }

    let (timestamp_text, after_timestamp) = line
        .split_once(' ')
        .ok_or_else(|| ParseError::new("timestamp_missing"))?;
    if !timestamp_text.ends_with('Z') {
        return Err(ParseError::new("timestamp_not_utc_z"));
    }
    if timestamp_text.len() != 24 {
        return Err(ParseError::new("timestamp_precision"));
    }
    let timestamp = DateTime::parse_from_rfc3339(timestamp_text)
        .map_err(|_| ParseError::new("timestamp_invalid"))?;

    let (level, after_level) = parse_level(after_timestamp)?;

    // The logger runs to the first space and MUST end in a colon. An r1 line
    // (`fusiform poll changed`) has no colon and fails here by design: it is
    // the colon that makes the logger unambiguous against a one-word message.
    let (logger_token, mut body) = match after_level.split_once(' ') {
        Some(split) => split,
        None => (after_level, ""),
    };
    let logger = logger_token
        .strip_suffix(':')
        .ok_or_else(|| ParseError::new("logger_not_terminated"))?;
    if logger.is_empty() {
        return Err(ParseError::new("logger_missing"));
    }
    if !logger.split('.').all(is_segment) {
        return Err(ParseError::new("logger_segment_grammar"));
    }
    let module_id = logger.split('.').next().unwrap_or(logger);

    let mut bound = None;
    if let Some(rest) = body.strip_prefix('[') {
        let close =
            find_bracket_close(rest).ok_or_else(|| ParseError::new("bound_unterminated"))?;
        let inside = &rest[..close];
        if inside.is_empty() {
            return Err(ParseError::new("empty_bound_bracket"));
        }
        if let Some(session) = bound_value(inside, "session") {
            let valid = session
                .rsplit_once(':')
                .is_some_and(|(issuer, id)| !issuer.is_empty() && !id.is_empty());
            // `session=global` and other issuer-less placeholders fail here on
            // their missing `issuer:` half; the renderer never has to know any
            // particular sentinel.
            if !valid {
                return Err(ParseError::new("session_missing_issuer"));
            }
        }
        bound = Some(inside);
        body = rest[close + 1..]
            .strip_prefix(' ')
            .unwrap_or(&rest[close + 1..]);
    }

    // A bracket AFTER the message is not context: context precedes the message
    // so its column is stable. Reject rather than silently reading it as a
    // field, because a reader that accepted both would train writers to put
    // it wherever, and the alignment property would be gone in a month.
    if bound.is_none() && body.contains(" [") && body.ends_with(']') {
        return Err(ParseError::new("bound_after_message"));
    }

    Ok(ParsedLine {
        timestamp: SystemTime::from(timestamp),
        level,
        logger,
        module_id,
        bound,
        body,
    })
}

// The bracket closes at the first `]` that is not inside a quoted value. A
// bound value containing `]` was quoted by the renderer for exactly this
// reason, so a naive `find(']')` would split a path like `[root="a]b"]`.
fn find_bracket_close(input: &str) -> Option<usize> {
    let mut in_quotes = false;
    let mut escaped = false;
    for (index, character) in input.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        match character {
            '\\' if in_quotes => escaped = true,
            '"' => in_quotes = !in_quotes,
            ']' if !in_quotes => return Some(index),
            _ => {}
        }
    }
    None
}

fn parse_level(input: &str) -> Result<(ParsedLevel, &str), ParseError> {
    for (prefix, level) in [
        ("TRACE ", ParsedLevel::Trace),
        ("DEBUG ", ParsedLevel::Debug),
        ("INFO  ", ParsedLevel::Info),
        ("WARN  ", ParsedLevel::Warn),
        ("ERROR ", ParsedLevel::Error),
    ] {
        if let Some(rest) = input.strip_prefix(prefix) {
            return Ok((level, rest));
        }
    }

    if ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]
        .iter()
        .any(|level| input.starts_with(level))
    {
        Err(ParseError::new("level_column_width"))
    } else {
        Err(ParseError::new("level_invalid"))
    }
}