Files
Library/tests/standalone/Log/Fields.cpp
T
faumarayandClaude Fable 5 6b5f61f00a Logging: colors, configurable/custom pattern tokens, split + history sinks
Pattern formatting (PatternFormatter):
- Token spec grammar {token:[align][width][,color]} — align <,>,^,
  std::format-style width, color a name or "auto" (= the level's color).
- Custom tokens via register_token(name, fn); unknown {name} resolves
  against registered renderers, then Entry::fields by key, else stays
  literal — so structured fields print as {request_id:>8}.
- Color model: AnsiColor + ColorPolicy{enabled, level_colors,
  token_colors} (ColorPolicy::defaults()); only PatternFormatter
  colorizes (never file/JSON/logfmt). ConsoleLogger now renders its
  default line through a colored PatternFormatter and exposes
  set_color_policy (honors Stacktrace::Color::g_color_enabled).

Structured fields:
- Entry gains a fields vector; Logging::FieldScope (RAII, thread-local)
  attaches key/values to every entry on the thread, snapshotted like
  scopes. JsonLines/logfmt include them.

New file sinks (async by default):
- Detail::RollingFile factors size+max-files rotation; RotatingFileLogger
  now delegates to it.
- SplitFileLogger: per-level files (<dir>/<level>.log) plus an optional
  combined <dir>/all.log.
- HistoryLogger: always-on, never-level-filtered durable JSON-lines
  audit trail over a rolling file.
- from_config gains split/history sinks and a log.color toggle.

22 new tests (pattern width/align/custom-token/field, color on/off + name
parsing, FieldScope nesting, split routing + combined, history JSON +
rotation, from_config split/history). 987 green; module build clean
under -Werror.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 00:24:31 +03:00

61 lines
1.6 KiB
C++

#include <Void/Log/Field.hpp>
#include <Void/Log/Formatter.hpp>
#include <Void/Log/Sinks.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
using namespace Void::Logging;
Void::Testing::Test field_scope_attaches_to_entries()
{
std::vector<Entry> captured;
CallbackLogger log{[&](const Entry& e) { captured.push_back(e); },
Mode::Immediate};
{
FieldScope svc{"service", "billing"};
log.info("inside scope");
}
log.info("outside scope");
Void::Testing::expect_eq(captured.size(), std::size_t{2});
Void::Testing::expect_eq(captured[0].fields.size(), std::size_t{1});
Void::Testing::expect_eq(captured[0].fields[0].first,
std::string{"service"});
Void::Testing::expect_eq(captured[0].fields[0].second,
std::string{"billing"});
Void::Testing::expect(captured[1].fields.empty());
}
Void::Testing::Test field_scope_nests()
{
std::vector<Entry> captured;
CallbackLogger log{[&](const Entry& e) { captured.push_back(e); },
Mode::Immediate};
FieldScope a{"a", "1"};
{
FieldScope b{{{"b", "2"}, {"c", "3"}}};
log.info("two scopes");
}
log.info("one scope");
Void::Testing::expect_eq(captured[0].fields.size(), std::size_t{3});
Void::Testing::expect_eq(captured[1].fields.size(), std::size_t{1});
}
Void::Testing::Test field_scope_feeds_pattern_token()
{
std::string rendered;
PatternFormatter fmt{"[{request_id}] {message}"};
CallbackLogger log{[&](const Entry& e) { rendered = fmt.format(e); },
Mode::Immediate};
FieldScope req{"request_id", "r-42"};
log.warning("processing");
Void::Testing::expect_eq(rendered, std::string{"[r-42] processing"});
}