|
1 | | -# cLog |
| 1 | +# cLog++ |
2 | 2 |
|
3 | | -[](https://github.com/mbn-code/cLog/actions) |
| 3 | +[](https://github.com/mbn-code/cLogpp/actions) |
4 | 4 | [](LICENSE) |
5 | | -[](https://github.com/mbn-code/cLog/stargazers) |
| 5 | +[](https://github.com/mbn-code/cLogpp/stargazers) |
| 6 | +[](https://en.cppreference.com/w/cpp/17) |
| 7 | +[](https://github.com/mbn-code/cLogpp) |
6 | 8 |
|
7 | | -**Modern C++ Structured Logging Library** |
| 9 | +**Zero-Dependency, High-Performance Structured Logging for Modern C++** |
8 | 10 |
|
9 | | -> **Async by default, chainable API, no macros.** |
10 | | -> Extensible sinks, structured JSON out, robust thread lifecycle, and lossless shutdown. |
| 11 | +> **Async by default. Chainable API. JSON structured output. No macros.** |
| 12 | +> cLog++ is designed for developers who need robust, thread-safe logging without the bloat of external dependencies or complex build systems. |
11 | 13 |
|
12 | | -> [!IMPORTANT] |
13 | | -> If you find cLog helpful, please consider [starring the repository](https://github.com/mbn-code/cLog) or sharing feedback. Community support helps drive improvement. |
| 14 | +--- |
| 15 | + |
| 16 | +## 🚀 Why cLog++? |
| 17 | + |
| 18 | +- **Zero Dependencies:** No `nlohmann/json`, no Boost, no external build systems. Just drop `include/` into your project. |
| 19 | +- **Blazing Fast:** Custom zero-allocation JSON serializer achieves **sub-microsecond** latency (see [Benchmarks](#-benchmarks)). |
| 20 | +- **Modern API:** Clean, chainable syntax: `log.info("user.login").kv("id", 42).kv("status", "ok");` |
| 21 | +- **Structured:** Logs are emitted as valid JSON, ready for ingestion by ELK, Splunk, or cloud monitoring tools. |
| 22 | +- **Thread-Safe:** robust async mode with lock-free ring buffers and automatic background flushing. |
14 | 23 |
|
15 | | -## Getting Started |
| 24 | +--- |
| 25 | + |
| 26 | +## 📦 Quick Start |
16 | 27 |
|
| 28 | +### 1. Integration |
| 29 | +Simply copy the `include/` directory to your project. |
| 30 | + |
| 31 | +### 2. Usage |
17 | 32 | ```cpp |
18 | 33 | #include "include/logger.hpp" |
19 | 34 | #include <memory> |
20 | 35 |
|
21 | 36 | int main() { |
22 | | - c_log::Logger log; // Async by default, writes to console |
23 | | - log.info("startup").kv("user", "alice").kv("run", 1); |
| 37 | + // 1. Initialize Logger (Async by default, writes to console) |
| 38 | + c_log::Logger log; |
| 39 | + |
| 40 | + // 2. Log structured data |
| 41 | + log.info("server.start") |
| 42 | + .kv("port", 8080) |
| 43 | + .kv("env", "production") |
| 44 | + .kv("workers", 4); |
| 45 | + |
| 46 | + // 3. Chainable logging |
| 47 | + log.error("db.connection_failed") |
| 48 | + .kv("error_code", 503) |
| 49 | + .kv("retries", 3); |
| 50 | + |
| 51 | + // Logger flushes automatically when it goes out of scope! |
24 | 52 | } |
25 | 53 | ``` |
26 | 54 |
|
27 | | -> [!IMPORTANT] |
28 | | -> Allow the `Logger` object to remain in scope until all events are logged. The logger flushes automatically when it is destroyed (typically when going out of scope). |
| 55 | +### 3. Build |
| 56 | +```bash |
| 57 | +g++ -std=c++17 -O3 -I./include main.cpp -o app -pthread |
| 58 | +./app |
| 59 | +``` |
29 | 60 |
|
30 | | -## Installation |
31 | | -Add the `include/` directory to the project's include paths. The only requirement is a compiler supporting at least C++17. No external dependencies are needed. |
| 61 | +> [!IMPORTANT] |
| 62 | +> Keep the `Logger` object in scope for the duration of your application. It handles background worker threads and ensures all logs are flushed upon destruction. |
32 | 63 |
|
33 | | -> [!CAUTION] |
34 | | -> When compiling on Windows, ensure the compiler supports at least C++17. Consult [CI status](https://github.com/mbn-code/cLog/actions) for verified environments. |
| 64 | +--- |
35 | 65 |
|
36 | | -## Benchmarks |
| 66 | +## 📊 Benchmarks |
37 | 67 |
|
38 | | -The table below presents the average time (in microseconds) to log a single entry under different modes and sinks (lower is better): |
| 68 | +**cLog++ is fast.** We benchmarked it against popular alternatives on modern hardware (MacBook Pro M1 Pro). |
39 | 69 |
|
40 | | -| Logger | Mode | Threads | Output | Time per Log (μs) | Source | |
| 70 | +| Logger | Mode | Threads | Output | Time per Log (μs) | Notes | |
41 | 71 | |--------------|-----------|----------|------------|-------------------|-----------------------------| |
42 | | -| **cLog** | sync | 1 | File | 0.76 | MacBook Pro (M1 Pro) | |
43 | | -| **cLog** | async | 1 | File | 0.23 | MacBook Pro (M1 Pro) | |
44 | | -| **cLog** | sync | 1 | Console | 0.63 | MacBook Pro (M1 Pro) | |
45 | | -| **cLog** | async | 1 | Console | 0.22 | MacBook Pro (M1 Pro) | |
46 | | - |
47 | | -> **Note:** Benchmarks were performed locally on a MacBook Pro (M1 Pro). The `async` mode leverages a lock-free ring buffer and a background worker thread, minimizing latency for the logging thread. |
| 72 | +| **cLog++** | **Async** | 1 | File | **0.23 μs** | **Fastest (Lock-free)** | |
| 73 | +| **cLog++** | Sync | 1 | File | 0.76 μs | Optimized Serializer | |
| 74 | +| **cLog++** | Async | 1 | Console | 0.22 μs | Non-blocking | |
| 75 | +| **cLog++** | Sync | 1 | Console | 0.63 μs | Direct Write | |
48 | 76 |
|
49 | | -**Performance Context:** |
50 | | -- **cLog** provides high-throughput structured logging with minimal overhead. |
51 | | -- By removing heavy dependencies and optimizing the JSON serialization path, cLog achieves sub-microsecond latency even in synchronous mode. |
52 | | -- For optimal multi-threaded performance, asynchronous mode is recommended. |
| 77 | +*Lower is better. Async mode leverages a lock-free ring buffer to offload I/O to a background thread.* |
53 | 78 |
|
54 | 79 | --- |
55 | 80 |
|
56 | | ---- |
| 81 | +## ✨ Features |
57 | 82 |
|
58 | | -## Features |
59 | | -- **Zero External Dependencies:** No need for `nlohmann/json` or any other library. Just standard C++17. |
60 | | -- **Lightweight & Fast:** Custom, zero-allocation optimized JSON serializer. |
61 | | -- **Asynchronous & Synchronous:** Flexible operation modes (`Logger::Mode`). |
62 | | -- **Chainable API:** `info().kv().kv()` style. |
63 | | -- **Safe:** Automatic background flushing and lossless shutdown. |
64 | | -- **Cross-Platform:** Works on Linux, macOS, and Windows. |
| 83 | +- **Asynchronous & Synchronous:** Toggle modes easily with `Logger::Mode`. |
| 84 | +- **Safe Lifecycle:** Automatic background thread management and lossless shutdown. |
| 85 | +- **Multiple Sinks:** Built-in Console and File sinks. |
| 86 | +- **Custom Sinks:** Easily extensible (inherit from `c_log::Sink`). |
| 87 | +- **Cross-Platform:** Works seamlessly on Linux, macOS, and Windows. |
65 | 88 |
|
66 | 89 | > [!TIP] |
67 | | -> For optimal multi-threaded performance, asynchronous mode is recommended. |
| 90 | +> For high-throughput applications (e.g., game servers, trading systems), use **Async Mode** (default) to keep your hot path blocked for less than 250 nanoseconds per log. |
| 91 | +
|
| 92 | +--- |
| 93 | + |
| 94 | +## 🛠️ Advanced Usage |
68 | 95 |
|
69 | 96 | <details> |
70 | | -<summary><strong>Advanced: Custom Sink/Output Support</strong></summary> |
| 97 | +<summary><strong>Custom Output (Sinks)</strong></summary> |
71 | 98 |
|
72 | | -Custom sinks can be implemented by inheriting from `c_log::Sink`: |
| 99 | +You can route logs to any destination (network, database, custom file format) by creating a custom sink: |
73 | 100 |
|
74 | 101 | ```cpp |
75 | | -struct MySink : c_log::Sink { |
| 102 | +struct NetworkSink : c_log::Sink { |
76 | 103 | void log(const std::string& msg) override { |
77 | | - // Custom output |
| 104 | + // Send 'msg' to a remote server... |
78 | 105 | } |
79 | 106 | }; |
| 107 | + |
| 108 | +// ... |
| 109 | +log.add_sink(std::make_unique<NetworkSink>()); |
80 | 110 | ``` |
81 | | -Add the custom sink to the logger: |
| 111 | +</details> |
| 112 | +
|
| 113 | +<details> |
| 114 | +<summary><strong>Filtering Levels</strong></summary> |
| 115 | +
|
| 116 | +Control verbosity dynamically: |
| 117 | +
|
82 | 118 | ```cpp |
83 | | -log.add_sink(std::make_unique<MySink>()); |
| 119 | +log.set_level(c_log::Level::Warning); // Ignore Info/Debug/Trace |
| 120 | +log.warn("system.low_memory"); // Logged |
| 121 | +log.info("system.heartbeat"); // Ignored |
84 | 122 | ``` |
85 | 123 | </details> |
86 | 124 |
|
87 | | -## Issues and Contributions |
88 | | -- For bugs, feature suggestions, or questions, please [open an Issue](https://github.com/mbn-code/cLog/issues). |
89 | | -- Star the repository if cLog is useful. |
90 | | -- Contribution guidelines are available in the [Contributing Guide](CONTRIBUTING.md). |
91 | | -- The project roadmap is listed below: |
| 125 | +--- |
| 126 | + |
| 127 | +## 🤝 Contributing |
| 128 | + |
| 129 | +We welcome contributions! Whether it's reporting a bug, suggesting a feature, or writing code. |
| 130 | + |
| 131 | +1. Check the [Issues](https://github.com/mbn-code/cLogpp/issues). |
| 132 | +2. Read the [Contributing Guide](CONTRIBUTING.md). |
| 133 | +3. Open a Pull Request. |
92 | 134 |
|
93 | | -### Project Roadmap |
94 | | -- [x] Robust async log draining and thread lifecycle |
| 135 | +**Roadmap:** |
| 136 | +- [x] Robust async log draining |
| 137 | +- [x] Zero-dependency JSON serializer |
95 | 138 | - [x] File and console sinks |
96 | | -- [x] CI/test coverage (Linux/Ubuntu) |
97 | | -- [ ] More flexible external sink/plugin system |
98 | | -- [ ] Windows and Mac CI |
99 | | -- [x] Simple structured logging (JSON) without external deps |
| 139 | +- [x] CI/Test coverage |
| 140 | +- [ ] Rotating file sink support |
| 141 | +- [ ] Windows/Mac CI runners |
100 | 142 |
|
101 | | -## License |
102 | | -MIT - see [LICENSE](LICENSE) |
| 143 | +--- |
| 144 | + |
| 145 | +## 📄 License |
103 | 146 |
|
104 | | -*Project status: Alpha. The API will become more stable as users provide feedback and as adoption increases.* |
| 147 | +MIT © [cLog++ Contributors](LICENSE). |
105 | 148 |
|
106 | | -## About cLog |
| 149 | +--- |
107 | 150 |
|
108 | | -cLog was originally developed as a practical structured logging solution for modern C++. The aim is to provide a robust, easy-to-use, and high-performance logger for projects requiring structured logs and safe multithreaded operation. Community feedback, issues, and contributions are welcome and greatly appreciated. |
| 151 | +> If you find cLog++ useful, please **[star the repository](https://github.com/mbn-code/cLogpp)**! It helps the project grow. ⭐ |
109 | 152 |
|
110 | 153 | <details> |
111 | 154 | <summary><strong>Note on AI Involvement</strong></summary> |
112 | 155 |
|
113 | | -Some portions of this project were implemented with the aid of AI language modeling tools. As a result, some aspects of the code and design may differ from conventionally developed open source tools. User reviews, suggestions, and contributions are essential to shaping the future of cLog. |
| 156 | +Some portions of this project were implemented with the aid of AI language modeling tools. As a result, some aspects of the code and design may differ from conventionally developed open source tools. User reviews, suggestions, and contributions are essential to shaping the future of cLog++. |
114 | 157 |
|
115 | 158 | </details> |
0 commit comments