LunarLog is a flexible, high-performance C++ logging library designed for modern applications. It offers a range of features to meet various logging needs, from basic console output to structured JSON and XML logging.
- Multiple log levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL)
- Support for Message Templates specification
- Customizable formatters, including built-in JSON and XML formatters
- Multiple transport options (file, console)
- Thread-safe design
- Rate limiting to prevent log flooding
- Placeholder validation for improved debugging
- Escaped brackets support for literal curly braces in log messages
- Context capture for enriched logging
- Compatible with C++11, C++14, and C++17
- C++11 or later compatible compiler
- Include the LunarLog headers in your project:
#include "lunar_log.hpp"- Ensure all component headers are in your include path.
- Configure your build system to use C++11 or later (e.g.,
-std=c++11,-std=c++14, or-std=c++17for GCC/Clang).
#include "lunar_log.hpp"
#include <iostream>
#include <thread>
#include <chrono>
int main() {
// Create a logger with minimum level set to TRACE
minta::LunarLog logger(minta::LogLevel::TRACE);
// Add a console sink with default formatter
logger.addSink<minta::ConsoleSink>();
// Add a file sink with default formatter
logger.addSink<minta::FileSink>("app.log");
// Add a file sink with built-in JSON formatter
logger.addSink<minta::FileSink, minta::JsonFormatter>("app.json.log");
// Add a file sink with built-in XML formatter
logger.addSink<minta::FileSink, minta::XmlFormatter>("app.xml.log");
// Basic logging with named placeholders
logger.info("User {username} logged in from {ip}", "alice", "192.168.1.1");
// Demonstrating escaped brackets
logger.info("Escaped brackets example: {{escaped}} {notEscaped}", "value");
return 0;
}Create custom formatters by implementing the IFormatter interface:
class CustomFormatter : public minta::IFormatter {
public:
std::string format(const minta::LogEntry &entry) const override {
return "CUSTOM: " + entry.message;
}
};
// Usage
logger.addSink<minta::FileSink, CustomFormatter>("custom_log.txt");LunarLog automatically applies rate limiting to prevent log flooding:
for (int i = 0; i < 2000; ++i) {
logger.info("Rate limit test message {index}", i);
}LunarLog provides warnings for common placeholder issues:
logger.info("Empty placeholder: {}", "value");
logger.info("Repeated placeholder: {placeholder} and {placeholder}", "value1", "value2");
logger.info("Too few values: {placeholder1} and {placeholder2}", "value");
logger.info("Too many values: {placeholder}", "value1", "value2");Enrich your logs with contextual information:
logger.setCaptureContext(true);
logger.setContext("session_id", "abc123");
logger.info("Log with global context");
{
minta::ContextScope scope(logger, "request_id", "req456");
logger.info("Log within scoped context");
}
logger.clearAllContext();
// Advanced usage with manual context specification
logger.logWithContext(minta::LogLevel::INFO, LUNAR_LOG_CONTEXT, "Manual context specification");
- Use named placeholders for better readability and maintainability.
- Set an appropriate log level for production environments.
- Implement multiple sinks for different logging needs (e.g., console, file, structured formats).
- Use JSON or XML logging for easier log parsing and analysis.
- Utilize custom formatters for specialized logging requirements.
- Be aware of rate limiting when logging high-frequency events.
- Use context capture to add rich, structured data to your logs.
LunarLog is designed to work with C++11, C++14, and C++17. When compiling, specify the appropriate standard for your project.
LunarLog is released under the MIT License. See the LICENSE file for details.
Contributions to LunarLog are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions about using LunarLog, please file an issue on the project's GitHub page.