Files
Library/tests/standalone/LogConfig/FromConfig.cpp

161 lines
5.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;
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.fields.redact", "token");
cfg.set("log.async", "false");
const auto logger = Void::Logging::from_config(cfg);
Void::Testing::expect(logger.has_value());
{
Void::Logging::FieldScope fs{"token", "top-secret"};
(*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::expect(line.find("top-secret") == std::string::npos);
Void::Testing::expect(line.find("[REDACTED]") != 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 from_config_rejects_invalid_values()
{
{
Config cfg;
cfg.set("log.level", "loud");
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
}
{
Config cfg;
cfg.set("log.format", "xml");
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
}
{
Config cfg;
cfg.set("log.overflow", "drop_middle");
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
}
{
Config cfg;
cfg.set("log.capacity", "0");
Void::Testing::expect(!Void::Logging::from_config(cfg).has_value());
}
{
Void::FileSystem::TempDir tmp{"void_fromcfg_bad"};
Config cfg;
cfg.set("log.sinks", "rotating");
cfg.set("log.rotating.path", (tmp.path() / "r.log").string());
cfg.set("log.rotating.max_kb", "0");
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, 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({});
}