Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::environment_strategy Class Reference

Configuration strategy based on environment variables. More...

#include <environment_strategy.h>

Inheritance diagram for kcenon::logger::environment_strategy:
Inheritance graph
Collaboration diagram for kcenon::logger::environment_strategy:
Collaboration graph

Public Member Functions

std::string get_name () const override
 Get the strategy name.
 
void apply (logger_config &config) const override
 Apply this strategy to a logger configuration.
 
bool is_applicable () const override
 Check if this strategy is applicable in the current context.
 
int priority () const override
 Get the strategy priority.
 
- Public Member Functions inherited from kcenon::logger::config_strategy_interface
virtual ~config_strategy_interface ()=default
 

Static Private Member Functions

static log_level parse_log_level (const char *str)
 
static bool parse_bool (const char *str)
 
static std::size_t parse_size (const char *str, std::size_t default_value)
 

Detailed Description

Configuration strategy based on environment variables.

Reads environment variables and applies corresponding settings to the logger configuration. Useful for containerized deployments where configuration is passed via environment.

Since
2.0.0

Definition at line 40 of file environment_strategy.h.

Member Function Documentation

◆ apply()

void kcenon::logger::environment_strategy::apply ( logger_config & config) const
inlineoverridevirtual

Apply this strategy to a logger configuration.

Parameters
configThe configuration to modify

Modifies the provided configuration according to the strategy's rules. Changes are applied in-place.

Implements kcenon::logger::config_strategy_interface.

Definition at line 46 of file environment_strategy.h.

46 {
47 // LOG_LEVEL
48 if (const char* level = std::getenv("LOG_LEVEL")) {
49 config.min_level = parse_log_level(level);
50 }
51
52 // LOG_ASYNC
53 if (const char* async_str = std::getenv("LOG_ASYNC")) {
54 config.async = parse_bool(async_str);
55 if (!config.async && config.batch_size > 1) {
56 config.batch_size = 1;
57 }
58 }
59
60 // LOG_BUFFER_SIZE
61 if (const char* buffer = std::getenv("LOG_BUFFER_SIZE")) {
62 config.buffer_size = parse_size(buffer, config.buffer_size);
63 }
64
65 // LOG_BATCH_SIZE
66 if (const char* batch = std::getenv("LOG_BATCH_SIZE")) {
67 config.batch_size = parse_size(batch, config.batch_size);
68 }
69
70 // LOG_FLUSH_INTERVAL
71 if (const char* interval = std::getenv("LOG_FLUSH_INTERVAL")) {
72 config.flush_interval = std::chrono::milliseconds(
73 parse_size(interval, static_cast<std::size_t>(config.flush_interval.count()))
74 );
75 }
76
77 // LOG_COLOR
78 if (const char* color = std::getenv("LOG_COLOR")) {
79 config.enable_color_output = parse_bool(color);
80 }
81
82 // LOG_METRICS
83 if (const char* metrics = std::getenv("LOG_METRICS")) {
84 config.enable_metrics = parse_bool(metrics);
85 }
86
87 // LOG_STRUCTURED
88 if (const char* structured = std::getenv("LOG_STRUCTURED")) {
89 config.enable_structured_logging = parse_bool(structured);
90 }
91
92 // LOG_CRASH_HANDLER
93 if (const char* crash = std::getenv("LOG_CRASH_HANDLER")) {
94 config.enable_crash_handler = parse_bool(crash);
95 }
96
97 // LOG_MAX_QUEUE_SIZE
98 if (const char* queue = std::getenv("LOG_MAX_QUEUE_SIZE")) {
99 config.max_queue_size = parse_size(queue, config.max_queue_size);
100 }
101
102 // LOG_BATCH_WRITING
103 if (const char* batch_write = std::getenv("LOG_BATCH_WRITING")) {
104 config.enable_batch_writing = parse_bool(batch_write);
105 }
106 }
static log_level parse_log_level(const char *str)
static bool parse_bool(const char *str)
static std::size_t parse_size(const char *str, std::size_t default_value)

