![]() |
LogIt++
|
A simple log formatter that formats log messages based on a user-defined pattern. More...
#include <SimpleLogFormatter.hpp>
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. | |
void | set_timestamp_offset (int64_t offset_ms) override |
Sets the timestamp offset for log formatting. | |
std::string | format (const LogRecord &record) const override |
Formats a log record according to the current pattern or as a JSON string. | |
![]() | |
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< FormatInstruction > | m_compiled_instructions |
Compiled instructions from the format pattern. | |
std::atomic< int64_t > | m_offset_ms = ATOMIC_VAR_INIT(0) |
Timestamp offset in milliseconds. | |
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.
|
inline |
Default constructor that uses the default log pattern.
Definition at line 27 of file SimpleLogFormatter.hpp.
|
inline |
Constructor that accepts a custom log pattern.
pattern | The custom format pattern for log messages. |
json_format | Flag to enable JSON formatting. |
Definition at line 34 of file SimpleLogFormatter.hpp.
|
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 85 of file SimpleLogFormatter.hpp.
|
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.
input | The input string that may contain special characters. |
Definition at line 142 of file SimpleLogFormatter.hpp.
|
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.
record | The log record containing log information. |
Implements logit::ILogFormatter.
Definition at line 68 of file SimpleLogFormatter.hpp.
|
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.
record | The log record containing log information. |
Definition at line 111 of file SimpleLogFormatter.hpp.
|
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.
record | The log record containing log information. |
Definition at line 96 of file SimpleLogFormatter.hpp.
|
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.
pattern | A string containing the custom format pattern. |
json_format | Flag to enable JSON formatting. |
Definition at line 45 of file SimpleLogFormatter.hpp.
|
inlineoverridevirtual |
Sets the timestamp offset for log formatting.
This function allows setting a timezone offset in milliseconds, which will be used for adjusting timestamps in formatted log messages.
offset_ms | Timezone offset in milliseconds. |
Implements logit::ILogFormatter.
Definition at line 57 of file SimpleLogFormatter.hpp.
|
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.
thread_id | The thread ID to be converted. |
Definition at line 171 of file SimpleLogFormatter.hpp.
|
private |
Compiled instructions from the format pattern.
Definition at line 78 of file SimpleLogFormatter.hpp.
|
private |
Formatter configuration holding the log format pattern.
Definition at line 77 of file SimpleLogFormatter.hpp.
|
private |
Timestamp offset in milliseconds.
Definition at line 79 of file SimpleLogFormatter.hpp.