fix(app): log hub masks dotted tokens, keeps worker errors and drops the oldest lines by time
- redact: a token with sentence dots around it is masked too. - Worker error messages are always logged (code and id), so the log screen and the error report show them without debug logging. - The voice worker logs with stt, llm and tts categories, so its lines land in those tabs. - The zip leaves out the oldest lines by the time they were logged, across every tab's file, not by file name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -81,9 +81,12 @@ STT, LLM and TTS lines go to `stt`, `llm`, `tts` by their worker category), `bro
|
||||
order), the app version and a short PC summary (OS, CPU count, memory); `None` without an error.
|
||||
When it is over 2,000 characters (Discord's message limit) the screen offers a download instead.
|
||||
- `export_zip(from, to, out)`: the log files of those days in a zip, at most 10 MB (Discord's file
|
||||
limit): when larger, the oldest lines are left out and a note says so.
|
||||
limit): when larger, the oldest lines by the time they were logged, across every tab's file, are
|
||||
left out and a note says how many.
|
||||
- Every copy and export runs through `redact`: each stored secret's value, and anything shaped like
|
||||
a Discord bot token, becomes `[비밀값]`.
|
||||
a Discord bot token (also with sentence dots around it), becomes `[비밀값]`.
|
||||
- Errors the voice worker reports to the app (`error` messages: code and request id) are always
|
||||
logged, debug logging or not, so the log screen and the error report show them.
|
||||
|
||||
## Tests
|
||||
|
||||
|
||||
@@ -213,19 +213,25 @@ impl LogHub {
|
||||
}
|
||||
}
|
||||
|
||||
/// The zip with the oldest `dropped` lines (oldest files first) left out.
|
||||
/// The zip with the `dropped` oldest lines left out: oldest by the time each line was logged, across
|
||||
/// every tab's file (a line's time is its first 23 characters, `YYYY-MM-DD HH:MM:SS.mmm`; lines
|
||||
/// without one count as oldest).
|
||||
fn build_zip(files: &[(NaiveDate, String, Vec<String>)], dropped: usize) -> std::io::Result<Vec<u8>> {
|
||||
let mut order: Vec<(&str, usize, usize)> = files.iter().enumerate()
|
||||
.flat_map(|(fi, (_, _, lines))| lines.iter().enumerate()
|
||||
.map(move |(li, l)| (l.get(..23).filter(|t| line_time_ok(t)).unwrap_or(""), fi, li)))
|
||||
.collect();
|
||||
order.sort();
|
||||
let gone: std::collections::HashSet<(usize, usize)> = order.iter().take(dropped).map(|&(_, f, l)| (f, l)).collect();
|
||||
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
let mut skip = dropped;
|
||||
for (_, name, lines) in files {
|
||||
let cut = skip.min(lines.len());
|
||||
skip -= cut;
|
||||
if cut == lines.len() {
|
||||
for (fi, (_, name, lines)) in files.iter().enumerate() {
|
||||
let kept: Vec<&String> = lines.iter().enumerate().filter(|(li, _)| !gone.contains(&(fi, *li))).map(|(_, l)| l).collect();
|
||||
if kept.is_empty() {
|
||||
continue;
|
||||
}
|
||||
zip.start_file(name.as_str(), opts).map_err(std::io::Error::other)?;
|
||||
for l in &lines[cut..] {
|
||||
for l in kept {
|
||||
zip.write_all(l.as_bytes())?;
|
||||
zip.write_all(b"\n")?;
|
||||
}
|
||||
@@ -237,6 +243,10 @@ fn build_zip(files: &[(NaiveDate, String, Vec<String>)], dropped: usize) -> std:
|
||||
Ok(zip.finish().map_err(std::io::Error::other)?.into_inner())
|
||||
}
|
||||
|
||||
fn line_time_ok(t: &str) -> bool {
|
||||
chrono::NaiveDateTime::parse_from_str(t, "%Y-%m-%d %H:%M:%S%.3f").is_ok()
|
||||
}
|
||||
|
||||
fn file_day(name: &str) -> Option<NaiveDate> {
|
||||
let stem = name.strip_suffix(".log")?;
|
||||
let date = stem.get(stem.len().checked_sub(10)?..)?;
|
||||
@@ -269,8 +279,18 @@ pub fn redact(text: &str, secrets: &[String]) -> String {
|
||||
}
|
||||
let mut result = String::with_capacity(out.len());
|
||||
let mut word = String::new();
|
||||
// A word may carry sentence dots around it ("token=<token>."): they are not part of the token.
|
||||
let flush = |word: &mut String, result: &mut String| {
|
||||
if looks_like_token(word) { result.push_str(SECRET_MARK) } else { result.push_str(word) }
|
||||
let core = word.trim_matches('.');
|
||||
if looks_like_token(core) {
|
||||
let lead = word.len() - word.trim_start_matches('.').len();
|
||||
let trail = word.len() - word.trim_end_matches('.').len();
|
||||
result.push_str(&word[..lead]);
|
||||
result.push_str(SECRET_MARK);
|
||||
result.push_str(&word[word.len() - trail..]);
|
||||
} else {
|
||||
result.push_str(word)
|
||||
}
|
||||
word.clear();
|
||||
};
|
||||
for c in out.chars() {
|
||||
@@ -402,6 +422,41 @@ mod tests {
|
||||
let token = format!("{}.{}.{}", "M".repeat(24), "GabcdE", "x".repeat(30));
|
||||
let out = redact(&format!("token={token}, ok id=123456789012345678 a.b.c"), &[]);
|
||||
assert_eq!(out, "token=[비밀값], ok id=123456789012345678 a.b.c");
|
||||
// Sentence dots around it do not hide it.
|
||||
assert_eq!(redact(&format!("token={token}. next ...{token}..."), &[]), "token=[비밀값]. next ...[비밀값]...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_zip_leaves_out_the_oldest_lines_by_time_across_tabs() {
|
||||
let at_hm = |d: u32, h: u32, m: u32| Local.with_ymd_and_hms(2026, 5, d, h, m, 0).unwrap();
|
||||
let dir = temp_dir("logorder");
|
||||
let hub = LogHub::new(Some(&dir));
|
||||
hub.push_at(at_hm(1, 0, 1), Level::Info, "chat", "first chat line");
|
||||
hub.push_at(at_hm(1, 23, 59), Level::Info, "app", "late on the first");
|
||||
hub.push_at(at_hm(2, 0, 1), Level::Info, "chat", "early on the second");
|
||||
let mut files: Vec<(NaiveDate, String, Vec<String>)> = vec![];
|
||||
for e in std::fs::read_dir(&dir).unwrap().flatten() {
|
||||
let name = e.file_name().to_string_lossy().into_owned();
|
||||
files.push((file_day(&name).unwrap(), name, std::fs::read_to_string(e.path()).unwrap().lines().map(str::to_owned).collect()));
|
||||
}
|
||||
files.sort();
|
||||
// Leave out one line: the 00:01 chat line of the 1st, not a line of the app file.
|
||||
let z = build_zip(&files, 1).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(z)).unwrap();
|
||||
let mut all = String::new();
|
||||
for i in 0..z.len() {
|
||||
std::io::Read::read_to_string(&mut z.by_index(i).unwrap(), &mut all).unwrap();
|
||||
}
|
||||
assert!(!all.contains("first chat line"));
|
||||
assert!(all.contains("late on the first") && all.contains("early on the second"));
|
||||
// Two: then the 23:59 app line goes and the next day's chat line stays.
|
||||
let z = build_zip(&files, 2).unwrap();
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(z)).unwrap();
|
||||
let mut all = String::new();
|
||||
for i in 0..z.len() {
|
||||
std::io::Read::read_to_string(&mut z.by_index(i).unwrap(), &mut all).unwrap();
|
||||
}
|
||||
assert!(!all.contains("late on the first") && all.contains("early on the second"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -546,7 +546,8 @@ fn on_text(inner: &Inner, t: &str) {
|
||||
inner.state.lock().unwrap().open_tts.remove(&d.id).then_some(WorkerEvent::TtsDone(d))
|
||||
}),
|
||||
"error" => serde_json::from_value::<WorkerError>(v).ok().map(|e| {
|
||||
debug_log!("worker", "voice worker error {}", e.code);
|
||||
// Always logged: the log screen and its error report must show what the worker refused.
|
||||
error_log!("worker", "voice worker error {} id={}", e.code, e.id.as_deref().unwrap_or("-"));
|
||||
// A request the worker refused outright never gets a tts_done.
|
||||
if e.code == "tts_unavailable" || e.code == "brain_unavailable" {
|
||||
if let Some(id) = &e.id {
|
||||
|
||||
Reference in New Issue
Block a user