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:
1 parent
ff6960a2af
commit
6b5f61f00a
22 files changed
+1384
-233
No files matched your search
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
@@ -11,20 +10,14 @@ namespace Void::Logging
|
||||
RotatingFileLogger::RotatingFileLogger(const std::filesystem::path& path,
|
||||
Options options)
|
||||
: Logger(options.mode, options.capacity, options.policy), m_path(path),
|
||||
m_options(options)
|
||||
m_file(path, Detail::RollingFileOptions{options.max_bytes,
|
||||
options.max_files})
|
||||
{
|
||||
std::error_code ec;
|
||||
if (std::filesystem::exists(m_path, ec)) {
|
||||
m_current_bytes = static_cast<std::size_t>(
|
||||
std::filesystem::file_size(m_path, ec));
|
||||
}
|
||||
m_file.open(m_path, std::ios::app);
|
||||
if (!m_file.is_open()) {
|
||||
if (!m_file.open()) {
|
||||
throw std::runtime_error(std::format(
|
||||
"RotatingFileLogger: cannot open '{}'", m_path.string()));
|
||||
}
|
||||
Void::Log::debug("Log.RotatingFile", "opened '{}' ({} bytes)",
|
||||
m_path.string(), m_current_bytes);
|
||||
Void::Log::debug("Log.RotatingFile", "opened '{}'", m_path.string());
|
||||
}
|
||||
|
||||
Void::Result::Result<std::unique_ptr<RotatingFileLogger>,
|
||||
@@ -46,7 +39,6 @@ namespace Void::Logging
|
||||
{
|
||||
append(entry);
|
||||
m_file.flush();
|
||||
rotate_if_needed();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::on_flush(std::span<const Entry> entries)
|
||||
@@ -55,55 +47,16 @@ namespace Void::Logging
|
||||
append(entry);
|
||||
}
|
||||
m_file.flush();
|
||||
rotate_if_needed();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::append(const Entry& entry)
|
||||
{
|
||||
std::string line;
|
||||
if (m_formatter) {
|
||||
line = m_formatter->format(entry);
|
||||
m_file.write(m_formatter->format(entry));
|
||||
} else {
|
||||
line =
|
||||
std::format("[{}] {}", to_string(entry.level), entry.message);
|
||||
m_file.write(
|
||||
std::format("[{}] {}", to_string(entry.level), entry.message));
|
||||
}
|
||||
line += '\n';
|
||||
m_file << line;
|
||||
m_current_bytes += line.size();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::rotate_if_needed()
|
||||
{
|
||||
if (m_current_bytes < m_options.max_bytes) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_file.close();
|
||||
std::error_code ec;
|
||||
|
||||
// Shift app.log.(N-1) -> app.log.N, ..., app.log -> app.log.1;
|
||||
// the oldest file falls off the end.
|
||||
const auto numbered = [&](std::size_t n) {
|
||||
auto p = m_path;
|
||||
p += std::format(".{}", n);
|
||||
return p;
|
||||
};
|
||||
std::filesystem::remove(numbered(m_options.max_files), ec);
|
||||
for (std::size_t n = m_options.max_files; n > 1; --n) {
|
||||
const auto from = numbered(n - 1);
|
||||
if (std::filesystem::exists(from, ec)) {
|
||||
std::filesystem::rename(from, numbered(n), ec);
|
||||
}
|
||||
}
|
||||
if (m_options.max_files > 0) {
|
||||
std::filesystem::rename(m_path, numbered(1), ec);
|
||||
} else {
|
||||
std::filesystem::remove(m_path, ec);
|
||||
}
|
||||
|
||||
m_file.open(m_path, std::ios::trunc);
|
||||
m_current_bytes = 0;
|
||||
Void::Log::debug("Log.RotatingFile", "rotated '{}'", m_path.string());
|
||||
}
|
||||
|
||||
} // namespace Void::Logging
|
||||
Reference in new issue
Block a user