34#include <source_location>
38#include <unordered_map>
68 std::source_location location = std::source_location::current();
69 std::vector<LogAttribute> attributes;
91 std::source_location location = std::source_location::current())
92 : level_(level), location_(location) {}
96 message_ = std::move(message);
101 Builder&
Attribute(std::string key, std::string value) {
102 attributes_.push_back(
LogAttribute{.key = std::move(key), .value = std::move(value)});
108 location_ = location;
115 .message = std::move(message_),
116 .location = location_,
117 .attributes = std::move(attributes_)};
122 std::string message_;
124 std::source_location location_;
125 std::vector<LogAttribute> attributes_;
134inline constexpr std::string_view kPatternProperty =
"pattern";
157 const std::unordered_map<std::string, std::string>& properties) {
158 if (
auto it = properties.find(std::string(
kLevelProperty)); it != properties.end()) {
160 if (!parsed)
return std::unexpected(parsed.error());
179 virtual
void Flush() noexcept {}
182 virtual bool IsNoop()
const {
return false; }
185 static std::shared_ptr<Logger>
Noop();
224 std::function<void(
const std::source_location&, std::string_view message)>;
258 explicit ScopedLogger(std::shared_ptr<Logger> logger)
noexcept;
267 std::shared_ptr<Logger> previous_;
301ICEBERG_EXPORT std::unique_ptr<Logger> MakeNoopLogger();
309ICEBERG_EXPORT
const std::shared_ptr<Logger>& CurrentLogger() noexcept;
316 const
std::source_location& location,
std::
string&& message);
323ICEBERG_EXPORT
void EmitFormatError(
Logger& logger,
LogLevel level,
324 const
std::source_location& location) noexcept;
332template <typename... Args>
334 std::format_string<Args...> fmt;
335 std::source_location loc;
337 template <
typename T>
338 requires std::convertible_to<
const T&, std::format_string<Args...>>
339 consteval FmtWithLoc(
341 const T& s, std::source_location loc = std::source_location::current())
342 : fmt(s), loc(loc) {}
349template <
typename... Args>
350void FormatAndEmit(Logger& logger,
LogLevel level,
const std::source_location& loc,
351 std::format_string<Args...> fmt, Args&&... args)
noexcept {
352 if (!logger.ShouldLog(level))
return;
354 Emit(logger, level, loc, std::format(fmt, std::forward<Args>(args)...));
359 EmitFormatError(logger, level, loc);
369template <
typename... Args>
370void Log(
LogLevel level, internal::FmtWithLoc<std::type_identity_t<Args>...> fmt,
371 Args&&... args)
noexcept {
372 const std::shared_ptr<Logger>& logger = internal::CurrentLogger();
374 internal::FormatAndEmit(*logger, level, fmt.loc, fmt.fmt,
375 std::forward<Args>(args)...);
382template <
typename... Args>
384 internal::FmtWithLoc<std::type_identity_t<Args>...> fmt,
385 Args&&... args)
noexcept {
386 internal::FormatAndEmit(logger, level, fmt.loc, fmt.fmt, std::forward<Args>(args)...);
Pluggable logging sink.
Definition logger.h:146
virtual void Log(LogMessage &&message) noexcept=0
Emit one (already-formatted) record, taking ownership. Must not throw.
virtual LogLevel level() const noexcept=0
Return the minimum level this logger emits.
static std::shared_ptr< Logger > Noop()
Return a shared, immortal no-op logger singleton.
virtual void SetLevel(LogLevel level) noexcept=0
Set the minimum level this logger emits.
virtual bool ShouldLog(LogLevel level) const noexcept=0
Cheap check whether a record at level would be emitted.
virtual bool IsNoop() const
Return true if this logger is a no-op.
Definition logger.h:182
virtual Status Initialize(const std::unordered_map< std::string, std::string > &properties)
Property-based setup, called by Loggers::Load() before first use.
Definition logger.h:156
Bind a logger for the current thread until this object leaves scope.
Definition logger.h:256
Define symbol visibility macros for core Iceberg APIs.
Severity levels for the logging system.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
ICEBERG_EXPORT FatalHandler GetFatalHandler()
Return the installed fatal handler (empty if none). Used by the fatal logging path; thread-safe.
ICEBERG_EXPORT void SetDefaultLevel(LogLevel level)
Set the minimum level of the current default logger.
void Log(LogLevel level, internal::FmtWithLoc< std::type_identity_t< Args >... > fmt, Args &&... args) noexcept
Log to the process-default logger, std::format style. Formats only if the level is enabled; never thr...
Definition logger.h:370
ICEBERG_EXPORT void SetDefaultLogger(std::shared_ptr< Logger > logger)
Install a new process-global default logger.
constexpr std::string_view kLevelProperty
Well-known Logger::Initialize() property keys.
Definition logger.h:133
ICEBERG_EXPORT void SetFatalHandler(FatalHandler handler)
Install (or clear, with nullptr) the process-global fatal handler.
std::function< void(const std::source_location &, std::string_view message)> FatalHandler
A hook invoked on the fatal-log path just before std::abort().
Definition logger.h:224
ICEBERG_EXPORT std::shared_ptr< Logger > GetCurrentLogger()
Return the effective logger for this thread (never null): the active ScopedLogger binding if any,...
Result< LogLevel > LogLevelFromString(std::string_view s)
Parse a LogLevel from a string (case-insensitive).
Definition log_level.h:77
LogLevel
Logging severity level, ordered from most to least verbose.
Definition log_level.h:38
ICEBERG_EXPORT std::shared_ptr< Logger > GetDefaultLogger()
Return the process-global default logger (never null).
Define Result, Status, and error helpers.
A structured key/value attribute attached to a log record.
Definition logger.h:53
A single log record handed to a Logger.
Definition logger.h:65
Builder & Location(std::source_location location)
Override the record's source location (defaults to the build site).
Definition logger.h:107
LogMessage Build()
Materialize the LogMessage, moving the accumulated state out.
Definition logger.h:113
Builder & Attribute(std::string key, std::string value)
Append a structured key/value attribute.
Definition logger.h:101
Builder & Message(std::string message)
Set the already-formatted message text.
Definition logger.h:95