MySQL 8.0.31
Source Code Documentation
mysql_harness::logging Namespace Reference

Classes

class  FileHandler
 Handler that writes to a file. More...
 
class  Handler
 Base class for log message handler. More...
 
class  Logger
 Logger class. More...
 
class  NullHandler
 Handler to write to a null device such as /dev/null (unix) or NUL (windows). More...
 
struct  Record
 Log record containing information collected by the logging system. More...
 
class  Registry
 
class  StreamHandler
 Handler to write to an output stream. More...
 

Enumerations

enum class  LogLevel {
  kFatal , kSystem , kError , kWarning ,
  kInfo , kNote , kDebug , kNotSet
}
 Log level values. More...
 
enum class  LogTimestampPrecision {
  kSec = 0 , kMilliSec = 3 , kMicroSec = 6 , kNanoSec = 9 ,
  kNotSet
}
 Log timestamp precision values. More...
 

Functions

HARNESS_EXPORT bool log_level_is_handled (LogLevel level, const char *domain)
 
static bool log_level_is_handled (LogLevel level)
 
HARNESS_EXPORT LogLevel log_level_from_string (std::string name)
 Converts string with log level description to LogLevel type. More...
 
HARNESS_EXPORT LogLevel get_default_log_level (const Config &config, bool raw_mode=false)
 Get default log level. More...
 
HARNESS_EXPORT std::string get_default_log_filename (const Config &config)
 Get default log filename. More...
 
HARNESS_EXPORT void attach_handler_to_all_loggers (Registry &registry, std::string name)
 Attach handler to all loggers. More...
 
HARNESS_EXPORT void set_log_level_for_all_loggers (Registry &registry, LogLevel level)
 Set log levels for all the loggers to specified value. More...
 
HARNESS_EXPORT void set_log_level_for_all_handlers (const Registry &registry, LogLevel level)
 Set log levels for all handlers to specified value. More...
 
HARNESS_EXPORT LogTimestampPrecision log_timestamp_precision_from_string (std::string name)
 Converts string with log timestamp precision description to LogTimestampPrecision type. More...
 
HARNESS_EXPORT LogTimestampPrecision get_default_timestamp_precision (const Config &config)
 Get default timestamp precision. More...
 
HARNESS_EXPORT void set_timestamp_precision_for_all_loggers (Registry &registry, LogTimestampPrecision precision)
 Set timestamp precision for all the loggers. More...
 
HARNESS_EXPORT void clear_registry (Registry &registry)
 Clear registry. More...
 
HARNESS_EXPORT void create_module_loggers (Registry &registry, const LogLevel level, const std::list< std::string > &modules, const std::string &main_app_log_domain)
 Initialize logging facility. More...
 
HARNESS_EXPORT void create_logger (Registry &registry, const LogLevel level, const std::string &logger_name)
 
HARNESS_EXPORT void create_main_log_handler (Registry &registry, const std::string &program, const std::string &logging_folder, bool format_messages, bool use_os_log=false)
 Initialize logfile handler. More...
 
HARNESS_EXPORT void set_log_level_for_all_loggers (LogLevel level)
 Set log level for all registered loggers. More...
 
HARNESS_EXPORT void set_log_level_for_all_handlers (LogLevel level)
 Set log level for all registered handlers. More...
 
HARNESS_EXPORT void set_timestamp_precison_for_all_loggers (LogTimestampPrecision precision)
 Set timestamp precision for all registered loggers. More...
 
HARNESS_EXPORT void register_handler (std::string name, std::shared_ptr< Handler > handler)
 Register handler for all plugins. More...
 
HARNESS_EXPORT void unregister_handler (std::string name)
 Unregister a handler. More...
 
HARNESS_EXPORT std::ostream * get_default_logger_stream ()
 Returns pointer to the default logger sink stream. More...
 
void set_timestamp_precision_for_all_loggers (LogTimestampPrecision precision)
 
