Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
composite_writer_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
17#include <iostream>
18#include <memory>
24
25using namespace kcenon::logger;
26
27int main() {
28 std::cout << "=== Composite Writer Example (SRP Architecture) ===" << std::endl;
29 std::cout << std::endl;
30
31 // Example 1: Console writer with timestamp formatter
32 std::cout << "Example 1: Timestamp formatter + Console sink" << std::endl;
33 {
34 auto formatter = std::make_unique<timestamp_formatter>();
35 auto sink = std::make_unique<console_sink>();
36 auto writer = make_composite_writer(std::move(formatter), std::move(sink));
37
38 log_entry entry;
39 entry.level = log_level::info;
40 entry.message = "Application started successfully";
41 entry.timestamp = std::chrono::system_clock::now();
42
43 writer->write(entry);
44 std::cout << "Writer name: " << writer->get_name() << std::endl;
45 std::cout << "Is healthy: " << (writer->is_healthy() ? "yes" : "no") << std::endl;
46 }
47 std::cout << std::endl;
48
49 // Example 2: File writer with timestamp formatter
50 std::cout << "Example 2: Timestamp formatter + File sink" << std::endl;
51 {
52 auto formatter = std::make_unique<timestamp_formatter>();
53 auto sink = std::make_unique<file_sink>("/tmp/app.log", true);
54 auto writer = make_composite_writer(std::move(formatter), std::move(sink));
55
56 log_entry entry;
57 entry.level = log_level::warning;
58 entry.message = "Configuration file not found, using defaults";
59 entry.timestamp = std::chrono::system_clock::now();
60
61 writer->write(entry);
62 writer->flush();
63 std::cout << "Logged to file: " << writer->get_sink()->get_info() << std::endl;
64 }
65 std::cout << std::endl;
66
67 // Example 3: Multiple writers with different sink configurations
68 std::cout << "Example 3: Multiple writers (different configurations)" << std::endl;
69 {
70 // Console to stdout
71 auto console_formatter = std::make_unique<timestamp_formatter>();
72 auto console_sink = std::make_unique<console_sink>(false, true); // stdout, auto-flush
74 std::move(console_formatter),
75 std::move(console_sink)
76 );
77
78 // Console to stderr
79 auto error_formatter = std::make_unique<timestamp_formatter>();
80 auto error_sink = std::make_unique<console_sink>(true, true); // stderr, auto-flush
81 auto error_writer = make_composite_writer(
82 std::move(error_formatter),
83 std::move(error_sink)
84 );
85
86 log_entry info_entry;
87 info_entry.level = log_level::info;
88 info_entry.message = "This goes to stdout";
89 info_entry.timestamp = std::chrono::system_clock::now();
90 console_writer->write(info_entry);
91
92 log_entry error_entry;
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);
97 }
98 std::cout << std::endl;
99
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;
106
107 return 0;
108}
Outputs log messages to console (stdout or stderr)
Core console writer for logging to stdout/stderr.
common::VoidResult write(const log_entry &entry) override
Write a log entry to console.
Composite writer combining formatter and sink (Pipeline Pattern) kcenon.
Console output sink implementation (stdout/stderr) kcenon.
File output sink implementation.
Data structures for representing log entries and source locations kcenon.
std::unique_ptr< composite_writer > make_composite_writer(std::unique_ptr< log_formatter_interface > formatter, std::unique_ptr< output_sink_interface > sink)
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
log_level level
Severity level of the log message.
Definition log_entry.h:162
small_string_256 message
The actual log message.
Definition log_entry.h:169
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.
Definition log_entry.h:175
Default human-readable formatter with timestamps kcenon.