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

Factory for creating log formatter instances. More...

#include <formatter_factory.h>

Collaboration diagram for kcenon::logger::formatter_factory:
Collaboration graph

Public Types

using creator_fn = std::function<std::unique_ptr<log_formatter_interface>()>
 

Static Public Member Functions

static std::unique_ptr< log_formatter_interfacecreate_plain (const format_options &opts=format_options{})
 Create a plain/timestamp formatter.
 
static std::unique_ptr< log_formatter_interfacecreate_json (const format_options &opts=format_options{})
 Create a JSON formatter.
 
static std::unique_ptr< log_formatter_interfacecreate_logfmt (const format_options &opts=format_options{})
 Create a logfmt formatter.
 
static std::unique_ptr< log_formatter_interfacecreate_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_interfacecreate_compact ()
 Create a compact formatter.
 
static std::unique_ptr< log_formatter_interfacecreate (format_type type, const format_options &opts=format_options{})
 Create a formatter by type enum.
 
static std::unique_ptr< log_formatter_interfacecreate_development ()
 Create a development formatter.
 
static std::unique_ptr< log_formatter_interfacecreate_production ()
 Create a production formatter.
 
static std::unique_ptr< log_formatter_interfacecreate_minimal ()
 Create a minimal formatter.
 
static void register_type (const std::string &name, creator_fn creator)
 Register a custom formatter type.
 
static std::unique_ptr< log_formatter_interfacecreate (const std::string &name)
 Create a formatter by registered name.
 
static bool has_type (const std::string &name)
 Check if a formatter type is registered.
 

Static Private Member Functions

static std::unordered_map< std::string, creator_fn > & registry ()
 

Detailed Description

Factory for creating log formatter instances.

Provides static factory methods for all built-in formatter types and a registry pattern for custom formatter types.

Since
2.0.0

Definition at line 50 of file formatter_factory.h.

Member Typedef Documentation

◆ creator_fn

using kcenon::logger::formatter_factory::creator_fn = std::function<std::unique_ptr<log_formatter_interface>()>

Definition at line 52 of file formatter_factory.h.

Member Function Documentation

◆ create() [1/2]

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create ( const std::string & name)
inlinestatic

Create a formatter by registered name.

Parameters
nameRegistered type name
Returns
Unique pointer to formatter, or nullptr if not found

Definition at line 211 of file formatter_factory.h.

211 {
212 auto it = registry().find(name);
213 if (it == registry().end()) {
214 return nullptr;
215 }
216 return it->second();
217 }
static std::unordered_map< std::string, creator_fn > & registry()

References registry().

Here is the call graph for this function:

◆ create() [2/2]

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create ( format_type type,
const format_options & opts = format_options{} )
inlinestatic

Create a formatter by type enum.

Parameters
typeFormat type
optsFormat options (for customization)
Returns
Unique pointer to formatter

Definition at line 128 of file formatter_factory.h.

130 {}
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 }
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_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_logfmt(const format_options &opts=format_options{})
Create a logfmt formatter.
static constexpr const char * DEFAULT_TEMPLATE
Default template pattern.
@ logfmt
Logfmt key=value format.
@ json
JSON structured format.
@ templated
Template-based custom format.
@ compact
Compact single-line format.
@ plain
Plain text with timestamps.

◆ create_compact()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_compact ( )
inlinestatic

Create a compact formatter.

Returns
Unique pointer to compact formatter

Compact format: minimal overhead, single line

Definition at line 112 of file formatter_factory.h.

112 {
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 }

References kcenon::logger::format_options::include_level, kcenon::logger::format_options::include_source_location, kcenon::logger::format_options::include_thread_id, kcenon::logger::format_options::include_timestamp, and kcenon::logger::format_options::use_colors.

◆ create_development()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_development ( )
inlinestatic

Create a development formatter.

Returns
Colored timestamp formatter with source location

Definition at line 155 of file formatter_factory.h.

155 {
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 }

