autotoc_md444
doc_id: "LOG-GUID-009" doc_title: "Critical Logging Quick Start" doc_version: "1.0.0" doc_date: "2026-04-04" doc_status: "Released" project: "logger_system"
category: "GUID"
Language: English | 한국어
Table of Contents
- 🎯 Goal
- 🚀 Quick Start
- Step 1: Include Headers
- Step 2: Create Hybrid Writer
- Step 3: Verify
- 📊 3 Usage Scenarios
- Scenario 1: General Web Services (Recommended)
- Scenario 2: Finance/Medical Systems (Maximum Reliability)
- Scenario 3: Games/Real-time Systems (High Performance)
- 🔧 Configuration Options
- ✅ Verification Methods
- Test 1: Check Critical Logs
- Test 2: Crash Simulation
- Test 3: Ctrl+C Test
- 📚 Learn More
- 🐛 Troubleshooting
- "WAL file not created"
- "Performance too slow"
- "Signal handler not working"
- 💡 Key Points
Critical Logging Quick Start
SSOT: This document is the single source of truth for Critical Logging Quick Start.
⚠️ Apply Critical Log Loss Prevention in 5 Minutes
🎯 Goal
Guarantee that critical logs are never lost in production environments.
🚀 Quick Start
Step 1: Include Headers
Synchronous wrapper for critical log messages to prevent loss.
High-performance, thread-safe logging system with asynchronous capabilities.
Step 2: Create Hybrid Writer
.
add_writer(
"main", std::make_unique<file_writer>(
"app.log"))
std::make_unique<hybrid_writer>(
std::make_unique<file_writer>("app.log")
)
)
Builder pattern for logger construction with validation.
logger_builder & with_async(bool async=true)
result< std::unique_ptr< logger > > build()
logger_builder & add_writer(const std::string &name, log_writer_ptr writer)
Add a writer to the logger.
Changes: Just wrap file_writer with hybrid_writer!
Step 3: Verify
logger->log(log_level::info,
"Normal message");
logger->log(log_level::critical,
"Critical error");
📊 3 Usage Scenarios
Scenario 1: General Web Services (Recommended)
auto hybrid = std::make_unique<hybrid_writer>(
std::make_unique<rotating_file_writer>("app.log", 100_MB, 10)
);
logger.add_writer(std::move(hybrid));
- ✅ Only critical/fatal immediately flushed
- ✅ Errors async (maintain performance)
- ⚡ Performance impact: < 1%
Scenario 2: Finance/Medical Systems (Maximum Reliability)
auto hybrid = std::make_unique<hybrid_writer>(
std::make_unique<rotating_file_writer>("audit.log", 50_MB, 20),
.force_flush_on_critical = true,
.force_flush_on_error = true,
.enable_signal_handlers = true,
.write_ahead_log = true,
.wal_path = "logs/.audit.wal"
}
);
logger.add_writer(std::move(hybrid));
Configuration for critical log writer.
- ✅ All errors and above flushed immediately
- ✅ Crash recovery possible with WAL
- ✅ Signal handler protection
- ⚡ Performance impact: 5-10% (acceptable)
Scenario 3: Games/Real-time Systems (High Performance)
auto hybrid = std::make_unique<hybrid_writer>(
std::make_unique<file_writer>("game.log"),
.force_flush_on_critical = true,
.force_flush_on_error = false,
.sync_on_critical = false
},
100000
);
logger.add_writer(std::move(hybrid));
- ✅ Only critical flushed (without fsync)
- ✅ Everything else fully async
- ⚡ Performance impact: < 0.1%
🔧 Configuration Options
.force_flush_on_critical = true,
.force_flush_on_error = false,
.enable_signal_handlers = true,
.write_ahead_log = false,
.sync_on_critical = true,
.critical_write_timeout_ms = 5000
}
| Option | Default | Recommended (General) | Recommended (High Reliability) |
force_flush_on_critical | true | true | true |
force_flush_on_error | false | false | true |
write_ahead_log | false | false | true |
sync_on_critical | true | true | true |
✅ Verification Methods
Test 1: Check Critical Logs
logger.log(log_level::critical,
"Test critical log");
system("tail -1 app.log");
Test 2: Crash Simulation
logger.log(log_level::critical,
"Before crash");
std::abort();
system("cat app.log | grep 'Before crash'");
Test 3: Ctrl+C Test
$ ./your_app
[INFO] App started
^C # Press Ctrl+C
[critical_writer] Signal SIGINT received, emergency flush
[critical_writer] Emergency flush completed
$ cat app.log
# All logs properly recorded!
📚 Learn More
🐛 Troubleshooting
"WAL file not created"
#include <filesystem>
std::filesystem::create_directories("logs");
"Performance too slow"
config.sync_on_critical = false;
"Signal handler not working"
💡 Key Points
- Use hybrid_writer → Performance + Safety
- Only critical/fatal immediately flushed → Minimal overhead
- WAL is optional → Enable only when needed
- fsync is expensive → Disable for high performance needs
Protect critical logs with just one line change!
.add_writer("main", std::make_unique<file_writer>("app.log"))
.add_writer("main", std::make_unique<hybrid_writer>(
std::make_unique<file_writer>("app.log")
))
Have questions or issues? Open an issue!
Last Updated: 2025-10-20