References kcenon::logger::logger_config::async, kcenon::logger::logger_config::batch_size, kcenon::logger::logger_config::buffer_size, kcenon::logger::logger_config::enable_batch_writing, kcenon::logger::logger_config::enable_color_output, kcenon::logger::logger_config::enable_crash_handler, kcenon::logger::logger_config::enable_metrics, kcenon::logger::logger_config::enable_structured_logging, kcenon::logger::logger_config::flush_interval, kcenon::logger::logger_config::max_queue_size, kcenon::logger::logger_config::min_level, parse_bool(), parse_log_level(), and parse_size().

Here is the call graph for this function:

◆ get_name()

std::string kcenon::logger::environment_strategy::get_name ( ) const
inlineoverridevirtual

Get the strategy name.

Returns
Human-readable strategy name

Implements kcenon::logger::config_strategy_interface.

Definition at line 42 of file environment_strategy.h.

42 {
43 return "environment";
44 }

◆ is_applicable()

bool kcenon::logger::environment_strategy::is_applicable ( ) const
inlineoverridevirtual

Check if this strategy is applicable in the current context.

Returns
true if the strategy should be applied

Override to implement conditional strategy application (e.g., only apply in certain environments).

Reimplemented from kcenon::logger::config_strategy_interface.

Definition at line 108 of file environment_strategy.h.

108 {
109 // Applicable if any relevant environment variable is set
110 return std::getenv("LOG_LEVEL") != nullptr ||
111 std::getenv("LOG_ASYNC") != nullptr ||
112 std::getenv("LOG_BUFFER_SIZE") != nullptr ||
113 std::getenv("LOG_BATCH_SIZE") != nullptr ||
114 std::getenv("LOG_FLUSH_INTERVAL") != nullptr ||
115 std::getenv("LOG_COLOR") != nullptr ||
116 std::getenv("LOG_METRICS") != nullptr;
117 }

◆ parse_bool()

static bool kcenon::logger::environment_strategy::parse_bool ( const char * str)
inlinestaticprivate

Definition at line 139 of file environment_strategy.h.

139 {
140 std::string value(str);
141 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
142 return value == "true" || value == "1" || value == "yes" || value == "on";
143 }

Referenced by apply().

Here is the caller graph for this function:

◆ parse_log_level()

static log_level kcenon::logger::environment_strategy::parse_log_level ( const char * str)
inlinestaticprivate

Definition at line 125 of file environment_strategy.h.

125 {
126 std::string level_str(str);
127 std::transform(level_str.begin(), level_str.end(), level_str.begin(), ::tolower);
128
129 if (level_str == "trace") return log_level::trace;
130 if (level_str == "debug") return log_level::debug;
131 if (level_str == "info") return log_level::info;
132 if (level_str == "warn" || level_str == "warning") return log_level::warning;
133 if (level_str == "error") return log_level::error;
134 if (level_str == "fatal" || level_str == "critical") return log_level::critical;
135
136 return log_level::info; // Default
137 }

Referenced by apply().

Here is the caller graph for this function:

◆ parse_size()

static std::size_t kcenon::logger::environment_strategy::parse_size ( const char * str,
std::size_t default_value )
inlinestaticprivate

Definition at line 145 of file environment_strategy.h.

145 {
146 try {
147 return std::stoull(str);
148 } catch (...) {
149 return default_value;
150 }
151 }

Referenced by apply().

Here is the caller graph for this function:

◆ priority()

int kcenon::logger::environment_strategy::priority ( ) const
inlineoverridevirtual

Get the strategy priority.

Returns
Priority value (higher = applied first)

When multiple strategies are applied, they are sorted by priority (descending) before application.

Reimplemented from kcenon::logger::config_strategy_interface.

Definition at line 119 of file environment_strategy.h.

119 {
120 // High priority - environment overrides other strategies
121 return 100;
122 }

The documentation for this class was generated from the following file: