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>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 00:24:31 +03:00
1 parent ff6960a2af
commit 6b5f61f00a
22 files changed
+1384 -233

No files matched your search

+10 -65
View File
@@ -1,86 +1,35 @@
#include <Void/Detail/Enum.hpp>
#include <Void/Log/ConsoleLogger.hpp>
#ifdef VOID_HAS_STACKTRACE
#include <Void/Stacktrace/Color.hpp>
#endif
#include <format>
#include <iostream>
#include <ostream>
#include <string>
#include <string_view>
namespace Void::Logging
{
namespace
{
#ifdef VOID_HAS_STACKTRACE
std::string_view color_for(Level level)
ColorPolicy console_default_policy()
{
using namespace Void::Stacktrace::Color;
#ifdef VOID_HAS_ENUM_REFLECTION
static constexpr auto enumerators =
std::define_static_array(std::meta::enumerators_of(^^Level));
template for (constexpr auto e : enumerators) {
if (level == [:e:]) {
if constexpr (std::meta::identifier_of(e) == "Trace")
return dim;
if constexpr (std::meta::identifier_of(e) == "Debug")
return cyan;
if constexpr (std::meta::identifier_of(e) == "Info")
return bold_cyan;
if constexpr (std::meta::identifier_of(e) == "Warning")
return bold_yellow;
if constexpr (std::meta::identifier_of(e) == "Error")
return bold_red;
if constexpr (std::meta::identifier_of(e) == "Fatal")
return bold_magenta;
return reset;
}
}
return reset;
#else
switch (level) {
case Level::Trace:
return dim;
case Level::Debug:
return cyan;
case Level::Info:
return bold_cyan;
case Level::Warning:
return bold_yellow;
case Level::Error:
return bold_red;
case Level::Fatal:
return bold_magenta;
default:
return reset;
}
#endif
}
#endif
std::string format_entry(const Entry& entry)
{
auto level_str = std::string{to_string(entry.level)};
ColorPolicy p = ColorPolicy::defaults();
#ifdef VOID_HAS_STACKTRACE
using namespace Void::Stacktrace::Color;
return std::format("[{}] {}\n",
apply(color_for(entry.level), level_str),
entry.message);
// Honor the global color toggle when Stacktrace is present.
p.enabled = Void::Stacktrace::Color::g_color_enabled;
#else
return std::format("[{}] {}\n", level_str, entry.message);
p.enabled = false;
#endif
return p;
}
} // namespace
ConsoleLogger::ConsoleLogger(std::ostream& out,
Mode mode,
ConsoleLogger::ConsoleLogger(std::ostream& out, Mode mode,
std::size_t capacity)
: Logger(mode, capacity), m_out(out)
{
m_default.set_color_policy(console_default_policy());
}
ConsoleLogger::ConsoleLogger() : ConsoleLogger(std::cerr) {}
@@ -89,12 +38,8 @@ namespace Void::Logging
void ConsoleLogger::write(const Entry& entry)
{
if (m_formatter) {
m_out << m_formatter->format(entry) << '\n';
}
else {
m_out << format_entry(entry);
}
const Formatter& fmt = m_formatter ? *m_formatter : m_default;
m_out << fmt.format(entry) << '\n';
m_out.flush();
}