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
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <initializer_list>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
/// RAII structured-field context, per thread. While alive, the fields
|
||||
/// it pushes are attached to every Entry produced on this thread (they
|
||||
/// appear in Entry::fields and so in `{field:key}` pattern tokens and
|
||||
/// JSON/logfmt output). Nest freely; later duplicate keys win.
|
||||
///
|
||||
/// Logging::FieldScope req{"request_id", id};
|
||||
/// logger.info("handling"); // entry carries request_id=<id>
|
||||
class FieldScope
|
||||
{
|
||||
public:
|
||||
|
||||
FieldScope(std::string key, std::string value);
|
||||
explicit FieldScope(
|
||||
std::initializer_list<std::pair<std::string, std::string>>
|
||||
fields);
|
||||
|
||||
~FieldScope() noexcept;
|
||||
|
||||
FieldScope(const FieldScope&) = delete;
|
||||
FieldScope& operator=(const FieldScope&) = delete;
|
||||
FieldScope(FieldScope&&) noexcept;
|
||||
FieldScope& operator=(FieldScope&&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
std::size_t m_count{0}; // how many entries to pop
|
||||
bool m_active{true};
|
||||
};
|
||||
|
||||
/// Snapshot view of this thread's active fields, outermost first.
|
||||
[[nodiscard]] std::span<const std::pair<std::string, std::string>>
|
||||
current_fields() noexcept;
|
||||
|
||||
} // namespace Void::Logging
|
||||
Reference in new issue
Block a user