Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
formatter_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
26
27namespace kcenon::logger {
28
33enum class format_type {
34 plain,
35 json,
36 logfmt,
37 templated,
38 compact
39};
40
51public:
52 using creator_fn = std::function<std::unique_ptr<log_formatter_interface>()>;
53
54 //========================================================================
55 // Static Factory Methods
56 //========================================================================
57
63 static std::unique_ptr<log_formatter_interface> create_plain(
64 const format_options& opts = format_options{}
65 ) {
66 return std::make_unique<timestamp_formatter>(opts);
67 }
68
74 static std::unique_ptr<log_formatter_interface> create_json(
75 const format_options& opts = format_options{}
76 ) {
77 return std::make_unique<json_formatter>(opts);
78 }
79
86 static std::unique_ptr<log_formatter_interface> create_logfmt(
87 const format_options& opts = format_options{}
88 ) {
89 return std::make_unique<logfmt_formatter>(opts);
90 }
91
99 static std::unique_ptr<log_formatter_interface> create_template(
100 const std::string& template_pattern = template_formatter::DEFAULT_TEMPLATE,
101 const format_options& opts = format_options{}
102 ) {
103 return std::make_unique<template_formatter>(template_pattern, opts);
104 }
105
112 static std::unique_ptr<log_formatter_interface> create_compact() {
113 format_options opts;
114 opts.include_timestamp = true;
115 opts.include_thread_id = false;
116 opts.include_source_location = false;
117 opts.include_level = true;
118 opts.use_colors = false;
119 return std::make_unique<timestamp_formatter>(opts);
120 }
121
128 static std::unique_ptr<log_formatter_interface> create(
129 format_type type,
130 const format_options& opts = format_options{}
131 ) {
132 switch (type) {
134 return create_json(opts);
136 return create_logfmt(opts);
140 return create_compact();
142 default:
143 return create_plain(opts);
144 }
145 }
146
147 //========================================================================
148 // Preset Factory Methods
149 //========================================================================
150
155 static std::unique_ptr<log_formatter_interface> create_development() {
156 format_options opts;
157 opts.include_timestamp = true;
158 opts.include_thread_id = true;
159 opts.include_source_location = true;
160 opts.include_level = true;
161 opts.use_colors = true;
162 return create_plain(opts);
163 }
164
169 static std::unique_ptr<log_formatter_interface> create_production() {
170 format_options opts;
171 opts.include_timestamp = true;
172 opts.include_thread_id = true;
173 opts.include_source_location = false;
174 opts.include_level = true;
175 opts.use_colors = false;
176 return create_json(opts);
177 }
178
183 static std::unique_ptr<log_formatter_interface> create_minimal() {
184 format_options opts;
185 opts.include_timestamp = false;
186 opts.include_thread_id = false;
187 opts.include_source_location = false;
188 opts.include_level = true;
189 opts.use_colors = false;
190 return create_plain(opts);
191 }
192
193 //========================================================================
194 // Registry Pattern
195 //========================================================================
196
202 static void register_type(const std::string& name, creator_fn creator) {
203 registry()[name] = std::move(creator);
204 }
205
211 static std::unique_ptr<log_formatter_interface> create(const std::string& name) {
212 auto it = registry().find(name);
213 if (it == registry().end()) {
214 return nullptr;
215 }
216 return it->second();
217 }
218
224 static bool has_type(const std::string& name) {
225 return registry().find(name) != registry().end();
226 }
227
228private:
229 static std::unordered_map<std::string, creator_fn>& registry() {
230 static std::unordered_map<std::string, creator_fn> instance;
231 return instance;
232 }
233};
234
235} // namespace kcenon::logger
Factory for creating log formatter instances.
static std::unique_ptr< log_formatter_interface > create_plain(const format_options &opts=format_options{})
Create a plain/timestamp formatter.
static std::unique_ptr< log_formatter_interface > create_compact()
Create a compact formatter.
static std::unique_ptr< log_formatter_interface > create_development()
Create a development formatter.
static std::unique_ptr< log_formatter_interface > create(format_type type, const format_options &opts=format_options{})
Create a formatter by type enum.
static std::unique_ptr< log_formatter_interface > create(const std::string &name)
Create a formatter by registered name.
static std::unique_ptr< log_formatter_interface > create_template(const std::string &template_pattern=template_formatter::DEFAULT_TEMPLATE, const format_options &opts=format_options{})
Create a template formatter with custom pattern.
static std::unique_ptr< log_formatter_interface > create_json(const format_options &opts=format_options{})
Create a JSON formatter.
static std::unique_ptr< log_formatter_interface > create_production()
Create a production formatter.
static std::unordered_map< std::string, creator_fn > & registry()
static std::unique_ptr< log_formatter_interface > create_minimal()
Create a minimal formatter.
std::function< std::unique_ptr< log_formatter_interface >()> creator_fn
static bool has_type(const std::string &name)
Check if a formatter type is registered.
static void register_type(const std::string &name, creator_fn creator)
Register a custom formatter type.
static std::unique_ptr< log_formatter_interface > create_logfmt(const format_options &opts=format_options{})
Create a logfmt formatter.
static constexpr const char * DEFAULT_TEMPLATE
Default template pattern.
JSON formatter for structured logging kcenon.
Interface for log message formatters (Strategy Pattern) kcenon.
Logfmt formatter for structured logging kcenon.
format_type
Pre-defined format types.
@ logfmt
Logfmt key=value format.
@ json
JSON structured format.
@ templated
Template-based custom format.
@ compact
Compact single-line format.
@ plain
Plain text with timestamps.
Configuration options for log formatting.
Template-based customizable formatter for structured logging kcenon.
Default human-readable formatter with timestamps kcenon.