Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 203
cpp_redis provides a flexible way to setup logging.
By default, the library logs nothing. However, it is possible to set a logger of your choice (either provided by the library or a custom one).
First of all, you have to enable logging by defining the appropriate CMake variable.
Please refer to this part of the wiki.
cpp_redis::active_logger is the variable that contains the instance of the current logger.
active_logger is defined as follows: extern std::unique_ptr<logger_iface> active_logger, within includes/cpp_redis/logger.hpp.
This variable can be reset to store the instance of your logger.
Please note that:
- Setting and getting the value of
active_loggeris not thread_safe. Thus, it is preferable to access and modifyactive_loggerbefore usingcpp_redisinside your program. - By default,
active_loggeris set tonullptr, meaning that no logger is used.
cpp_redis provides a default logger: cpp_redis::logger.
You can use it by using this line of code:
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);Default logger supports different log_level to filter what you want to see. By default, the log_level is set to info, but can be changed using the constructor parameter:
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger(cpp_redis::logger::log_level::debug));The following levels are available:
enumclasslog_level {
error = 0,
warn = 1,
info = 2,
debug = 3
};This logger uses a mutex to provide clean output in the multithreaded context of the library. This is really useful for debug and reporting issues, but it might not be what you are looking for if your primary concern is performance.

You can create and use your custom logger.
cpp_redis provides an interface logger_iface, defined in includes/cpp_redis/logger.hpp:
classlogger_iface {
// ...virtualvoiddebug(const std::string& msg, const std::string& file, unsignedint line) = 0;
virtualvoidinfo(const std::string& msg, const std::string& file, unsignedint line) = 0;
virtualvoidwarn(const std::string& msg, const std::string& file, unsignedint line) = 0;
virtualvoiderror(const std::string& msg, const std::string& file, unsignedint line) = 0;
// ...
};Your custom logger just has to inherit from that interface and implement the debug, info, warn and error functions.
classmy_logger : publiccpp_redis::logger_iface {
// ...voiddebug(const std::string& msg, const std::string& file, unsignedint line) { ... }
voidinfo(const std::string& msg, const std::string& file, unsignedint line) { ... }
voidwarn(const std::string& msg, const std::string& file, unsignedint line) { ... }
voiderror(const std::string& msg, const std::string& file, unsignedint line) { ... }
// ...
};Then, just set cpp_redis::active_logger with an instance of your custom logger: your custom logger will automatically be used:
cpp_redis::active_logger = std::unique_ptr<my_logger>(new my_logger);Need more information? Contact me.