LogIt++
|
Defines the base path used for log file paths. If LOGIT_BASE_PATH
is not defined or is empty ({}), the full path from __FILE__
will be used for log file paths.
LogIt++
is a flexible and versatile C++ logging library that supports various backends and stream-based output. It provides an easy-to-use interface for logging messages with different severity levels and allows customization of log formats and destinations. The library combines the simplicity of macro-based logging similar to IceCream-Cpp and the configurability of logging backends and formats like spdlog.
LogIt++
is fully compatible with C++11
, ensuring support for a wide range of compilers and systems.
Here's a simple example demonstrating how to use LogIt++ in your application:
LogIt++
supports customizable log message formatting using format flags. You can define how each log message should appear by including placeholders for different pieces of information such as the timestamp, log level, file name, function name, and message.
Below is a list of all supported format flags and their meanings:
%Y
: Year (e.g., 2024)%m
: Month (01-12)%d
: Day of the month (01-31)%H
: Hour (00-23)%M
: Minute (00-59)%S
: Second (00-59)%e
: Millisecond (000-999)%C
: Two-digit year (e.g., 24 for 2024)%c
: Full date and time (e.g., Mon Oct 4 12:45:30 2024)%D
: Short date (e.g., 10/04/24)%T
, %X
: Time in ISO 8601 format (e.g., 12:45:30)%F
: Date in ISO 8601 format (e.g., 2024-10-04)%s
, %E
: Unix timestamp in seconds%ms
: Unix timestamp in milliseconds%b
: Abbreviated month name (e.g., Jan)%B
: Full month name (e.g., January)%a
: Abbreviated weekday name (e.g., Mon)%A
: Full weekday name (e.g., Monday)%l
: Full log level (e.g., INFO, ERROR)%L
: Short log level (e.g., I for INFO, E for ERROR)%f
, %fn
, %bs
: Base name of the source file (e.g., main.cpp)%g
, %ffn
: Full file path (e.g., /home/user/project/src/main.cpp)%@
: Source file and line number (e.g., main.cpp:45)%#
: Line number (e.g., 45)%!
: Function name (e.g., main)%t
: Thread identifier%^
: Start color formatting%$
: End color formatting%SC
: Start removing color codes (Strip Color)%EC
: End removing color codes (End Color)%v
: The log message contentTo define a custom format for your log messages, you can use the following method:
Alternatively, you can use the LOGIT_ADD_CONSOLE
macro for an even simpler setup:
This example format will produce log messages like:
[2024-10-04 12:45:30.123] [main.cpp:45] [main] [thread:1400] [INFO] This is a log message
You can adjust the format string by changing or rearranging the format flags according to your needs. For example, if you prefer short log levels and don't need thread information, you could use:
This will produce more compact log messages like:
[2024-10-04 12:45:30.123] [I] This is a log message
You can also include static text and other custom formatting options by mixing text with the flags.
LogIt++
provides shortened versions of logging macros when LOGIT_SHORT_NAME
is defined. These macros allow for concise logging across different log levels, including both standard and stream-based logging.
LOG_T(...)
: Logs a TRACE-level message.LOG_T0()
: Logs a TRACE-level message without arguments.LOG_0T()
: Alias for LOG_T0()
.LOG_0_T()
: Alias for LOG_T0()
.LOG_T_NOARGS()
: Alias for LOG_T0()
.LOG_NOARGS_T()
: Alias for LOG_T0()
.LOG_TF(fmt, ...)
: Logs a formatted TRACE-level message using format strings.LOG_FT(fmt, ...)
: Alias for LOG_TF(fmt, ...)
.LOG_T_PRINT(...)
: Logs a TRACE-level message by printing each argument.LOG_PRINT_T(...)
: Alias for LOG_T_PRINT(...)
.LOG_T_PRINTF(fmt, ...)
: Logs a formatted TRACE-level message using printf-style formatting.LOG_PRINTF_T(fmt, ...)
: Alias for LOG_T_PRINTF(fmt, ...)
.LOG_TP(...)
: Alias for LOG_T_PRINT(...)
.LOG_PT(...)
: Alias for LOG_T_PRINT(...)
.LOG_TPF(fmt, ...)
: Alias for LOG_T_PRINTF(fmt, ...)
.LOG_PFT(fmt, ...)
: Alias for LOG_T_PRINTF(fmt, ...)
.LOG_TRACE(...)
: Logs a TRACE-level message (same as LOG_T(...)
).LOG_TRACE0()
: Logs a TRACE-level message without arguments (same as LOG_T0()
).LOG_0TRACE()
: Alias for LOG_TRACE0()
.LOG_0_TRACE()
: Alias for LOG_TRACE0()
.LOG_TRACE_NOARGS()
: Logs a TRACE-level message with no arguments (same as LOG_T_NOARGS()
).LOG_NOARGS_TRACE()
: Alias for LOG_TRACE_NOARGS()
.LOG_TRACEF(fmt, ...)
: Logs a formatted TRACE-level message (same as LOG_TF(fmt, ...)
).LOG_FTRACE(fmt, ...)
: Alias for LOG_TRACEF(fmt, ...)
.LOG_TRACE_PRINT(...)
: Logs a TRACE-level message by printing each argument (same as LOG_T_PRINT(...)
).LOG_PRINT_TRACE(...)
: Alias for LOG_TRACE_PRINT(...)
.LOG_TRACE_PRINTF(fmt, ...)
: Logs a formatted TRACE-level message using printf-style formatting (same as LOG_T_PRINTF(fmt, ...)
).LOG_PRINTF_TRACE(fmt, ...)
: Alias for LOG_TRACE_PRINTF(fmt, ...)
.These macros provide flexibility and convenience when logging messages at the TRACE level. They allow you to choose between different logging styles, such as standard logging, formatted logging, and printing each argument separately.
Similar macros are available for other log levels — INFO (LOG_I
, LOG_INFO
), DEBUG (LOG_D
, LOG_DEBUG
), WARN (LOG_W
, LOG_WARN
), ERROR (LOG_E
, LOG_ERROR
), and FATAL (LOG_F
, LOG_FATAL
). The naming conventions are consistent across levels, you only need to replace the level letter or word in the macro name.
LogIt++ provides several macros that allow for customization and configuration. Below are the available configuration macros:
Sets the default color for console output. If LOGIT_DEFAULT_COLOR
is not defined, it defaults to TextColor::LightGray
.
Defines the console text color for each log level. By default, each log level is associated with a specific color, but these can be customized.
Available log levels:
TextColor::DarkGray
TextColor::Blue
TextColor::Green
TextColor::Yellow
TextColor::Red
TextColor::Magenta
Macro to get the current timestamp in milliseconds. By default, it uses std::chrono
for time calculation. You can override this to customize the timestamp generation.
Defines the default log pattern for the console logger. If LOGIT_CONSOLE_PATTERN
is not defined, it defaults to %H:%M:%S.%e | %^%v%$
.
Defines the default directory path for log files. If LOGIT_FILE_LOGGER_PATH
is not defined, it defaults to "data/logs".
Defines the number of days after which old log files are deleted. If LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS
is not defined, it defaults to 30
days.
Defines the default log pattern for file-based loggers. If LOGIT_FILE_LOGGER_PATTERN
is not defined, it defaults to [%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %SC%v
.
Defines the default directory path for unique log files. If LOGIT_UNIQUE_FILE_LOGGER_PATH
is not defined, it defaults to "data/logs/unique_logs".
Defines the default log pattern for unique file-based loggers. If LOGIT_UNIQUE_FILE_LOGGER_PATTERN
is not defined, it defaults to v
.
Defines the length of the hash used in the unique log file names. If LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH
is not defined, it defaults to 8
characters.
This macro controls the length of the hash part in the filenames for unique log files, ensuring unique names for each log.
Enables short names for logging macros, such as LOG_T
, LOG_D
, LOG_E
, etc., for concise logging.
Enables the use of the fmt library for string formatting. If defined, the logging system will use fmt for advanced formatting of log messages.
LogIt++ allows you to extend the logging system by creating your own loggers and formatters. This section explains how to implement a custom backend for logging and a custom log formatter.
To create a custom logger backend, you need to implement the ILogger
interface, which requires defining the log()
and wait()
methods. Here's an example of a simple custom logger that logs messages to a text file:
This FileLogger
class writes log messages to a specified file. You can add this logger to your logging system like this:
In addition to creating custom loggers, you can also create custom formatters by implementing the ILogFormatter
interface. Here's an example of a simple formatter that outputs log messages in JSON format:
This JsonLogFormatter
formats log messages as JSON objects. You can combine it with any logger, including the FileLogger
, as shown below:
By implementing your own ILogger
and ILogFormatter
, you can extend the functionality of LogIt++ to log messages to various destinations or in different formats. You can combine custom loggers and formatters to create powerful logging solutions tailored to your application's needs.
Here's a quick summary:
logit::Logger::get_instance().add_logger()
to add your custom logger and formatter to the logging system.LogIt++ is a header-only library, which means it can be easily included in your project without the need for compilation or linking. Below are the steps to integrate it into your project.
First, clone the LogIt++ repository from GitHub along with its submodules. The library has dependencies on other header-only libraries, such as time-shield-cpp (for time utilities) and fmt (for string formatting, if LOGIT_USE_FMT_LIB
is enabled).
To clone the repository with submodules, use the following command:
If you have already cloned the repository without submodules, you can initialize and update the submodules by running the following commands:
Since LogIt++ is a header-only library, you can simply include the main header in your project:
This will give you access to the entire logging system.
LogIt++ depends on time-shield-cpp, which is located in the libs
folder as a submodule. Ensure that the path to libs\time-shield-cpp\include
is added to your project's include directories. If you are using an IDE like Visual Studio or CLion, you can add the include path in the project settings.
LogIt++ supports the fmt library for advanced string formatting, which is also included as a submodule. To enable fmt
in LogIt++, define the macro LOGIT_USE_FMT_LIB
in your project:
This will allow you to use fmt
-style formatting within your log messages. The fmt
library is also located in the libs
folder of the repository.
After adding the necessary include paths, you can proceed to build and run your project. LogIt++ is designed to be easy to integrate and requires no linking since it's header-only.
The LogIt++ library is open-source and hosted on GitHub: LogIt++ GitHub Repository.
This library is licensed under the MIT License. See the LICENSE file in the repository for more details.