-
Notifications
You must be signed in to change notification settings - Fork 561
Logger
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).
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/logger.hpp.
This variable can be reset to store the instance of your logger.
Please not 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);.
You can create and use your custom logger.
cpp_redis provides an interface logger_iface, defined in includes/logger.hpp:
class logger_iface {
// ...
virtual void debug(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void info(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void warn(const std::string& msg, const std::string& file, unsigned int line) = 0;
virtual void error(const std::string& msg, const std::string& file, unsigned int line) = 0;
// ...
};Your custom logger just has to inherit from that interface and implement the debug, info, warn and error functions.
class my_logger : public cpp_redis::logger_iface {
// ...
void debug(const std::string& msg, const std::string& file, unsigned int line) { ... }
void info(const std::string& msg, const std::string& file, unsigned int line) { ... }
void warn(const std::string& msg, const std::string& file, unsigned int line) { ... }
void error(const std::string& msg, const std::string& file, unsigned int 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.