userver: logging Namespace Reference
Loading...
Searching...
No Matches
logging Namespace Reference

Detailed Description

Logging levels, helpers, default logger, and related utilities.

Logging macro and utilities.

Namespaces

namespace  stacktrace_cache
 Cached stringification for stack traces.

Classes

class  DefaultLoggerGuard
 Atomically replaces the default logger. More...
class  DefaultLoggerLevelScope
 Allows to override global log level for the whole service within a scope. Primarily for use in tests. More...
struct  Hex
 Formats value in a hex mode with the fixed length representation. More...
struct  HexShort
class  JsonString
class  LogExtra
 Extra tskv fields storage. More...
class  LogHelper
struct  Quoted
 Formats a string as quoted, escaping the '\' and '"' symbols. More...
struct  TimeString

Typedefs

using LoggerRef = impl::LoggerBase&
using LoggerPtr = std::shared_ptr<impl::LoggerBase>
using TextLoggerRef = impl::TextLogger&
using TextLoggerPtr = std::shared_ptr<impl::TextLogger>

Enumerations

enum class  Level : std::uint8_t {
  kTrace = 0 ,
  kDebug = 1 ,
  kInfo = 2 ,
  kWarning = 3 ,
  kError = 4 ,
  kCritical = 5 ,
  kNone = 6
}
 Log levels. More...
enum class  LogClass {
  kLog ,
  kTrace
}
enum class  Format {
  kTskv ,
  kLtsv ,
  kRaw ,
  kJson ,
  kJsonYaDeploy
}
 Text-based log formats. More...

Functions

LoggerRef GetDefaultLogger () noexcept
 Returns the default logger previously set by SetDefaultLogger. If the logger was not set - returns a logger that does no logging.
void SetDefaultLoggerLevel (Level)
 Sets new log level for the default logger.
Level GetDefaultLoggerLevel () noexcept
 Returns log level for the default logger.
bool ShouldLog (Level level) noexcept
void SetLoggerLevel (LoggerRef, Level)
 Sets new log level for a logger.
bool LoggerShouldLog (LoggerRef logger, Level level) noexcept
bool LoggerShouldLog (const LoggerPtr &logger, Level level) noexcept
Level GetLoggerLevel (LoggerRef logger) noexcept
void LogFlush ()
 Forces flush of default logger message queue.
void LogFlush (LoggerRef logger)
 Forces flush of logger message queue.
logging::LogHelperoperator<< (logging::LogHelper &h, const google::protobuf::Message &message)
 Logs the protobuf message as a debug string (see protobuf::json::MessageToDebugString).
template<typename T>
requires (std::is_base_of_v<::google::protobuf::Message, T>)
logging::LogHelperoperator<< (logging::LogHelper &h, const T &message)
 Logs message — any protobuf message type, derived from or equal to google::protobuf::Message — as a debug string, forwarding to the overload above.
Level LevelFromString (std::string_view)
 Converts lowercase level name to a corresponding Level, throws std::runtime_error if no matching log level found.
std::string_view ToString (Level level)
 Returns a string representation of logging level, e.g. "info".
std::string_view ToUpperCaseString (Level level) noexcept
 Returns a string representation of logging level, e.g. "INFO".
std::optional< LevelOptionalLevelFromString (const std::optional< std::string > &level_name)
 Returns std::nullopt if level_name is std::nullopt, otherwise behaves exactly like logging::LevelFromString.
