Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
logger_config_builder_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#include <iostream>
19
20using namespace kcenon::logger;
21
22int main() {
23 // Example 1: Use production defaults with custom settings
24 auto config1_result = logger_config_builder()
26 .set_buffer_size(32768)
27 .enable_metrics(false)
28 .set_log_directory("./production_logs")
29 .build();
30
31 if (config1_result.is_ok()) {
32 std::cout << "Production config created successfully" << std::endl;
33 std::cout << " Buffer size: " << config1_result.value().buffer_size << std::endl;
34 std::cout << " Log directory: " << config1_result.value().log_directory << std::endl;
35 }
36
37 // Example 2: Debug configuration
38 auto config2_result = logger_config_builder()
42 .build();
43
44 if (config2_result.is_ok()) {
45 std::cout << "\nDebug config created successfully" << std::endl;
46 std::cout << " Async: " << config2_result.value().async << std::endl;
47 std::cout << " Source location: " << config2_result.value().enable_source_location << std::endl;
48 }
49
50 // Example 3: Custom configuration from scratch
51 auto config3_result = logger_config_builder()
52 .set_async(true)
53 .set_buffer_size(16384)
54 .set_batch_size(100)
55 .set_flush_interval(std::chrono::milliseconds(500))
56 .enable_metrics(true)
57 .enable_timestamp(true)
58 .set_max_queue_size(50000)
59 .set_overflow_policy(logger_config::overflow_policy::drop_oldest)
60 .set_log_directory("./custom_logs")
61 .set_log_file_prefix("myapp")
62 .build();
63
64 if (config3_result.is_ok()) {
65 std::cout << "\nCustom config created successfully" << std::endl;
66 std::cout << " Buffer size: " << config3_result.value().buffer_size << std::endl;
67 std::cout << " Batch size: " << config3_result.value().batch_size << std::endl;
68 std::cout << " Max queue size: " << config3_result.value().max_queue_size << std::endl;
69 }
70
71 // Example 4: High-performance with network logging
72 auto config4_result = logger_config_builder()
74 .set_remote_logging("log-server.example.com", 5140)
75 .set_network_timeout(std::chrono::milliseconds(3000))
77 .build();
78
79 if (config4_result.is_ok()) {
80 std::cout << "\nHigh-performance network config created successfully" << std::endl;
81 std::cout << " Remote host: " << config4_result.value().remote_host << std::endl;
82 std::cout << " Remote port: " << config4_result.value().remote_port << std::endl;
83 }
84
85 // Example 5: Invalid configuration (will fail validation)
86 auto config5_result = logger_config_builder()
87 .set_buffer_size(0) // Invalid: buffer size must be > 0
88 .build();
89
90 if (!config5_result.is_ok()) {
91 std::cout << "\nValidation failed as expected:" << std::endl;
92 std::cout << " Error: " << config5_result.error().message << std::endl;
93 }
94
95 return 0;
96}
Fluent builder for logger_config.
logger_config_builder & enable_timestamp(bool enabled)
Enable/disable timestamp in log messages.
logger_config_builder & set_buffer_size(std::size_t size)
Set buffer size.
logger_config_builder & set_network_retry_count(std::size_t count)
Set network retry count.
logger_config_builder & enable_metrics(bool enabled)
Enable/disable metrics collection.
logger_config_builder & set_flush_interval(std::chrono::milliseconds interval)
Set flush interval.
logger_config_builder & use_debug_defaults()
Use debug configuration as base.
logger_config_builder & set_network_timeout(std::chrono::milliseconds timeout)
Set network timeout.
logger_config_builder & use_production_defaults()
Use production configuration as base.
logger_config_builder & set_overflow_policy(logger_config::overflow_policy policy)
Set queue overflow policy.
logger_config_builder & enable_source_location(bool enabled)
Enable/disable source location in log messages.
logger_config_builder & set_log_directory(const std::string &directory)
Set log directory.
logger_config_builder & use_high_performance_defaults()
Use high-performance configuration as base.
logger_config_builder & set_max_queue_size(std::size_t size)
Set maximum queue size.
logger_config_builder & set_async(bool enabled)
Set asynchronous logging mode.
result< logger_config > build() const
Build the logger_config.
logger_config_builder & set_log_file_prefix(const std::string &prefix)
Set log file prefix.
logger_config_builder & set_remote_logging(const std::string &host, uint16_t port)
Set remote logging host.
logger_config_builder & set_batch_size(std::size_t size)
Set batch size for batch writing.
logger_config_builder & enable_color_output(bool enabled)
Enable/disable color output.
Fluent builder for logger_config.