Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.
Simon Ninon edited this page Oct 7, 2016 · 7 revisions

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

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

Using your own 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);

Clone this wiki locally