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>
131 lines
4.2 KiB
C++
131 lines
4.2 KiB
C++
#include <Void/Configuration/Config.hpp>
|
|
#include <Void/Log/FromConfig.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <Void/Log/RotatingFileLogger.hpp>
|
|
#include <Void/Log/Sinks.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
using Void::Configuration::Config;
|
|
|
|
Void::Testing::Test from_config_defaults_to_console()
|
|
{
|
|
Config cfg; // empty: console sink, trace level, async
|
|
const auto logger = Void::Logging::from_config(cfg);
|
|
Void::Testing::expect(logger.has_value());
|
|
Void::Testing::expect((*logger)->mode() ==
|
|
Void::Logging::Mode::Async);
|
|
}
|
|
|
|
Void::Testing::Test from_config_builds_multi_sink_tree()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fromcfg"};
|
|
Config cfg;
|
|
cfg.set("log.sinks", "console, file");
|
|
cfg.set("log.file.path", (tmp.path() / "a.log").string());
|
|
cfg.set("log.level", "warning");
|
|
cfg.set("log.async", "false");
|
|
|
|
const auto logger = Void::Logging::from_config(cfg);
|
|
Void::Testing::expect(logger.has_value());
|
|
Void::Testing::expect((*logger)->mode() ==
|
|
Void::Logging::Mode::Immediate);
|
|
Void::Testing::expect((*logger)->level() ==
|
|
Void::Logging::Level::Warning);
|
|
|
|
(*logger)->warning("to both sinks");
|
|
(*logger)->flush();
|
|
std::ifstream f(tmp.path() / "a.log");
|
|
std::string content((std::istreambuf_iterator<char>(f)), {});
|
|
Void::Testing::expect(content.find("to both sinks") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test from_config_split_and_history_sinks()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fromcfg"};
|
|
Config cfg;
|
|
cfg.set("log.sinks", "split, history");
|
|
cfg.set("log.split.dir", (tmp.path() / "split").string());
|
|
cfg.set("log.history.path", (tmp.path() / "history.jsonl").string());
|
|
cfg.set("log.async", "false");
|
|
|
|
const auto logger = Void::Logging::from_config(cfg);
|
|
Void::Testing::expect(logger.has_value());
|
|
(*logger)->error("split me");
|
|
(*logger)->flush();
|
|
|
|
std::ifstream err(tmp.path() / "split" / "error.log");
|
|
std::string err_content((std::istreambuf_iterator<char>(err)), {});
|
|
Void::Testing::expect(err_content.find("split me") !=
|
|
std::string::npos);
|
|
|
|
std::ifstream hist(tmp.path() / "history.jsonl");
|
|
std::string hist_content((std::istreambuf_iterator<char>(hist)), {});
|
|
Void::Testing::expect(hist_content.find("split me") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test from_config_json_format_applies()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fromcfg"};
|
|
Config cfg;
|
|
cfg.set("log.sinks", "file");
|
|
cfg.set("log.file.path", (tmp.path() / "j.log").string());
|
|
cfg.set("log.format", "json");
|
|
cfg.set("log.async", "false");
|
|
|
|
const auto logger = Void::Logging::from_config(cfg);
|
|
Void::Testing::expect(logger.has_value());
|
|
(*logger)->info("structured");
|
|
(*logger)->flush();
|
|
|
|
std::ifstream f(tmp.path() / "j.log");
|
|
std::string line;
|
|
std::getline(f, line);
|
|
Void::Testing::expect(line.find("\"message\":\"structured\"") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test from_config_missing_file_path_is_error()
|
|
{
|
|
Config cfg;
|
|
cfg.set("log.sinks", "file");
|
|
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
|
|
}
|
|
|
|
Void::Testing::Test from_config_unknown_sink_is_error()
|
|
{
|
|
Config cfg;
|
|
cfg.set("log.sinks", "carrier-pigeon");
|
|
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
|
|
}
|
|
|
|
Void::Testing::Test apply_facade_config_sets_levels()
|
|
{
|
|
struct Reset {
|
|
~Reset()
|
|
{
|
|
Void::Log::set_min_level(Void::Log::Level::Trace);
|
|
Void::Log::clear_module_level("Fsm");
|
|
}
|
|
} reset;
|
|
|
|
Config cfg;
|
|
cfg.set("log.level", "error");
|
|
cfg.set("log.module_levels.Fsm", "trace");
|
|
Void::Logging::apply_facade_config(cfg);
|
|
|
|
Void::Log::set_sink([](auto, auto, auto) {});
|
|
Void::Testing::expect(
|
|
!Void::Log::enabled(Void::Log::Level::Info, "Other"));
|
|
Void::Testing::expect(
|
|
Void::Log::enabled(Void::Log::Level::Trace, "Fsm"));
|
|
Void::Log::set_sink({});
|
|
}
|