Logging

connection::async_run and co_connection::run are complex algorithms, with features like built-in reconnection. This can make configuration problems, like a misconfigured hostname, difficult to debug - Boost.Redis will keep retrying to connect to the same hostname over and over. For this reason, Boost.Redis incorporates a lightweight logging solution, and will log some status messages to stderr by default.

Logging can be customized by passing a logger object to the connection’s constructor. For example, logging can be disabled by writing:

  • Asio

  • Corosio

asio::io_context ioc;
connection conn{ioc, logger{logger::level::disabled}};
corosio::io_context ctx;
co_connection conn{ctx, logger{logger::level::disabled}};

Every message logged by the library is attached a syslog-like severity tag (a logger::level). You can filter messages by severity by creating a logger with a specific level:

  • Asio

  • Corosio

asio::io_context ioc;

// Logs to stderr messages with severity >= level::error.
// This will hide all informational output.
connection conn{ioc, logger{logger::level::error}};
corosio::io_context ctx;

// Logs to stderr messages with severity >= level::error.
// This will hide all informational output.
co_connection conn{ctx, logger{logger::level::error}};

The logger constructor accepts a std::function<void(logger::level, std::string_view)> as second argument. If supplied, Boost.Redis will call this function when logging instead of printing to stderr. This can be used to integrate third-party logging libraries. See our spdlog integration examples for sample code: cpp17_spdlog.cpp (Asio) / corosio_spdlog.cpp (Corosio).