Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
critical_logging_example.cpp

This example shows how to configure the logger system to prevent loss of critical log messages during crashes or abnormal termination.

// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <kcenon/common/interfaces/logger_interface.h>
#include <iostream>
using namespace kcenon::logger;
namespace ci = kcenon::common::interfaces;
std::cout << "\n=== Example 1: Basic Critical Writer ===\n";
// Create logger with critical writer
logger log(false); // Synchronous mode for this example
// Wrap file writer with critical_writer
auto file = std::make_unique<file_writer>("logs/critical_basic.log");
auto critical = std::make_unique<critical_writer>(
std::move(file),
.force_flush_on_critical = true,
.force_flush_on_error = false,
.write_ahead_log = false,
.sync_on_critical = true
}
);
log.add_writer(std::move(critical));
// Normal logs (buffered)
log.log(ci::log_level::info, std::string("Application started"));
log.log(ci::log_level::debug, std::string("Debug information"));
// Critical log (immediately flushed to disk)
log.log(ci::log_level::critical, std::string("Critical error occurred - guaranteed on disk"));
// Even if the program crashes here, the critical log above is safe
std::cout << "Critical log written and flushed immediately\n";
}
std::cout << "\n=== Example 2: Write-Ahead Logging ===\n";
logger log(false);
auto file = std::make_unique<rotating_file_writer>(
"logs/wal_main.log",
1024 * 1024, // 1 MB per file
5 // Keep 5 rotations
);
auto critical = std::make_unique<critical_writer>(
std::move(file),
.force_flush_on_critical = true,
.write_ahead_log = true, // Enable WAL
.wal_path = "logs/.critical.wal",
.sync_on_critical = true
}
);
log.add_writer(std::move(critical));
log.log(ci::log_level::info, std::string("Normal log"));
log.log(ci::log_level::critical, std::string("Critical log - written to WAL first"));
std::cout << "Check logs/.critical.wal for write-ahead log entries\n";
}
std::cout << "\n=== Example 3: Hybrid Writer (Async + Critical) ===\n";
logger log(false);
// Hybrid writer combines:
// - Async queue for normal/debug/info/warn (high performance)
// - Immediate flush for error/critical/fatal (no loss)
auto hybrid = std::make_unique<hybrid_writer>(
std::make_unique<file_writer>("logs/hybrid.log"),
.force_flush_on_critical = true,
.force_flush_on_error = true // Also flush errors immediately
},
10000 // Async queue size
);
log.add_writer(std::move(hybrid));
// These go through async queue (fast)
for (int i = 0; i < 100; ++i) {
log.log(ci::log_level::info, "High-frequency log " + std::to_string(i));
}
// This bypasses the queue and flushes immediately (safe)
log.log(ci::log_level::critical, std::string("Critical error - no loss guaranteed"));
std::cout << "Hybrid writer provides both performance and safety\n";
}
std::cout << "\n=== Example 4: Production Configuration ===\n";
// Use logger_builder for comprehensive configuration
.with_async(true) // Async for performance
.with_buffer_size(32768) // Large buffer for high throughput
.with_min_level(log_level::info) // Production: info and above
.add_writer("main",
std::make_unique<hybrid_writer>(
std::make_unique<rotating_file_writer>(
"logs/production.log",
100 * 1024 * 1024, // 100 MB per file
10 // Keep 10 rotations
),
.force_flush_on_critical = true,
.force_flush_on_error = true,
.write_ahead_log = true,
.wal_path = "logs/.production.wal",
.sync_on_critical = true,
.critical_write_timeout_ms = 5000
},
50000 // Large async queue
)
)
.build();
if (!result) {
std::cerr << "Failed to build logger: " << result.error_message() << "\n";
return;
}
auto log = std::move(result.value());
log->start();
// Production logging examples
log->log(ci::log_level::info, std::string("Service started"));
log->log(ci::log_level::warning, std::string("Cache miss rate high"));
log->log(ci::log_level::error, std::string("Database connection timeout"));
log->log(ci::log_level::critical, std::string("Out of memory - terminating"));
log->flush();
log->stop();
std::cout << "Production setup complete\n";
std::cout << "Configuration:\n";
std::cout << " - Async logging for normal messages (performance)\n";
std::cout << " - Immediate flush for errors and critical (safety)\n";
std::cout << " - Write-ahead logging for crash recovery\n";
std::cout << " - File rotation to manage disk space\n";
}
std::cout << "\n=== Example 5: Error Handling & Statistics ===\n";
logger log(false);
auto critical = std::make_unique<critical_writer>(
std::make_unique<file_writer>("logs/stats.log"),
.force_flush_on_critical = true,
.write_ahead_log = true,
.wal_path = "logs/.stats.wal"
}
);
// Store reference before moving
const auto& config = critical->get_config();
const auto& stats = critical->get_stats();
log.add_writer(std::move(critical));
// Generate logs
log.log(ci::log_level::info, std::string("Info message"));
log.log(ci::log_level::warning, std::string("Warning message"));
log.log(ci::log_level::error, std::string("Error message"));
log.log(ci::log_level::critical, std::string("Critical message 1"));
log.log(ci::log_level::critical, std::string("Critical message 2"));
log.log(ci::log_level::critical, std::string("Fatal message"));
// Check statistics
std::cout << "\nConfiguration:\n";
std::cout << " Force flush on critical: " << config.force_flush_on_critical << "\n";
std::cout << " Force flush on error: " << config.force_flush_on_error << "\n";
std::cout << " WAL enabled: " << config.write_ahead_log << "\n";
std::cout << " Sync on critical: " << config.sync_on_critical << "\n";
std::cout << "\nStatistics:\n";
std::cout << " Total critical writes: " << stats.total_critical_writes.load() << "\n";
std::cout << " Total flushes: " << stats.total_flushes.load() << "\n";
std::cout << " WAL writes: " << stats.wal_writes.load() << "\n";
std::cout << " Sync calls: " << stats.sync_calls.load() << "\n";
// Runtime configuration change
std::cout << "\nChanging configuration at runtime...\n";
// Note: set_force_flush_on_critical() would be called on the writer directly
// This example shows the concept
}
int main() {
std::cout << "Critical Logging Examples\n";
std::cout << "=========================\n";
try {
std::cout << "\n=== All Examples Completed Successfully ===\n";
std::cout << "\nCheck the logs/ directory for output files:\n";
std::cout << " - *.log: Main log files\n";
std::cout << " - .*.wal: Write-ahead log files\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
return 0;
}
int main()
Asynchronous wrapper for log writers.
Builder pattern for logger construction with validation.
logger_builder & with_min_level(log_level level)
logger_builder & with_async(bool async=true)
logger_builder & add_writer(const std::string &name, log_writer_ptr writer)
Add a writer to the logger.
logger_builder & with_buffer_size(std::size_t size)
Set buffer size.
common::VoidResult add_writer(log_writer_ptr writer)
Definition logger.cpp:265
common::VoidResult log(common::interfaces::log_level level, const std::string &message) override
Log a message with specified level (ILogger interface)
Definition logger.cpp:378
const std::string & error_message() const
void example_write_ahead_logging()
void example_basic_critical_writer()
void example_hybrid_writer()
void example_error_handling()
void example_production_setup()
Synchronous wrapper for critical log messages to prevent loss.
File writer for logging to files with optional buffering.
High-performance, thread-safe logging system with asynchronous capabilities.
Builder pattern implementation for flexible logger configuration kcenon.
Rotating file writer with size and time-based rotation.
Configuration for critical log writer.