LogIt++
Loading...
Searching...
No Matches
logit::SimpleLogFormatter Class Reference

A simple log formatter that formats log messages based on a user-defined pattern. More...

#include <SimpleLogFormatter.hpp>

Inheritance diagram for logit::SimpleLogFormatter:
logit::ILogFormatter

Classes

struct  Config
 Configuration for the log formatter. More...
 

Public Member Functions

 SimpleLogFormatter ()
 Default constructor that uses the default log pattern.
 
 SimpleLogFormatter (const std::string &pattern, const bool json_format=false)
 Constructor that accepts a custom log pattern.
 
void set_pattern (const std::string &pattern, bool json_format=false)
 Sets a custom pattern for log formatting or switches to JSON formatting.
 
std::string format (const LogRecord &record) const override
 Formats a log record according to the current pattern or as a JSON string.
 
- Public Member Functions inherited from logit::ILogFormatter
virtual ~ILogFormatter ()=default
 

Private Member Functions

void compile_pattern ()
 Compiles the log format pattern into instructions.
 
std::string format_as_pattern (const LogRecord &record) const
 Formats a log record according to the compiled pattern.
 
std::string format_as_json (const LogRecord &record) const
 Formats a log record as a JSON string without external JSON libraries.
 
std::string escape_json_string (const std::string &input) const
 Helper function to escape special characters in JSON strings.
 
std::string thread_id_to_string (const std::thread::id &thread_id) const
 Helper function to convert a thread ID to a string.
 

Private Attributes

Config m_config
 Formatter configuration holding the log format pattern.
 
std::vector< FormatInstructionm_compiled_instructions
 Compiled instructions from the format pattern.
 

Detailed Description

A simple log formatter that formats log messages based on a user-defined pattern.

The SimpleLogFormatter class allows users to define a pattern for formatting log messages. The pattern supports various placeholders for timestamps, log levels, and other log-related information.

Definition at line 17 of file SimpleLogFormatter.hpp.

Constructor & Destructor Documentation

◆ SimpleLogFormatter() [1/2]

logit::SimpleLogFormatter::SimpleLogFormatter ( )
inline

Default constructor that uses the default log pattern.

Definition at line 27 of file SimpleLogFormatter.hpp.

◆ SimpleLogFormatter() [2/2]

logit::SimpleLogFormatter::SimpleLogFormatter ( const std::string & pattern,
const bool json_format = false )
inline

Constructor that accepts a custom log pattern.

Parameters
patternThe custom format pattern for log messages.
json_formatFlag to enable JSON formatting.

Definition at line 34 of file SimpleLogFormatter.hpp.

Member Function Documentation

◆ compile_pattern()

void logit::SimpleLogFormatter::compile_pattern ( )
inlineprivate

Compiles the log format pattern into instructions.

This method compiles the format string into a series of instructions that are applied when formatting log messages.

Definition at line 74 of file SimpleLogFormatter.hpp.

◆ escape_json_string()

std::string logit::SimpleLogFormatter::escape_json_string ( const std::string & input) const
inlineprivate

Helper function to escape special characters in JSON strings.

This function replaces special characters like quotes, backslashes, and control characters with their escaped versions for proper JSON formatting.

Parameters
inputThe input string that may contain special characters.
Returns
A properly escaped JSON string.

Definition at line 131 of file SimpleLogFormatter.hpp.

◆ format()

std::string logit::SimpleLogFormatter::format ( const LogRecord & record) const
inlineoverridevirtual

Formats a log record according to the current pattern or as a JSON string.

This method formats the log message either by applying the compiled pattern instructions or by creating a JSON string if the json_format flag is enabled.

Parameters
recordThe log record containing log information.
Returns
A formatted string representing the log message.

Implements logit::ILogFormatter.

Definition at line 58 of file SimpleLogFormatter.hpp.

◆ format_as_json()

std::string logit::SimpleLogFormatter::format_as_json ( const LogRecord & record) const
inlineprivate

Formats a log record as a JSON string without external JSON libraries.

This method manually constructs the JSON string using string streams and simple concatenation.

Parameters
recordThe log record containing log information.
Returns
A JSON string representing the log message.

Definition at line 100 of file SimpleLogFormatter.hpp.

◆ format_as_pattern()

std::string logit::SimpleLogFormatter::format_as_pattern ( const LogRecord & record) const
inlineprivate

Formats a log record according to the compiled pattern.

This method formats the log message by applying the compiled pattern instructions to the log record.

Parameters
recordThe log record containing log information.
Returns
A formatted string representing the log message.

Definition at line 85 of file SimpleLogFormatter.hpp.

◆ set_pattern()

void logit::SimpleLogFormatter::set_pattern ( const std::string & pattern,
bool json_format = false )
inline

Sets a custom pattern for log formatting or switches to JSON formatting.

This method allows changing the format of log messages at runtime. It updates the internal configuration and recompiles the pattern or switches to JSON formatting.

Parameters
patternA string containing the custom format pattern.
json_formatFlag to enable JSON formatting.

Definition at line 45 of file SimpleLogFormatter.hpp.

◆ thread_id_to_string()

std::string logit::SimpleLogFormatter::thread_id_to_string ( const std::thread::id & thread_id) const
inlineprivate

Helper function to convert a thread ID to a string.

Converts the thread ID to a string format that can be used in the JSON output.

Parameters
thread_idThe thread ID to be converted.
Returns
A string representation of the thread ID.

Definition at line 160 of file SimpleLogFormatter.hpp.

Member Data Documentation

◆ m_compiled_instructions

std::vector<FormatInstruction> logit::SimpleLogFormatter::m_compiled_instructions
private

Compiled instructions from the format pattern.

Definition at line 68 of file SimpleLogFormatter.hpp.

◆ m_config

Config logit::SimpleLogFormatter::m_config
private

Formatter configuration holding the log format pattern.

Definition at line 67 of file SimpleLogFormatter.hpp.


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