static void log_system (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void log_error (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void static void log_warning (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void static void static void log_info (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void static void static void static void log_note (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void static void static void static void static void log_debug (const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 
static void static void static void static void static void static void static void log_custom (const LogLevel log_level, const char *fmt,...) ATTRIBUTE_GCC_FORMAT(printf
 

Variables

const size_t kLogMessageMaxSize = 4096
 Max message length that can be logged; if message is longer, it will be truncated to this length. More...
 
constexpr char kConfigOptionLogFilename [] = "filename"
 Section name and option name used in config file (and later in configuration object) to specify log level, best explained by example: More...
 
constexpr char kConfigOptionLogDestination [] = "destination"
 
constexpr char kConfigOptionLogLevel [] = "level"
 
constexpr char kConfigOptionLogTimestampPrecision [] = "timestamp_precision"
 
constexpr char kConfigSectionLogger [] = "logger"
 
constexpr char kNone [] = ""
 
constexpr char kMainLogger [] = "main"
 Special names reserved for "main" program logger. More...
 
constexpr char kMainLogHandler [] = "main_log_handler"
 
constexpr char kMainConsoleHandler [] = "main_console_handler"
 
constexpr char kSqlLogger [] = "sql"
 
constexpr char kDefaultLogFilename [] = "mysqlrouter.log"
 Default log filename. More...
 
const LogLevel kDefaultLogLevel = LogLevel::kWarning
 Default log level used by the router. More...
 
const char *const kDefaultLogLevelName = "warning"
 Log level name for the default log level used by the router. More...
 
const char *const kRawLogLevelName = "info"
 Log level name used in raw logging mode. More...
 
static std::string g_main_app_log_domain
 

Enumeration Type Documentation

◆ LogLevel

Log level values.

Log levels are ordered numerically from most important (lowest value) to least important (highest value).

Enumerator
kFatal 

Fatal failure.

Router usually exits after logging this.

kSystem 

System message.

These messages are always logged, such as state changes during startup and shutdown.

kError 

Error message.

indicate that something is not working properly and actions need to be taken. However, the router continue operating but the particular thread issuing the error message might terminate.

kWarning 

Warning message.

Indicate a potential problem that could require actions, but does not cause a problem for the continuous operation of the router.

kInfo 

Informational message.

Information that can be useful to check the behaviour of the router during normal operation.

kNote 

Note level contains additional information over the normal informational messages.

kDebug 

Debug message.

Message contain internal details that can be useful for debugging problematic situations, especially regarding the router itself.

kNotSet 

◆ LogTimestampPrecision

Log timestamp precision values.

Enumerator
kSec 
kMilliSec 
kMicroSec 
kNanoSec 
kNotSet 

Function Documentation

◆ attach_handler_to_all_loggers()

void mysql_harness::logging::attach_handler_to_all_loggers ( Registry registry,
std::string  name 
)

Attach handler to all loggers.

Parameters
registryRegistry object, typically managed by DIM
nameLogger id (log domain it services)

◆ clear_registry()

void mysql_harness::logging::clear_registry ( Registry registry)

Clear registry.

Removes all Loggers and removes all references to Handlers (they're held as shared pointers, which may mean they will also be deleted)

Parameters
registryRegistry object, typically managed by DIM

◆ create_logger()

void mysql_harness::logging::create_logger ( Registry registry,
const LogLevel  level,
const std::string &  logger_name 
)

◆ create_main_log_handler()

void mysql_harness::logging::create_main_log_handler ( Registry registry,
const std::string &  program,
const std::string &  logging_folder,
bool  format_messages,
bool  use_os_log = false 
)

Initialize logfile handler.

Initializes handler which will handle application's log. This handler will be attached to all currently-registered loggers. If logging_folder is provided, handler will log messages to logfile; its path and filename will be derived from program and logging_folder parameters. If logging_folder is empty, handler will log messages to console, unless use_os_log is set to true, in which case it will log to system logger instead (i.e. Syslog, Windows Eventlog, etc. Currently, only Windows Eventlog is supported).

Parameters
registryRegistry object, typically managed by DIM
programName of the main program (Router)
logging_folderlogging_folder provided in configuration file
format_messagesIf set to true, log messages will be formatted (prefixed with log level, timestamp, etc) before logging
use_os_logIf true, use system logger instead of STDERR (currently, only Windows Eventlog is supported)
Exceptions
std::runtime_errorif opening log file or OS log fails

◆ create_module_loggers()

void mysql_harness::logging::create_module_loggers ( Registry registry,
const LogLevel  level,
const std::list< std::string > &  modules,
const std::string &  main_app_log_domain 
)

Initialize logging facility.

Initializes logging facility by creating and registering a logger for each given module. Loggers will have their log level set to the log level passed as a parameter.

Note
Loggers will not have any handlers attached, this needs to be done separately (see create_main_log_handler())
Parameters
registryRegistry object, typically managed by DIM
levelThe log level of the logger
modulesList of plugin names loaded
main_app_log_domainLog domain (logger id) to be used as the main program logger. This logger must exist, because log_*() functions might fail
Exceptions
std::logic_error

◆ get_default_log_filename()

std::string mysql_harness::logging::get_default_log_filename ( const Config config)

Get default log filename.

Fetches default log filename set in the configuration file

Parameters
configConfiguration items from configuration file

◆ get_default_log_level()

LogLevel mysql_harness::logging::get_default_log_level ( const Config config,
bool  raw_mode = false 
)

Get default log level.

Fetches default log level set in the configuration file

Parameters
configConfiguration items from configuration file
raw_modetrue if the default level should be for the raw mode, false otherwise
Exceptions
std::invalid_argumentif [logger].level in configuration is invalid

◆ get_default_logger_stream()

std::ostream * mysql_harness::logging::get_default_logger_stream ( )

Returns pointer to the default logger sink stream.

◆ get_default_timestamp_precision()

LogTimestampPrecision mysql_harness::logging::get_default_timestamp_precision ( const Config config)

Get default timestamp precision.

Fetches default timestamp precision for logfiles

Parameters
configConfiguration items from configuration file

◆ log_custom()

static void mysql_harness::logging::log_custom ( const LogLevel  log_level,
const char *  fmt,
  ... 
)
inlinestatic

◆ log_debug()

static void mysql_harness::logging::log_debug ( const char *  fmt,
  ... 
)
inlinestatic

◆ log_error()

static void mysql_harness::logging::log_error ( const char *  fmt,
  ... 
)
inlinestatic

◆ log_info()

static void mysql_harness::logging::log_info ( const char *  fmt,
  ... 
)
inlinestatic

◆ log_level_from_string()

HARNESS_EXPORT LogLevel mysql_harness::logging::log_level_from_string ( std::string  name)

Converts string with log level description to LogLevel type.

Parameters
namestring with log level description
Exceptions
std::invalid_argumentif log level string is invalid

◆ log_level_is_handled() [1/2]

static bool mysql_harness::logging::log_level_is_handled ( LogLevel  level)
inlinestatic

◆ log_level_is_handled() [2/2]

bool mysql_harness::logging::log_level_is_handled ( LogLevel  level,
const char *  domain 
)

◆ log_note()

static void mysql_harness::logging::log_note ( const char *  fmt,
  ... 
)
inlinestatic

◆ log_system()

static void static void static void static void static void static void static void static void mysql_harness::logging::log_system ( const char *  fmt,
  ... 
)
inlinestatic

◆ log_timestamp_precision_from_string()

HARNESS_EXPORT LogTimestampPrecision mysql_harness::logging::log_timestamp_precision_from_string ( std::string  name)

Converts string with log timestamp precision description to LogTimestampPrecision type.

Parameters
namestring with log timestamp precision description
Exceptions
std::invalid_argumentif log timestamp precision string is invalid

◆ log_warning()

static void mysql_harness::logging::log_warning ( const char *  fmt,
  ... 
)
inlinestatic

◆ register_handler()

void mysql_harness::logging::register_handler ( std::string  name,
std::shared_ptr< Handler handler 
)

Register handler for all plugins.

This will register a handler for all plugins that have been registered with the logging subsystem (normally all plugins that have been loaded by Loader).

Parameters
nameThe name under which handler is registered
handlerShared pointer to dynamically allocated handler.

For example, to register a custom handler from a plugin, you would do the following:

void init() {
...
register_handler(std::make_shared<MyHandler>(...));
...
}
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
HARNESS_EXPORT void register_handler(std::string name, std::shared_ptr< Handler > handler)
Register handler for all plugins.
Definition: registry.cc:453

◆ set_log_level_for_all_handlers() [1/2]

void mysql_harness::logging::set_log_level_for_all_handlers ( const Registry registry,
LogLevel  level 
)

Set log levels for all handlers to specified value.

Parameters
registryRegistry object, typically managed by DIM
levelLog level for logger

◆ set_log_level_for_all_handlers() [2/2]

void mysql_harness::logging::set_log_level_for_all_handlers ( LogLevel  level)

Set log level for all registered handlers.

◆ set_log_level_for_all_loggers() [1/2]

void mysql_harness::logging::set_log_level_for_all_loggers ( LogLevel  level)

Set log level for all registered loggers.

◆ set_log_level_for_all_loggers() [2/2]

void mysql_harness::logging::set_log_level_for_all_loggers ( Registry registry,
LogLevel  level 
)

Set log levels for all the loggers to specified value.

Parameters
registryRegistry object, typically managed by DIM
levelLog level for logger

◆ set_timestamp_precision_for_all_loggers() [1/2]

void mysql_harness::logging::set_timestamp_precision_for_all_loggers ( LogTimestampPrecision  precision)

◆ set_timestamp_precision_for_all_loggers() [2/2]

void mysql_harness::logging::set_timestamp_precision_for_all_loggers ( Registry registry,
LogTimestampPrecision  precision 
)

Set timestamp precision for all the loggers.

Parameters
registryRegistry object, typically managed by DIM
precisionPrecision of timestamps

◆ set_timestamp_precison_for_all_loggers()

HARNESS_EXPORT void mysql_harness::logging::set_timestamp_precison_for_all_loggers ( LogTimestampPrecision  precision)

Set timestamp precision for all registered loggers.

◆ unregister_handler()

void mysql_harness::logging::unregister_handler ( std::string  name)

Unregister a handler.

This will unregister a previously registered handler.

Parameters
namename of registered handler.

Variable Documentation

◆ g_main_app_log_domain

std::string mysql_harness::logging::g_main_app_log_domain
static

◆ kConfigOptionLogDestination

constexpr char mysql_harness::logging::kConfigOptionLogDestination[] = "destination"
constexpr

◆ kConfigOptionLogFilename

constexpr char mysql_harness::logging::kConfigOptionLogFilename[] = "filename"
constexpr

Section name and option name used in config file (and later in configuration object) to specify log level, best explained by example:

v--------------------— kConfigSectionLogger [logger] v---------------------— kConfigOptionLogLevel level = DEBUG v---------------------— kConfigOptionLogFilename filename = foo.log v---------------------— kConfigOptionLogTimestampPrecision timestamp_precision = second|sec|s|millisecond|msec|ms| microsecond|usec|us|nanosecond|nsec|ns

◆ kConfigOptionLogLevel

constexpr char mysql_harness::logging::kConfigOptionLogLevel[] = "level"
constexpr

◆ kConfigOptionLogTimestampPrecision

constexpr char mysql_harness::logging::kConfigOptionLogTimestampPrecision[] = "timestamp_precision"
constexpr

◆ kConfigSectionLogger

constexpr char mysql_harness::logging::kConfigSectionLogger[] = "logger"
constexpr

◆ kDefaultLogFilename

constexpr char mysql_harness::logging::kDefaultLogFilename[] = "mysqlrouter.log"
constexpr

Default log filename.

◆ kDefaultLogLevel

const LogLevel mysql_harness::logging::kDefaultLogLevel = LogLevel::kWarning

Default log level used by the router.

◆ kDefaultLogLevelName

const char* const mysql_harness::logging::kDefaultLogLevelName = "warning"

Log level name for the default log level used by the router.

◆ kLogMessageMaxSize

const size_t mysql_harness::logging::kLogMessageMaxSize = 4096

Max message length that can be logged; if message is longer, it will be truncated to this length.

◆ kMainConsoleHandler

constexpr char mysql_harness::logging::kMainConsoleHandler[] = "main_console_handler"
constexpr

◆ kMainLogger

constexpr char mysql_harness::logging::kMainLogger[] = "main"
constexpr

Special names reserved for "main" program logger.

It will use one of the two handlers, depending on whether logging_folder is empty or not.

◆ kMainLogHandler

constexpr char mysql_harness::logging::kMainLogHandler[] = "main_log_handler"
constexpr

◆ kNone

constexpr char mysql_harness::logging::kNone[] = ""
constexpr

◆ kRawLogLevelName

const char* const mysql_harness::logging::kRawLogLevelName = "info"

Log level name used in raw logging mode.

◆ kSqlLogger

constexpr char mysql_harness::logging::kSqlLogger[] = "sql"
constexpr