-
Notifications
You must be signed in to change notification settings - Fork 4.6k
0. FAQ
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(<level>); // flush when the given level or higher message is logged
my_logger->flush_on(spdlog::level::info); // flush when "info" or higher message is logged
spdlog::flush_on(spdlog::level::info); // 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.
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
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.
At the end of main, call spdlog::shutdown();
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).
See How to use spdlog in DLLs for details.
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.
You need enclose the colored part you want with %^
and %$
.
For example spdlog::set_pattern(“%^[%l]%$ %v”);
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"
}
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
- define
NOMINMAX
macro for your project, or - do not use the static library distribution of fmt.