- Notifications
You must be signed in to change notification settings - Fork209
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 tothis 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
, withinincludes/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_logger
is not thread_safe. Thus, it is preferable to access and modifyactive_logger
before usingcpp_redis
inside your program. - By default,
active_logger
is 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 toinfo
, 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 interfacelogger_iface
, defined inincludes/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 thedebug
,info
,warn
anderror
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 setcpp_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.