This project is a customizable, thread-safe logging library in C++. The logger supports multiple log destinations such as console, file, and more. Each Logger instance can be configured to log messages to specific destinations, and the library is designed for use in a multithreaded environment.
- Multiple Log Destinations: Log messages to various destinations.
- Thread-Safe: Safe to use in multithreaded applications.
- Customizable: Easily extendable to add new logging destinations.
- Log levels: Logger has several levels of logging, such as:
INFO,DEBUG,WARNINGandERROR. - Customizable Prefix: Each Logger instance could have its own log prefix, that will be included into the each logline that this instance logs.
- C++11 or later: Ensure that your environment supports at least C++11.
Clone this repository to your local machine:
git clone https://github.com/i-gap4uk/multithreading_logger.gitcd multithreading_logger
mkdir build && cd build
cmake ..
cmake --build .You can create a Logger instance via several ways:
- Without Prefix:
auto logger = getLogger();In this case, this instance of Logger will have no prefix in its loglines. Example:
01/01/1970 00:00:00; INFO; (128581333313344): Starting the app
Breakdown:
01/01/1970 00:00:00- datetime stamp,INFO- log level (theINFOis default),(128581333313344)- a thread id, in which the instance is running,Starting the app- log itself.
- Another Instance without Prefix:
Logger logger2;Similar to the first case, this instance will have no prefix in its log lines.
- With a Custom Prefix:
Logger logger3("instance3");This instance of Logger will have prefix instance3 in its loglines. Example:
01/01/1970 00:00:00; INFO; instance3(128581333313344): Starting the appBy default, the Logger logs into the console. There is also a predefined log destination for logging to a file (with a predefined non-configurable file path).
You can add your own custom LogPrinter via the add_log_printer method:
logger.add_log_printer(std::make_shared<CustomLogPrinter>());The Logger is designed to be thread-safe. Each Logger instance manages its own message queue, and logging operations are synchronized internally. The static collection of printers is protected by a mutex to prevent race conditions when adding or removing printers dynamically.
To build the logger as a static library, follow these steps:
- Clone the repository:
git clone git@github.com:i-gap4uk/Logger.git
cd Logger- Build the project: By default, the project builds the logger as a static library.
mkdir build
cd build
cmake ..
makeThis will create a static library file, typically liblogger.a on Linux, inside the build directory.
You can build and run the examples provided in the logger repository by enabling the BUILD_EXAMPLES flag when configuring the project:
cmake -DBUILD_EXAMPLES=ON ..
make