The log file remains empty

For performance reasons, log entries are not flushed to files immediately, but only after BUFSIZ bytes have been logged (as defined in libc).

Use any of the following methods to force flush:

my_logger->flush();                            // flush now
my_logger->flush_on(spdlog::level::info);      // auto flush when "info" or higher message is logged
spdlog::flush_on(spdlog::level::info);         // auto flush when "info" or higher message is logged on all loggers
spdlog::flush_every(std::chrono::seconds(5));  // flush periodically every 5 seconds (caution: must be _mt logger)

See the flush policy for details.

How to remove all debug statements at compile time ?

Define SPDLOG_ACTIVE_LEVEL to the desired log level before including spdlog.h and use the SPDLOG_ macros:

#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO // All DEBUG/TRACE statements will be removed by the pre-processor
#include "spdlog/spdlog.h"
...
SPDLOG_DEBUG("debug message to default logger"); // removed at compile time
SPDLOG_LOGGER_TRACE(my_logger, "trace message"); // removed at compile time
SPDLOG_INFO("info message to default logger");   // included

How to use external fmt library instead of the bundled?

To use external fmt library, SPDLOG_FMT_EXTERNAL macro must be defined.

If you build spdlog with CMake, you must define CMake variable SPDLOG_FMT_EXTERNAL or SPDLOG_FMT_EXTERNAL_HO (header-only fmt). SPDLOG_FMT_EXTERNAL macro is automatically defined in spdlog added by CMake.

Asynchronous logging hangs on application exit under Windows

At the end of main, call spdlog::shutdown();

Asynchronous logging hangs under Windows dll

Make sure you don't create the async logger (or the thread pool) inside DllMain (e.g. by using static logger variable inside the dll).

How to use spdlog in shared library?

See How to use spdlog in DLLs for details.

Memory leak detected when using inside a shared library

The async logger and spdlog::flush_every() create a worker thread.

When using these in a shared library, you must call spdlog::shutdown(); before the shared library is unloaded from memory. Otherwise, the objects associated with the thread may not be released.

Colors do not appear when using custom format

You need enclose the colored part you want with %^ and %$.

For example spdlog::set_pattern(“%^[%l]%$ %v”);

Source information do not appear when using custom format

Some custom format patterns, such as %@, %s, %g, %#, %!, print the source information.

For these to work, you must use compile time log level macros.

For example:

#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_sinks.h"

void source_info_example()
{
    auto console = spdlog::stdout_logger_mt("console");
    spdlog::set_default_logger(console);
    spdlog::set_pattern("[source %s] [function %!] [line %#] %v");

    SPDLOG_LOGGER_INFO(console, "log with source info"); // Console: "[source example.cpp] [function source_info_example] [line 10] log with source info"
    SPDLOG_INFO("global log with source info"); // Console: "[source example.cpp] [function source_info_example] [line 11] global logger with source info"

    console->info("source info is not printed"); // Console: "[source ] [function ] [line ] source info is not printed"
}

WinAPI min/max macro definitions

When using the header-only distribution of spdlog together with the bundled fmt library on Windows you end up including Windows.h via the bundled fmt library. To prevent any errors caused by the definiton of min/max macros in WinAPI you can either

  • Surround your min() or max() calls with parentheses: (std::max)(n1, n2) or
  • define NOMINMAX macro before including spdlog.h, or
  • use external fmt library(-DSPDLOG_FMT_EXTERNAL=ON when building spdlog)

See #1553 or fmt#1508 for details.

Switch the default logger to stderr

You can set_default_logger() with a new stderr logger. If you want the logging format to remain unchanged, you should specify the name as an empty string, but this will cause a conflict with the initial default logger, which is already using that name. To resolve this, first replace the default logger with one that has some other arbitrary name…

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
int main()
{
    // Replace the default logger with a (color, single-threaded) stderr logger
    // (but first replace it with an arbitrarily-named logger to prevent a name clash)
    spdlog::set_default_logger( spdlog::stderr_color_st( "some_arbitrary_name" ) );
    spdlog::set_default_logger( spdlog::stderr_color_st( "" ) );
    spdlog::info("This message will go to stderr");
}





Add Discussion

Log in to comment