A custom sink for the spdlog logging library that sends log messages to Graylog using GELF (Graylog Extended Log Format) over UDP or TCP.
- Sends logs from
spdlog
to Graylog. - Supports both UDP and TCP protocols.
- Customizable log levels and configuration options.
- Uses:
- Asio for network communication.
- nlohmann-json for JSON message serialization.
- spdlog for integration with existing logging infrastructure.
Ensure you have the following dependencies installed:
- A C++17 or later compiler.
- CMake 3.15 or higher.
- vcpkg (optional) for managing dependencies.
-
Clone the repository:
git clone https://github.com/MUN1Z/spdlog-sinks-graylog.git cd spdlog-sinks-graylog
-
Install dependencies using
vcpkg
:vcpkg install asio nlohmann-json spdlog
-
Build the project:
mkdir build && cd build cmake .. -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake make
Manually download and integrate the dependencies into your project:
Update your CMakeLists.txt to include the paths to these libraries.
Here’s an example of how to use the custom Graylog sink with spdlog
:
#include "gray_log_sink.hpp"
int main() {
// Configure Graylog sink options
GrayLogSinkOptions options{
"127.0.0.1", // Graylog host
"MyApp", // Source application name
"info" // Log level
12201, // Port (default for GELF)
};
// Create Graylog sink
auto graylog_sink = std::make_shared<GrayLogSink>(options);
// Create a logger with the custom sink
auto logger = std::make_shared<spdlog::logger>("graylog_logger", graylog_sink);
// Log messages
logger->info("This is an info message sent to Graylog.");
logger->error("This is an error message sent to Graylog.");
}
Field | Description | Example |
---|---|---|
hostNameOrAddress |
Graylog server address or hostname | "127.0.0.1" |
source |
Source name to identify the sender | "MyApp" |
level |
Minimum log level to send to Graylog | info |
port |
Port for GELF communication | 12201 |
By default, the sink uses UDP. To enable TCP, adjust the GrayLogSink
implementation to create a TCP socket.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/my-feature
). - Commit your changes (
git commit -m 'Add my feature'
). - Push to the branch (
git push origin feature/my-feature
). - Open a pull request.
This project is licensed under the MIT License.
- spdlog for the logging framework.
- Graylog for the centralized logging platform.
- nlohmann-json for JSON serialization.
- Asio for networking utilities.
Feel free to reach out if you have any issues or suggestions!