28 std::cout <<
"=== Composite Writer Example (SRP Architecture) ===" << std::endl;
29 std::cout << std::endl;
32 std::cout <<
"Example 1: Timestamp formatter + Console sink" << std::endl;
34 auto formatter = std::make_unique<timestamp_formatter>();
35 auto sink = std::make_unique<console_sink>();
39 entry.
level = log_level::info;
40 entry.
message =
"Application started successfully";
41 entry.
timestamp = std::chrono::system_clock::now();
44 std::cout <<
"Writer name: " << writer->get_name() << std::endl;
45 std::cout <<
"Is healthy: " << (writer->is_healthy() ?
"yes" :
"no") << std::endl;
47 std::cout << std::endl;
50 std::cout <<
"Example 2: Timestamp formatter + File sink" << std::endl;
52 auto formatter = std::make_unique<timestamp_formatter>();
53 auto sink = std::make_unique<file_sink>(
"/tmp/app.log",
true);
57 entry.
level = log_level::warning;
58 entry.
message =
"Configuration file not found, using defaults";
59 entry.
timestamp = std::chrono::system_clock::now();
63 std::cout <<
"Logged to file: " << writer->get_sink()->get_info() << std::endl;
65 std::cout << std::endl;
68 std::cout <<
"Example 3: Multiple writers (different configurations)" << std::endl;
71 auto console_formatter = std::make_unique<timestamp_formatter>();
72 auto console_sink = std::make_unique<console_sink>(
false,
true);
74 std::move(console_formatter),
79 auto error_formatter = std::make_unique<timestamp_formatter>();
80 auto error_sink = std::make_unique<console_sink>(
true,
true);
82 std::move(error_formatter),
87 info_entry.
level = log_level::info;
88 info_entry.
message =
"This goes to stdout";
89 info_entry.
timestamp = std::chrono::system_clock::now();
93 error_entry.
level = log_level::error;
94 error_entry.
message =
"This goes to stderr";
95 error_entry.
timestamp = std::chrono::system_clock::now();
96 error_writer->write(error_entry);
98 std::cout << std::endl;
100 std::cout <<
"=== Benefits of SRP Architecture ===" << std::endl;
101 std::cout <<
"1. Single Responsibility: Each component has one job" << std::endl;
102 std::cout <<
"2. Open/Closed: Add new formatters/sinks without modifying existing code" << std::endl;
103 std::cout <<
"3. Testability: Each component can be tested independently" << std::endl;
104 std::cout <<
"4. Flexibility: Mix and match any formatter with any sink" << std::endl;
105 std::cout <<
"5. Reusability: Formatters and sinks can be reused in different combinations" << std::endl;
Data structures for representing log entries and source locations kcenon.
Represents a single log entry with all associated metadata.
log_level level
Severity level of the log message.
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.