autotoc_md943
doc_id: "LOG-API-002b" doc_title: "API Reference - Writers" doc_version: "1.0.0" doc_date: "2026-04-04" doc_status: "Released" project: "logger_system"
category: "API"
Language: English | 한국어
API Reference - Writers
Split from: API_REFERENCE.md
Version: 0.5.0.0 Last Updated: 2026-02-08
Table of Contents
- Writers
- Filters
- Formatters
- Log Sampling (v3.3.0)
- Real-time Analysis (v3.2.0)
- Backend Integration
- Monitoring Integration
- Usage Examples
- Thread Safety
- Performance Considerations
- Migration from v2.x
Writers
kcenon::logger::console_writer
class console_writer : public base_writer {
public:
console_writer(bool use_color = true);
void set_use_color(bool use_color);
};
kcenon::logger::file_writer
class file_writer : public base_writer {
public:
file_writer(const std::string& filename);
};
kcenon::logger::rotating_file_writer
class rotating_file_writer : public base_writer {
public:
enum class rotation_type {
size_based,
time_based,
daily,
hourly
};
rotating_file_writer(const std::string& filename,
std::size_t max_size,
std::size_t max_files);
rotating_file_writer(const std::string& filename,
rotation_type type,
std::size_t max_files);
};
kcenon::logger::network_writer
class network_writer : public base_writer {
public:
enum class protocol_type { tcp, udp };
network_writer(const std::string& host,
uint16_t port,
protocol_type protocol = protocol_type::tcp);
};
kcenon::logger::critical_writer
Synchronous writer for critical messages that bypass async queue:
class critical_writer : public base_writer {
public:
critical_writer(std::unique_ptr<base_writer> underlying_writer);
};
kcenon::logger::batch_writer
Wraps another writer with batching support:
class batch_writer : public base_writer {
public:
struct config {
std::size_t max_batch_size = 100;
std::chrono::milliseconds flush_interval{1000};
};
batch_writer(std::unique_ptr<base_writer> underlying_writer,
const config& cfg = {});
};
Filters
kcenon::logger::filters::level_filter
public:
};
Custom filter that only allows specific log levels.
level_filter(log_level min_level)
kcenon::logger::filters::regex_filter
class regex_filter : public log_filter_interface {
public:
regex_filter(const std::string& pattern, bool include = true);
};
kcenon::logger::filters::function_filter
class function_filter : public log_filter_interface {
public:
using filter_func = std::function<bool(const log_entry&)>;
function_filter(filter_func func);
};
kcenon::logger::filters::composite_filter
class composite_filter : public log_filter_interface {
public:
enum class logic_type { AND, OR };
composite_filter(logic_type logic = logic_type::AND);
void add_filter(std::unique_ptr<log_filter_interface> filter);
};
Formatters
kcenon::logger::plain_formatter
class plain_formatter : public base_formatter {
public:
std::string format(const log_entry& entry) const override;
};
kcenon::logger::json_formatter
class json_formatter : public base_formatter {
public:
json_formatter(bool pretty_print = false);
std::string format(const log_entry& entry) const override;
};
Log Sampling (v3.3.0)
Headers: <kcenon/logger/sampling/log_sampler.h>, <kcenon/logger/sampling/sampling_config.h>
Sampling Strategies
};
sampling_strategy
Defines the sampling algorithm to use.
@ hash_based
Deterministic sampling based on message hash.
@ random
Simple random sampling based on probability.
@ rate_limiting
Rate-based sampling (N logs per time window)
@ adaptive
Adaptive sampling that adjusts based on volume.
kcenon::logger::sampling::sampling_config
Configuration structure for log sampling behavior.
struct sampling_config {
bool enabled = false;
double rate = 1.0;
sampling_strategy strategy = sampling_strategy::random;
std::vector<log_level> always_log_levels = {
log_level::error,
log_level::critical
};
std::unordered_map<std::string, double> category_rates;
std::unordered_map<std::string, std::unordered_map<std::string, double>> field_rates;
std::vector<std::string> always_log_fields;
std::size_t rate_limit_per_second = 1000;
std::size_t rate_limit_window_ms = 1000;
bool adaptive_enabled = false;
std::size_t adaptive_threshold = 10000;
double adaptive_min_rate = 0.01;
std::uint64_t hash_seed = 0;
};
Static Factory Methods
static sampling_config disabled();
static sampling_config random_sampling(double sample_rate);
static sampling_config rate_limited(std::size_t max_per_second);
static sampling_config adaptive(std::size_t threshold,
double min_rate = 0.01);
static sampling_config hash_based(double sample_rate,
std::uint64_t seed = 0);
sampling_config with_always_log(std::vector<log_level> levels) &&;
sampling_config with_always_log(std::vector<log_level> levels) const &;
kcenon::logger::sampling::sampling_stats
struct sampling_stats {
std::uint64_t total_count = 0;
std::uint64_t sampled_count = 0;
std::uint64_t dropped_count = 0;
std::uint64_t bypassed_count = 0;
double effective_rate = 1.0;
bool is_throttling = false;
[[nodiscard]] double actual_ratio() const;
void reset();
};
kcenon::logger::sampling::log_sampler
Thread-safe log sampler with multiple strategy support. Uses fast PRNG (xorshift64) for minimal overhead.
Key Methods
explicit log_sampler(const sampling_config& config = sampling_config{});
[[nodiscard]] bool should_sample(const log_entry& entry);
[[nodiscard]] bool should_sample(log_level level, const std::string& message);
[[nodiscard]] bool should_sample(log_level level, const std::string& message,
const std::optional<std::string>& category);
void set_config(const sampling_config& config);
[[nodiscard]] sampling_config get_config() const;
[[nodiscard]] sampling_stats get_stats() const;
void reset_stats();
[[nodiscard]] bool is_enabled() const;
void set_enabled(bool enabled);
[[nodiscard]] double get_effective_rate() const;
kcenon::logger::sampling::sampler_factory
Factory for creating pre-configured samplers.
static std::unique_ptr<log_sampler> create_disabled();
static std::unique_ptr<log_sampler> create_random(double rate);
static std::unique_ptr<log_sampler> create_rate_limited(std::size_t max_per_second);
static std::unique_ptr<log_sampler> create_adaptive(
std::size_t threshold = 10000, double min_rate = 0.01);
static std::unique_ptr<log_sampler> create_production(
double base_rate = 0.1,
std::vector<log_level> critical_levels = {
log_level::warn, log_level::error, log_level::fatal
});
Usage Examples
auto config = sampling_config::random_sampling(0.1)
.with_always_log({log_level::error, log_level::fatal});
if (sampler.should_sample(entry)) {
}
config.category_rates["database"] = 0.01;
config.category_rates["security"] = 1.0;
config.category_rates["http"] = 0.05;
sampler.set_config(config);
config.field_rates["severity"]["high"] = 1.0;
config.field_rates["severity"]["low"] = 0.1;
config.field_rates["endpoint"]["/health"] = 0.01;
config.always_log_fields = {"error_id", "transaction_id"};
auto adaptive_sampler = sampler_factory::create_adaptive(10000, 0.01);
auto prod_sampler = sampler_factory::create_production(0.1);
auto stats = sampler.get_stats();
std::cout << "Sampled: " << stats.sampled_count
<< "/" << stats.total_count
<< " (" << (stats.actual_ratio() * 100) << "%)"
<< std::endl;
Thread-safe log sampler with multiple strategy support.
Log sampling implementation for high-volume scenarios kcenon.
Represents a single log entry with all associated metadata.
Real-time Analysis (v3.2.0)
Headers: <kcenon/logger/analysis/realtime_log_analyzer.h>, <kcenon/logger/analysis/log_analyzer.h>
kcenon::logger::analysis::anomaly_event
Represents an anomaly detected during real-time analysis.
struct anomaly_event {
enum class type : std::uint8_t {
error_spike,
pattern_match,
rate_anomaly,
new_error_type
};
type anomaly_type;
std::chrono::system_clock::time_point detected_at;
std::string description;
std::vector<analyzed_log_entry> related_entries;
std::string pattern;
size_t current_count = 0;
size_t threshold = 0;
};
kcenon::logger::analysis::realtime_analysis_config
struct realtime_analysis_config {
size_t error_spike_threshold = 100;
size_t rate_anomaly_high_threshold = 1000;
size_t rate_anomaly_low_threshold = 0;
std::chrono::seconds window_duration{60};
std::chrono::seconds baseline_duration{300};
bool track_new_errors = true;
bool enable_rate_anomaly_detection = true;
double rate_deviation_factor = 2.0;
size_t max_related_entries = 10;
};
kcenon::logger::analysis::realtime_log_analyzer
Real-time log analyzer with anomaly detection. Designed to be integrated directly into the logging pipeline with minimal performance impact (< 5% overhead).
Callback Type
using anomaly_callback = std::function<void(const anomaly_event&)>;
Constructor
realtime_log_analyzer() = default;
explicit realtime_log_analyzer(const realtime_analysis_config& config);
Key Methods
void set_anomaly_callback(anomaly_callback cb);
void analyze(const analyzed_log_entry& entry);
void set_error_spike_threshold(size_t errors_per_minute);
void add_pattern_alert(const std::string& pattern, log_level min_level);
bool remove_pattern_alert(const std::string& pattern);
void clear_pattern_alerts();
void set_rate_thresholds(size_t high_threshold, size_t low_threshold = 0);
void set_track_new_errors(bool enable);
double get_error_rate() const;
double get_log_rate() const;
const realtime_analysis_config& get_config() const;
void set_config(const realtime_analysis_config& config);
void reset();
Statistics
struct statistics {
size_t total_analyzed = 0;
size_t total_errors = 0;
size_t anomalies_detected = 0;
size_t error_spikes = 0;
size_t pattern_matches = 0;
size_t rate_anomalies = 0;
size_t new_error_types = 0;
double current_log_rate = 0.0;
double current_error_rate = 0.0;
};
statistics get_statistics() const;
kcenon::logger::analysis::realtime_analyzer_factory
Factory for creating pre-configured analyzers.
static std::unique_ptr<realtime_log_analyzer> create_basic();
static std::unique_ptr<realtime_log_analyzer> create(
const realtime_analysis_config& config);
static std::unique_ptr<realtime_log_analyzer> create_production(
size_t error_threshold = 50,
realtime_log_analyzer::anomaly_callback callback = nullptr);
Usage Example
auto analyzer = realtime_analyzer_factory::create_production(50,
switch (event.anomaly_type) {
case anomaly_event::type::error_spike:
send_pagerduty_alert(event.description);
break;
case anomaly_event::type::pattern_match:
send_slack_notification(event.description);
break;
case anomaly_event::type::rate_anomaly:
log_warning(event.description);
break;
case anomaly_event::type::new_error_type:
track_new_error(event.description);
break;
}
});
analyzer->add_pattern_alert("Connection refused", log_level::error);
analyzer->add_pattern_alert("OOM", log_level::fatal);
analyzer->add_pattern_alert("deadlock", log_level::error);
analyzer->set_rate_thresholds(
1000,
10
);
entry.
level = log_level::error;
entry.
message =
"Connection refused to db-primary";
entry.
timestamp = std::chrono::system_clock::now();
analyzer->analyze(entry);
auto stats = analyzer->get_statistics();
std::cout << "Analyzed: " << stats.total_analyzed
<< ", Anomalies: " << stats.anomalies_detected
<< ", Error rate: " << stats.current_error_rate << "/min"
<< std::endl;
Real-time log analysis with anomaly detection.
std::chrono::system_clock::time_point timestamp
Represents an anomaly event detected during real-time analysis.
Thread Safety
The analyze() method is thread-safe and can be called from multiple threads concurrently. The anomaly callback is invoked synchronously; for non-blocking operation, dispatch to a separate thread within the callback.
Backend Integration
Integration Backend Interface
namespace backends {
class integration_backend {
public:
virtual ~integration_backend() = default;
virtual common::interfaces::log_level to_common_level(
logger_system::log_level level) const = 0;
virtual logger_system::log_level from_common_level(
common::interfaces::log_level level) const = 0;
};
}
Standalone Backend (Default)
class standalone_backend : public integration_backend {
public:
standalone_backend() = default;
};
Monitoring Integration
IMonitor Integration (Phase 2.2.4)
std::shared_ptr<common::interfaces::IMonitor> monitor);
logger_builder& with_health_check_interval(std::chrono::milliseconds interval);
Builder pattern for logger construction with validation.
Usage Examples
Basic Usage with ILogger Interface
.
add_writer(
"console", std::make_unique<console_writer>())
std::cerr <<
"Failed: " <<
result.error().message() <<
"\n";
return 1;
}
logger->log(common::interfaces::log_level::info,
"Application started");
logger->log(common::interfaces::log_level::debug,
"Debug message");
return 0;
}
logger_builder & use_template(const std::string &name)
result< std::unique_ptr< logger > > build()
logger_builder & add_writer(const std::string &name, log_writer_ptr writer)
Add a writer to the logger.
Console writer for logging to stdout/stderr.
High-performance, thread-safe logging system with asynchronous capabilities.
Builder pattern implementation for flexible logger configuration kcenon.
Native API (Backward Compatible)
logger->log(log_level::info,
"Message with native types");
logger->log(log_level::error,
"Error occurred");
Configuration Strategies
.
add_writer(
"file", std::make_unique<rotating_file_writer>(
"logs/app.log", 10 * 1024 * 1024, 5))
.value();
logger_builder & auto_configure()
Auto-configure from environment variables.
logger_builder & with_performance_tuning(performance_level level)
Apply performance tuning.
logger_builder & for_environment(deployment_env env)
Configure for a specific deployment environment.
Custom Writer
public:
}
}
};
Base interface for all log writers and decorators.
virtual common::VoidResult flush()=0
Flush any buffered data.
virtual common::VoidResult write(const log_entry &entry)=0
Write a log entry.
Filtering
return entry.file.find("debug") == std::string::npos;
})
.build();
logger_builder & add_function_filter(std::function< bool(const log_entry &)> predicate)
Add a function-based filter (convenience method)
logger_builder & add_regex_filter(const std::string &pattern, bool include_matches=true)
Add a regex filter (convenience method)
logger_builder & add_level_filter(log_level min_level)
Add a level filter (convenience method)
Thread Safety
All public methods of the logger class are thread-safe. Writers are called sequentially (no concurrent writes to the same writer).
Performance Considerations
- Use asynchronous mode for better performance
- Batch size affects latency vs throughput trade-off
- Larger buffer sizes reduce contention but increase memory usage
- Use
is_enabled() check before expensive message construction
- Consider
critical_writer only for truly critical messages
Migration from v2.x
See MIGRATION_GUIDE.md for detailed migration instructions.
Key changes in v3.0:
- Namespace:
logger_module -> kcenon::logger
- Interface: Implements
common::interfaces::ILogger
- thread_system: Now optional (standalone mode is default)
- C++20 source_location support
Last Updated: 2026-02-08