63#include <kcenon/common/interfaces/logger_interface.h>
93 "[{timestamp}] [{level}] [{thread_id}] {message}";
132 std::ostringstream oss;
135 if (segment.is_placeholder) {
138 oss << segment.content;
151 [[nodiscard]] std::string
get_name()
const override {
152 return "template_formatter";
204 size_t len =
template_.length();
207 size_t start =
template_.find(
'{', pos);
209 if (start == std::string::npos) {
223 size_t end =
template_.find(
'}', start);
224 if (end == std::string::npos) {
231 std::string placeholder =
template_.substr(start + 1, end - start - 1);
234 size_t colon_pos = placeholder.find(
':');
235 if (colon_pos != std::string::npos) {
236 std::string width_str = placeholder.substr(colon_pos + 1);
237 placeholder = placeholder.substr(0, colon_pos);
239 width = std::stoi(width_str);
245 segments_.emplace_back(placeholder,
true, width);
258 const std::string& name,
264 if (name ==
"timestamp") {
266 }
else if (name ==
"timestamp_local") {
268 }
else if (name ==
"level") {
277 }
else if (name ==
"level_lower") {
281 }
else if (name ==
"message") {
283 }
else if (name ==
"thread_id") {
287 }
else if (name ==
"file") {
289 value = entry.
location->file.to_string();
291 }
else if (name ==
"filename") {
297 }
else if (name ==
"line") {
299 value = std::to_string(entry.
location->line);
301 }
else if (name ==
"function") {
303 value = entry.
location->function.to_string();
305 }
else if (name ==
"category") {
307 value = entry.
category->to_string();
309 }
else if (name ==
"trace_id") {
313 }
else if (name ==
"span_id") {
320 auto it = entry.
fields->find(name);
321 if (it != entry.
fields->end()) {
328 if (width > 0 && !value.empty()) {
331 if (display_len <
static_cast<size_t>(width)) {
332 value += std::string(width - display_len,
' ');
345 return std::visit([](
const auto& v) -> std::string {
346 using T = std::decay_t<
decltype(v)>;
347 if constexpr (std::is_same_v<T, std::string>) {
349 }
else if constexpr (std::is_same_v<T, bool>) {
350 return v ?
"true" :
"false";
351 }
else if constexpr (std::is_same_v<T, int64_t>) {
352 return std::to_string(v);
353 }
else if constexpr (std::is_same_v<T, double>) {
354 std::ostringstream oss;
355 oss << std::fixed << std::setprecision(6) << v;
358 return std::to_string(v);
370 bool in_escape =
false;
372 for (
size_t i = 0; i < str.length(); ++i) {
373 if (str[i] ==
'\033') {
375 }
else if (in_escape) {
std::string to_string() const
Convert to std::string.
static std::string level_to_color(log_level level, bool use_colors=true)
Convert log level to ANSI color code.
static std::string level_to_string(log_level level)
Convert log level to human-readable string.
static const char * color_reset()
ANSI color reset sequence.
static std::string to_lower(const std::string &str)
Convert string to lowercase.
static std::string extract_filename(const std::string &file_path)
Extract filename from full file path.
static std::string format_timestamp(const std::chrono::system_clock::time_point &tp)
Format timestamp to human-readable format (YYYY-MM-DD HH:MM:SS.mmm)
static std::string format_iso8601(const std::chrono::system_clock::time_point &tp)
Format timestamp to ISO 8601 / RFC 3339 format with UTC timezone.
Data structures for representing log entries and source locations kcenon.
std::variant< std::string, int64_t, double, bool > log_value
Value type for structured logging fields.
String utility functions for log formatting and conversion.
Represents a single log entry with all associated metadata.
std::optional< source_location > location
Optional source code location information.
std::optional< log_fields > fields
Optional structured fields for key-value logging.
std::optional< small_string_64 > thread_id
Optional thread identifier.
std::optional< otlp::otel_context > otel_ctx
Optional OpenTelemetry context for trace correlation.
log_level level
Severity level of the log message.
std::optional< small_string_128 > category
Optional category for log filtering and routing.
small_string_256 message
The actual log message.
std::chrono::system_clock::time_point timestamp
Timestamp when the log entry was created.
Time utility functions for timestamp formatting.