LogHelperoperator<< (LogHelper &lh, std::error_code ec)
template<typename T>
LogHelperoperator<< (LogHelper &lh, const std::atomic< T > &value)
template<typename T>
LogHelperoperator<< (LogHelper &lh, const T *value) noexcept
template<typename T>
LogHelperoperator<< (LogHelper &lh, T *value)
template<typename T>
LogHelperoperator<< (LogHelper &lh, const std::optional< T > &value)
template<typename Fun>
requires std::is_invocable_r_v<void, Fun, LogHelper&>
LogHelperoperator<< (LogHelper &lh, Fun &&value)
template<class Result, class... Args>
LogHelperoperator<< (LogHelper &lh, Result(*)(Args...))
LogHelperoperator<< (LogHelper &lh, std::chrono::system_clock::time_point tp)
LogHelperoperator<< (LogHelper &lh, std::chrono::seconds value)
LogHelperoperator<< (LogHelper &lh, std::chrono::milliseconds value)
LogHelperoperator<< (LogHelper &lh, std::chrono::microseconds value)
LogHelperoperator<< (LogHelper &lh, std::chrono::nanoseconds value)
LogHelperoperator<< (LogHelper &lh, std::chrono::minutes value)
LogHelperoperator<< (LogHelper &lh, std::chrono::hours value)
Format FormatFromString (std::string_view format_str)
 Parse Format enum from string.
void WriteToStream (const JsonString &value, formats::json::StringBuilder &sw)
template<typename... Types>
LogHelperoperator<< (LogHelper &lh, const std::tuple< Types... > &value)
TextLoggerRef GetNullLogger () noexcept
 Returns a logger that drops all incoming messages.
TextLoggerPtr MakeNullLogger ()
 Creates a logger that drops all incoming messages.
TimeString GetCurrentGMTimeString (std::chrono::system_clock::time_point time) noexcept
 Formats time in UTC as YYYY-MM-DDTHH:MM:SS.
TimeString GetCurrentLocalTimeString (std::chrono::system_clock::time_point time) noexcept
 Formats time in the system local timezone as YYYY-MM-DDTHH:MM:SS.
Level Parse (const yaml_config::YamlConfig &value, formats::parse::To< Level >)
Level Parse (const formats::json::Value &value, formats::parse::To< Level >)
LoggerPtr MakeStderrLogger (const std::string &name, Format format, Level level=Level::kInfo)
 Creates synchronous stderr logger with default tskv pattern.
LoggerPtr MakeStdoutLogger (const std::string &name, Format format, Level level=Level::kInfo)
 Creates synchronous stdout logger with default tskv pattern.
LoggerPtr MakeFileLogger (const std::string &name, const std::string &path, Format format, Level level=Level::kInfo)
 Creates synchronous file logger with default tskv pattern.
template<typename TStruct>
requires proto_structs::traits::ProtoStruct<std::remove_cvref_t<TStruct>>
logging::LogHelperoperator<< (logging::LogHelper &h, const TStruct &obj)
 Logs the protobuf struct obj as a debug string (see logging::operator<< for google::protobuf::Message). The representation uses proto field names (not json_name), is truncated once it grows too large, [debug_redact = true] fields are redacted, and it must not be parsed back or relied upon for stability.

Variables

constexpr auto kLevelMax = static_cast<int>(Level::kNone)
const LogExtra kEmptyLogExtra

Typedef Documentation

◆ LoggerPtr

using logging::LoggerPtr = std::shared_ptr<impl::LoggerBase>

Definition at line 22 of file fwd.hpp.

◆ LoggerRef

using logging::LoggerRef = impl::LoggerBase&

Definition at line 21 of file fwd.hpp.

◆ TextLoggerPtr

using logging::TextLoggerPtr = std::shared_ptr<impl::TextLogger>

Definition at line 24 of file fwd.hpp.

◆ TextLoggerRef

using logging::TextLoggerRef = impl::TextLogger&

Definition at line 23 of file fwd.hpp.

Enumeration Type Documentation

◆ Format

enum class logging::Format
strong

Text-based log formats.

For otlp logs, see Logging and Tracing

Definition at line 15 of file format.hpp.

◆ Level

enum class logging::Level : std::uint8_t
strong

Log levels.

Enumerator
kTrace 

Level for very verbose debug messages.

kDebug 

Level for debug messages.

kInfo 

Level for non-error informational messages.

