Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
filter_factory.h
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
5#pragma once
6
16#include <memory>
17#include <string>
18#include <unordered_map>
19#include <functional>
20
24
25namespace kcenon::logger {
26
37public:
38 using creator_fn = std::function<std::unique_ptr<log_filter_interface>()>;
39
40 //========================================================================
41 // Static Factory Methods
42 //========================================================================
43
49 static std::unique_ptr<log_filter_interface> create_level(
50 log_level min_level
51 ) {
52 return std::make_unique<filters::level_filter>(min_level);
53 }
54
61 static std::unique_ptr<log_filter_interface> create_regex(
62 const std::string& pattern,
63 bool include_matches = true
64 ) {
65 return std::make_unique<filters::regex_filter>(pattern, include_matches);
66 }
67
73 static std::unique_ptr<log_filter_interface> create_function(
74 std::function<bool(const log_entry&)> predicate
75 ) {
76 return std::make_unique<filters::function_filter>(std::move(predicate));
77 }
78
83 static std::unique_ptr<filters::composite_filter> create_composite_and() {
84 return std::make_unique<filters::composite_filter>(
86 );
87 }
88
93 static std::unique_ptr<filters::composite_filter> create_composite_or() {
94 return std::make_unique<filters::composite_filter>(
96 );
97 }
98
99 //========================================================================
100 // Preset Factory Methods
101 //========================================================================
102
107 static std::unique_ptr<log_filter_interface> create_development() {
108 return create_level(log_level::trace);
109 }
110
115 static std::unique_ptr<log_filter_interface> create_production() {
117 composite->add_filter(create_level(log_level::warning));
118 composite->add_filter(create_regex("password|secret|token|api.?key", false));
119 return composite;
120 }
121
126 static std::unique_ptr<log_filter_interface> create_errors_only() {
127 return create_level(log_level::error);
128 }
129
135 static std::unique_ptr<log_filter_interface> create_component_filter(
136 const std::string& component
137 ) {
138 return create_regex("\\[" + component + "\\]", true);
139 }
140
145 static std::unique_ptr<log_filter_interface> create_sensitive_filter() {
146 return create_regex(
147 "password|secret|token|api.?key|credential|auth|bearer",
148 false // Exclude matches
149 );
150 }
151
152 //========================================================================
153 // Builder Pattern Helper
154 //========================================================================
155
159 class builder {
160 public:
164 builder& with_min_level(log_level level) {
165 filters_.push_back(create_level(level));
166 return *this;
167 }
168
172 builder& include_pattern(const std::string& pattern) {
173 filters_.push_back(create_regex(pattern, true));
174 return *this;
175 }
176
180 builder& exclude_pattern(const std::string& pattern) {
181 filters_.push_back(create_regex(pattern, false));
182 return *this;
183 }
184
188 builder& with_predicate(std::function<bool(const log_entry&)> pred) {
189 filters_.push_back(create_function(std::move(pred)));
190 return *this;
191 }
192
197 std::unique_ptr<log_filter_interface> build() {
198 if (filters_.empty()) {
199 return nullptr;
200 }
201 if (filters_.size() == 1) {
202 return std::move(filters_[0]);
203 }
204
206 for (auto& filter : filters_) {
207 composite->add_filter(std::move(filter));
208 }
209 return composite;
210 }
211
212 private:
213 std::vector<std::unique_ptr<log_filter_interface>> filters_;
214 };
215
221 return builder{};
222 }
223
224 //========================================================================
225 // Registry Pattern
226 //========================================================================
227
233 static void register_type(const std::string& name, creator_fn creator) {
234 registry()[name] = std::move(creator);
235 }
236
242 static std::unique_ptr<log_filter_interface> create(const std::string& name) {
243 auto it = registry().find(name);
244 if (it == registry().end()) {
245 return nullptr;
246 }
247 return it->second();
248 }
249
255 static bool has_type(const std::string& name) {
256 return registry().find(name) != registry().end();
257 }
258
259private:
260 static std::unordered_map<std::string, creator_fn>& registry() {
261 static std::unordered_map<std::string, creator_fn> instance;
262 return instance;
263 }
264};
265
266} // namespace kcenon::logger
Filter builder for complex filter construction.
builder & exclude_pattern(const std::string &pattern)
Exclude messages matching pattern.
std::unique_ptr< log_filter_interface > build()
Build the composite filter.
builder & with_min_level(log_level level)
Set minimum log level.
builder & include_pattern(const std::string &pattern)
Include messages matching pattern.
builder & with_predicate(std::function< bool(const log_entry &)> pred)
Add custom filter function.
std::vector< std::unique_ptr< log_filter_interface > > filters_
Factory for creating log filter instances.
static std::unique_ptr< log_filter_interface > create_function(std::function< bool(const log_entry &)> predicate)
Create a function-based filter.
static std::unique_ptr< log_filter_interface > create_component_filter(const std::string &component)
Create a debug filter for specific component.
static void register_type(const std::string &name, creator_fn creator)
Register a custom filter type.
static std::unique_ptr< log_filter_interface > create_production()
Create a production filter.
static std::unique_ptr< filters::composite_filter > create_composite_and()
Create a composite filter with AND logic.
static std::unique_ptr< log_filter_interface > create_sensitive_filter()
Create a sensitive data filter.
static std::unique_ptr< log_filter_interface > create_development()
Create a development filter.
static std::unique_ptr< filters::composite_filter > create_composite_or()
Create a composite filter with OR logic.
std::function< std::unique_ptr< log_filter_interface >()> creator_fn
static std::unique_ptr< log_filter_interface > create(const std::string &name)
Create a filter by registered name.
static std::unique_ptr< log_filter_interface > create_errors_only()
Create an error-only filter.
static std::unique_ptr< log_filter_interface > create_level(log_level min_level)
Create a level filter.
static std::unordered_map< std::string, creator_fn > & registry()
static bool has_type(const std::string &name)
Check if a filter type is registered.
static builder create_builder()
Start building a filter.
static std::unique_ptr< log_filter_interface > create_regex(const std::string &pattern, bool include_matches=true)
Create a regex filter.
Log filtering functionality.
Data structures for representing log entries and source locations kcenon.
Interface for log filters used by filtered_logger.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155