References create_plain(), kcenon::logger::format_options::include_level, kcenon::logger::format_options::include_source_location, kcenon::logger::format_options::include_thread_id, kcenon::logger::format_options::include_timestamp, and kcenon::logger::format_options::use_colors.

Here is the call graph for this function:

◆ create_json()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_json ( const format_options & opts = format_options{})
inlinestatic

Create a JSON formatter.

Parameters
optsFormat options
Returns
Unique pointer to JSON formatter

Definition at line 74 of file formatter_factory.h.

75 {}
76 ) {
77 return std::make_unique<json_formatter>(opts);
78 }

Referenced by create_production().

Here is the caller graph for this function:

◆ create_logfmt()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_logfmt ( const format_options & opts = format_options{})
inlinestatic

Create a logfmt formatter.

Parameters
optsFormat options
Returns
Unique pointer to logfmt formatter
Since
3.1.0

Definition at line 86 of file formatter_factory.h.

87 {}
88 ) {
89 return std::make_unique<logfmt_formatter>(opts);
90 }

◆ create_minimal()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_minimal ( )
inlinestatic

Create a minimal formatter.

Returns
Compact formatter with minimal fields

Definition at line 183 of file formatter_factory.h.

183 {
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 }

References create_plain(), kcenon::logger::format_options::include_level, kcenon::logger::format_options::include_source_location, kcenon::logger::format_options::include_thread_id, kcenon::logger::format_options::include_timestamp, and kcenon::logger::format_options::use_colors.

Here is the call graph for this function:

◆ create_plain()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_plain ( const format_options & opts = format_options{})
inlinestatic

Create a plain/timestamp formatter.

Parameters
optsFormat options
Returns
Unique pointer to timestamp formatter

Definition at line 63 of file formatter_factory.h.

64 {}
65 ) {
66 return std::make_unique<timestamp_formatter>(opts);
67 }

Referenced by create_development(), and create_minimal().

Here is the caller graph for this function:

◆ create_production()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_production ( )
inlinestatic

Create a production formatter.

Returns
JSON formatter for structured logging

Definition at line 169 of file formatter_factory.h.

169 {
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 }

References create_json(), kcenon::logger::format_options::include_level, kcenon::logger::format_options::include_source_location, kcenon::logger::format_options::include_thread_id, kcenon::logger::format_options::include_timestamp, and kcenon::logger::format_options::use_colors.

Here is the call graph for this function:

◆ create_template()

static std::unique_ptr< log_formatter_interface > kcenon::logger::formatter_factory::create_template ( const std::string & template_pattern = template_formatter::DEFAULT_TEMPLATE,
const format_options & opts = format_options{} )
inlinestatic

Create a template formatter with custom pattern.

Parameters
template_patternTemplate string with placeholders
optsFormat options
Returns
Unique pointer to template formatter
Since
3.1.0

Definition at line 99 of file formatter_factory.h.

101 {}
102 ) {
103 return std::make_unique<template_formatter>(template_pattern, opts);
104 }

◆ has_type()

static bool kcenon::logger::formatter_factory::has_type ( const std::string & name)
inlinestatic

Check if a formatter type is registered.

Parameters
nameType name to check
Returns
true if registered

Definition at line 224 of file formatter_factory.h.

224 {
225 return registry().find(name) != registry().end();
226 }

References registry().

Here is the call graph for this function:

◆ register_type()

static void kcenon::logger::formatter_factory::register_type ( const std::string & name,
creator_fn creator )
inlinestatic

Register a custom formatter type.

Parameters
nameType name for the formatter
creatorFactory function to create the formatter

Definition at line 202 of file formatter_factory.h.

202 {
203 registry()[name] = std::move(creator);
204 }

References registry().

Here is the call graph for this function:

◆ registry()

static std::unordered_map< std::string, creator_fn > & kcenon::logger::formatter_factory::registry ( )
inlinestaticprivate

Definition at line 229 of file formatter_factory.h.

229 {
230 static std::unordered_map<std::string, creator_fn> instance;
231 return instance;
232 }

Referenced by create(), has_type(), and register_type().

Here is the caller graph for this function:

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