-
Notifications
You must be signed in to change notification settings - Fork 4.5k
2. Creating loggers
Each logger contains a vector of one or more std::shared_ptr<spdlog::sink>
.
On each log call (if the log level is right) the logger will call the sink(log_msg)
function on each of them.
spdlog's sinks have _mt (multi threaded) or _st (single threaded) suffixes to indicate the thread safety. While single threaded sinks cannot be used from multiple threads simultaneously they might be faster because no locking is employed.
The easiest way to create loggers is to use the factory functions in spdlog.h:
//Create and return a shared_ptr to a multithreaded console logger.
auto console = spd::stdout_logger_mt("some_unique_name");
This will create a console logger, register it in spdlog's global registry with "some_unique_name" as its id, and return it as shared_ptr.
Loggers can be accessed from anywhere using the thread safe spdlog::get("logger_name")
which returns a shared pointer.
Note: spdlog::get
might slow your code down since it locks a mutex, so use with caution. Better save somewhere a shared_ptr<spdlog::logger>
and use it directly in hot paths.
A good approach would be to have a private member of type shared_ptr<spdlog::logger>
which will be set on ctor:
class MyClass
{
private:
std::shared_ptr<spdlog::logger> _logger;
public:
MyClass()
{
_logger = spdlog::get("some_logger");
//or
//_logger = spdlog::rotating_file_logger_mt("my_logger", ...);
}
};
Note: Only registered loggers can be accessed using get("..")
but normally there is no need to register loggers as they are registered automatically for you.
//Create rotating file multithreaded logger
auto file_logger = spd::rotating_logger_mt("file_logger", "logs/mylogfile", 1048576 * 5, 3);
..
..
auto same_logger= spdlog::get("file_logger);
Call the spdlog::set_async_mode(size_T queue_size)
to tell spdlog to create async loggers.
The "queue_size" param indicates the number of pre-allocated slots in the message queue and must be power of two
//Create asynchronous, rotating file multi threaded logger
spdlog::set_async_mode(1048576);
auto file_logger = spd::rotating_logger_mt("file_logger", "logs/mylogfile", 1048576 * 5, 3);
auto sink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
auto my_logger= std::make_shared<spdlog::logger>("mylogger", sink);
Please note that manually created loggers(i.e. were not created using the spdlog.h factory methods) do not get registered.
If you want to register such logger use the register_logger(..)
function.
Example:
spdlog::register_logger(my_logger);
...
auto the_same_logger = spdlog::get("mylogger");
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_st>());
sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_st>("logfile", "txt", 23, 59));
auto combined_logger = std::make_shared<spdlog::logger>("name", begin(sinks), end(sinks));
//register it if you need to access it globaly
spdlog::register_logger(combine_logger);