SLFMT is a logging library that uses fmt to format log messages. It is heavily inspired by log4j, being an easy-to-use, header-only library that can be used in any C++ project.
- Class level logging.
- Log levels: trace, debug, info, warn, error, fatal.
- Logger messages are formatted using fmt (see full list of features here).
- Logger messages contain the class from which the message was logged.
- You can modify the log message format by modifying the
SLFMT_LOG_FORMATmacro. - Logs are written to the console or to a file (continuous or rolling).
There are several ways to use this library in your project.
Download the repository in your project and add the include directory to your include directories.
Then, insert the add_subdirectory command to add the library to your project.
add_subdirectory(path/to/slfmt)Finally, link the library to your target:
target_link_libraries(your_target PRIVATE slfmt)If you use CPM.cmake in your project, you can add the library
as a dependency in your CMakeLists.txt file with the following command:
CPMAddPackage("gh:scastd/slfmt#main")The link step is the same as the one described in the previous section.
target_link_libraries(your_target PRIVATE slfmt)There are two ways to use the library: explicitly creating the loggers or using macros to create them automatically.
This is the verbose way to create a logger; by using the slfmt::LogManager::GetLogger method to
declare a static field in the class.
static inline const auto logger = slfmt::LogManager::GetLogger("Class");The GetLogger method takes the name of the class as a parameter and returns a std::unique_ptr to a console logger.
If you want to create a file logger, you can use the GetFileLogger method instead.
static inline const auto logger = slfmt::LogManager::GetFileLogger("Class", "path/to/file.log");This is the recommended way to create a logger because it automatically generates the static field depending on the logger type you want. There are several macros you can use:
SLFMT_CONSOLE_LOGGER: Creates a console logger.SLFMT_FILE_LOGGER: Creates a file logger.SLFMT_FILE_LOGGER_NAME: Creates a file logger with the specified name.SLFMT_ROLLING_FILE_LOGGER_NAME: Creates a rolling file logger with the specified name and size.SLFMT_CONSOLE_LOGGER_FIELD: Creates a static field for a console logger.SLFMT_FILE_LOGGER_FIELD: Creates a static field for a file logger.SLFMT_FILE_LOGGER_NAME_FIELD: Creates a static field for a file logger with the specified name.SLFMT_ROLLING_FILE_LOGGER_NAME_FIELD: Creates a static field for a rolling file logger with the specified name and size.SLFMT_COMBINED_LOGGER_FIELD: Creates a static field for a combined logger. You pass as parameters the loggers you want to combine.SLFMT_FILE_CONSOLE_COMBINED_LOGGER_FIELDS: Creates a static field for a combined logger composed of a file logger and a console logger.
Any logger can be used to log messages of different levels (each one uses a different color in the console):
Important
The colors are only available on Linux and macOS. On Windows, the colors are not displayed.
logger->Trace("This is a trace message"); // white
logger->Debug("This is a debug message"); // magenta
logger->Info("This is an info message"); // blue
logger->Warn("This is a warn message"); // yellow
logger->Error("This is an error message"); // red
logger->Fatal("This is a fatal message"); // red | bold | underlineOr you can call the Log method directly and specify the log level:
logger->Log(slfmt::LogLevel::Trace, "This is a trace message"); // white
logger->Log(slfmt::LogLevel::Debug, "This is a debug message"); // magenta
logger->Log(slfmt::LogLevel::Info, "This is an info message"); // blue
logger->Log(slfmt::LogLevel::Warn, "This is a warn message"); // yellow
logger->Log(slfmt::LogLevel::Error, "This is an error message"); // red
logger->Log(slfmt::LogLevel::Fatal, "This is a fatal message"); // red | bold | underlineThe default log format is:
Timestamp Level (Class) [ThreadID] Message
You can customize the log format by modifying the SLFMT_LOG_FORMAT macro.
This project is licensed under the MIT License: see the LICENSE file for details.
- Add support for logging to file.
- Rotating log files.
- Log file size limit.
- Log file path / name.
- Make a builder for log format.
- Add support for custom log levels.
- Add support for custom log level colors.