kWarning 

Level for warning messages.

kError 

Level for error messages.

kCritical 

Level for fatal error messages that can not be disabled.

kNone 

"Do not output messages" level

Definition at line 16 of file level.hpp.

◆ LogClass

enum class logging::LogClass
strong

Definition at line 69 of file log_helper.hpp.

Function Documentation

◆ GetCurrentGMTimeString()

TimeString logging::GetCurrentGMTimeString ( std::chrono::system_clock::time_point time)
noexcept

Formats time in UTC as YYYY-MM-DDTHH:MM:SS.

This function is coroutine-safe and does not block. It caches the formatted value in coroutine-safe thread-local storage.

◆ GetCurrentLocalTimeString()

TimeString logging::GetCurrentLocalTimeString ( std::chrono::system_clock::time_point time)
noexcept

Formats time in the system local timezone as YYYY-MM-DDTHH:MM:SS.

This function is coroutine-safe and does not block. It caches the formatted value in coroutine-safe thread-local storage.

◆ GetDefaultLogger()

LoggerRef logging::GetDefaultLogger ( )
noexcept

Returns the default logger previously set by SetDefaultLogger. If the logger was not set - returns a logger that does no logging.

Note
While the coroutine engine is running, any reference to the default logger is guaranteed to be alive. No lifetime guarantees are given for the default logger reference outside the engine's lifetime. The rule of thumb there is not to keep this reference in any extended scope.

◆ GetNullLogger()

TextLoggerRef logging::GetNullLogger ( )
noexcept

Returns a logger that drops all incoming messages.

See also
components::Logging

◆ MakeFileLogger()

LoggerPtr logging::MakeFileLogger ( const std::string & name,
const std::string & path,
Format format,
Level level = Level::kInfo )

Creates synchronous file logger with default tskv pattern.

Parameters
namelogger name, for internal use, must be unique
pathtarget log file path
formatlogs output format
levelminimum logs level to write to this logger, a.k.a. logger's log level
See also
components::Logging

◆ MakeNullLogger()

TextLoggerPtr logging::MakeNullLogger ( )

Creates a logger that drops all incoming messages.

Use GetNullLogger() is you need a reference to logger.

See also
components::Logging

◆ MakeStderrLogger()

LoggerPtr logging::MakeStderrLogger ( const std::string & name,
Format format,
Level level = Level::kInfo )

Creates synchronous stderr logger with default tskv pattern.

Parameters
namelogger name, for internal use, must be unique
formatlogs output format
levelminimum logs level to write to this logger, a.k.a. logger's log level
See also
components::Logging

◆ MakeStdoutLogger()

LoggerPtr logging::MakeStdoutLogger ( const std::string & name,
Format format,
Level level = Level::kInfo )

Creates synchronous stdout logger with default tskv pattern.

Parameters
namelogger name, for internal use, must be unique
formatlogs output format
levelminimum logs level to write to this logger, a.k.a. logger's log level
See also
components::Logging

◆ operator<<() [1/11]

logging::LogHelper & logging::operator<< ( logging::LogHelper & h,
const google::protobuf::Message & message )

Logs the protobuf message as a debug string (see protobuf::json::MessageToDebugString).

Definition at line 9 of file log.cpp.

◆ operator<<() [2/11]

template<typename T>
requires (std::is_base_of_v<::google::protobuf::Message, T>)
logging::LogHelper & logging::operator<< ( logging::LogHelper & h,
const T & message )

Logs message — any protobuf message type, derived from or equal to google::protobuf::Message — as a debug string, forwarding to the overload above.

This has to be a constrained template rather than an unconstrained one: for a concrete derived message type, LogHelper's own generic member operator<<(const T&) is an exact match, while binding straight to const google::protobuf::Message& requires a derived-to-base conversion — so an unconstrained free function would always lose to the member. Deducing T directly gives this overload an equally exact match; C++20 partial ordering then prefers the more constrained candidate (this one) over the member's unconstrained template.

