Skip to content
space55 edited this page Jan 25, 2020 · 2 revisions

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).

Enable Logging

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

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_logger is not thread_safe. Thus, it is preferable to access and modify active_logger before using cpp_redis inside your program.
  • By default, active_logger is set to nullptr, meaning that no logger is used.

Using the provided logger

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:

  enum class log_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.

Using your own logger

You can create and use your custom logger.

cpp_redis provides an interface logger_iface, defined in includes/cpp_redis/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);