Files
Library/include/Void/Log/RotatingFileLogger.hpp
T
faumarayandClaude Fable 5 6b5f61f00a 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>
2026-06-14 00:24:31 +03:00

80 lines
2.2 KiB
C++

#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Log/Detail/RollingFile.hpp>
#include <Void/Log/Formatter.hpp>
#include <Void/Log/Logger.hpp>
#include <cstddef>
#include <filesystem>
#include <memory>
#include <span>
namespace Void::Logging
{
/// File sink with size-based rotation.
///
/// When the current file reaches `max_bytes` after a batch, files are
/// shifted (`app.log` → `app.log.1` → `app.log.2` …); at most
/// `max_files` rotated files are kept, the oldest is deleted.
///
/// Defaults to Async dispatch — rotation and writes happen on the
/// worker thread. An optional Formatter overrides the default
/// "[Level] message" rendering; set it before logging starts.
struct RotatingFileOptions {
std::size_t max_bytes = 1024 * 1024;
std::size_t max_files = 5;
Mode mode = Mode::Async;
std::size_t capacity = 512;
OverflowPolicy policy = OverflowPolicy::Block;
};
class RotatingFileLogger final : public Logger
{
public:
using Options = RotatingFileOptions;
explicit RotatingFileLogger(const std::filesystem::path& path,
Options options = {});
~RotatingFileLogger() noexcept override;
RotatingFileLogger(const RotatingFileLogger&) = delete;
RotatingFileLogger& operator=(const RotatingFileLogger&) = delete;
RotatingFileLogger(RotatingFileLogger&&) = delete;
RotatingFileLogger& operator=(RotatingFileLogger&&) = delete;
/// Non-throwing factory.
[[nodiscard]] static Void::Result::
Result<std::unique_ptr<RotatingFileLogger>, Void::Result::Error>
open(const std::filesystem::path& path, Options options = {});
/// Install a formatter. Call before logging starts.
void set_formatter(std::shared_ptr<const Formatter> formatter)
{
m_formatter = std::move(formatter);
}
[[nodiscard]] const std::filesystem::path& path() const noexcept
{
return m_path;
}
protected:
void write(const Entry& entry) override;
void on_flush(std::span<const Entry> entries) override;
private:
void append(const Entry& entry);
std::filesystem::path m_path;
Detail::RollingFile m_file;
std::shared_ptr<const Formatter> m_formatter;
};
} // namespace Void::Logging