Warning
The constraint must actually restrict T to google::protobuf::Message and its derived types. std::is_base_of_v<::google::protobuf::Message, T> alone is both necessary and sufficient (it is already true for T = google::protobuf::Message itself, since is_base_of treats a type as its own base). Do not widen it (e.g. by || !std::is_same_v<...>, as used for JsonToMessage, where it is vestigial but harmless): unlike JsonToMessage, this template competes with LogHelper's unconstrained member operator<<, and C++20 partial ordering picks the more constrained candidate without evaluating whether the constraint is actually restrictive — a tautological constraint (true for every T) would still "win", then hijack h << x for values unrelated to protobuf (e.g. plain std::string, as produced by protobuf::json::MessageToDebugString below) and fail to compile on the static_cast in the body.

The static_cast in the body changes the argument's static type to exactly google::protobuf::Message, so on the resulting h << ... call the non-template overload above — preferred by the standard tie-breaking rule over an equally exact-match template — is the one actually selected, instead of recursing back into this template (or, worse, back into the member).

Definition at line 44 of file log.hpp.

◆ operator<<() [3/11]

template<typename TStruct>
requires proto_structs::traits::ProtoStruct<std::remove_cvref_t<TStruct>>
logging::LogHelper & logging::operator<< ( logging::LogHelper & h,
const TStruct & obj )

Logs the protobuf struct obj as a debug string (see logging::operator<< for google::protobuf::Message). The representation uses proto field names (not json_name), is truncated once it grows too large, [debug_redact = true] fields are redacted, and it must not be parsed back or relied upon for stability.

Definition at line 21 of file log.hpp.

◆ operator<<() [4/11]

template<typename T>
LogHelper & logging::operator<< ( LogHelper & lh,
const std::atomic< T > & value )

Definition at line 254 of file log_helper.hpp.

◆ operator<<() [5/11]

template<typename T>
LogHelper & logging::operator<< ( LogHelper & lh,
const std::optional< T > & value )

Definition at line 280 of file log_helper.hpp.

◆ operator<<() [6/11]

template<typename... Types>
LogHelper & logging::operator<< ( LogHelper & lh,
const std::tuple< Types... > & value )

Definition at line 15 of file log_helper_extras.hpp.

◆ operator<<() [7/11]

template<typename T>
LogHelper & logging::operator<< ( LogHelper & lh,
const T * value )
noexcept

Definition at line 259 of file log_helper.hpp.

◆ operator<<() [8/11]

template<typename Fun>
requires std::is_invocable_r_v<void, Fun, LogHelper&>
LogHelper & logging::operator<< ( LogHelper & lh,
Fun && value )

Definition at line 291 of file log_helper.hpp.

◆ operator<<() [9/11]

template<class Result, class... Args>
LogHelper & logging::operator<< ( LogHelper & lh,
Result(*  )(Args...) )

Definition at line 297 of file log_helper.hpp.

◆ operator<<() [10/11]

LogHelper & logging::operator<< ( LogHelper & lh,
std::error_code ec )
inline

Definition at line 248 of file log_helper.hpp.

◆ operator<<() [11/11]

template<typename T>
LogHelper & logging::operator<< ( LogHelper & lh,
T * value )

Definition at line 271 of file log_helper.hpp.

◆ SetDefaultLoggerLevel()

void logging::SetDefaultLoggerLevel ( Level )

Sets new log level for the default logger.

Note
Prefer using logging::DefaultLoggerLevelScope if possible

◆ ShouldLog()

bool logging::ShouldLog ( Level level)
noexcept

Returns true if the provided log level is greater or equal to the current log level and to the tracing::Span (if any) local log level.

Variable Documentation

◆ kLevelMax

auto logging::kLevelMax = static_cast<int>(Level::kNone)
inlineconstexpr

Definition at line 26 of file level.hpp.