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

+110
View File
@@ -0,0 +1,110 @@
#include <Void/Log/SplitFileLogger.hpp>
#include <Void/Log/Log.hpp>
#include <cctype>
#include <format>
#include <stdexcept>
#include <system_error>
namespace Void::Logging
{
namespace
{
std::string level_filename(Level level)
{
std::string name{to_string(level)};
for (auto& c : name) {
c = static_cast<char>(
std::tolower(static_cast<unsigned char>(c)));
}
return name + ".log";
}
} // namespace
SplitFileLogger::SplitFileLogger(std::filesystem::path dir, Options options)
: Logger(options.mode, options.capacity, options.policy),
m_dir(std::move(dir)), m_options(options)
{
std::error_code ec;
std::filesystem::create_directories(m_dir, ec);
if (ec) {
throw std::runtime_error(std::format(
"SplitFileLogger: cannot create '{}'", m_dir.string()));
}
if (m_options.combined) {
m_combined = std::make_unique<Detail::RollingFile>(
m_dir / "all.log",
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files});
(void)m_combined->open();
}
Void::Log::debug("Log.Split", "split logging into '{}'",
m_dir.string());
}
Void::Result::Result<std::unique_ptr<SplitFileLogger>, Void::Result::Error>
SplitFileLogger::open(std::filesystem::path dir, Options options)
{
try {
return Void::Result::ok(
std::make_unique<SplitFileLogger>(std::move(dir), options));
} catch (const std::exception& e) {
return Void::Result::fail(std::string{e.what()});
}
}
SplitFileLogger::~SplitFileLogger() noexcept { flush(); }
Detail::RollingFile& SplitFileLogger::file_for(Level level)
{
auto it = m_per_level.find(level);
if (it == m_per_level.end()) {
Detail::RollingFile file{
m_dir / level_filename(level),
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files}};
(void)file.open();
it = m_per_level.emplace(level, std::move(file)).first;
}
return it->second;
}
void SplitFileLogger::append(const Entry& entry)
{
std::string line;
if (m_formatter) {
line = m_formatter->format(entry);
} else {
line =
std::format("[{}] {}", to_string(entry.level), entry.message);
}
file_for(entry.level).write(line);
if (m_combined) {
m_combined->write(line);
}
}
void SplitFileLogger::write(const Entry& entry)
{
append(entry);
file_for(entry.level).flush();
if (m_combined) {
m_combined->flush();
}
}
void SplitFileLogger::on_flush(std::span<const Entry> entries)
{
for (const auto& e : entries) {
append(e);
}
for (auto& [level, file] : m_per_level) {
file.flush();
}
if (m_combined) {
m_combined->flush();
}
}
} // namespace Void::Logging