Skip to content
Isaac Rowe edited this page Jun 8, 2018 · 51 revisions

By default spdlog lets the underlying libc flush whenever it sees fit in order to achieve good performance. You can override this using the following options:

Manual flush

You can use the logger->flush() function to instruct a logger to flush its contents. The logger will in turn call the flush() function on each of the underlying sinks.

Important: logger->flush() is always a synchronous operation, even if the logger is in async mode! In fact, in async mode it might take a long time since spdlog will first wait for the queue to drain before flushing.

Severity based flush

You can set the minimum log level that will trigger automatic flush.

For example, this will trigger flush whenever errors or more severe messages are logged:

my_logger->flush_on(spdlog::level::err); 

Interval based flush (async mode only)

In asynchronous logging mode only, spdlog supports setting the flush interval in milliseconds.

Set the flush_interval_ms in the set_async_mode(..) call, to turn on periodic flush.

For example, turn on periodic flush with interval of 2 seconds:

spdlog::set_async_mode(q_size, spdlog::async_overflow_policy::block_retry,
                       nullptr,
                       std::chrono::seconds(2));

Note flush interval just periodically flushes to disk rather than waiting for the queue to become empty.