Repo-Init
 
Loading...
Searching...
No Matches
spdlog::sinks::loki_api_sink< Mutex > Class Template Reference

#include <Loki.hpp>

Inheritance diagram for spdlog::sinks::loki_api_sink< Mutex >:
Collaboration diagram for spdlog::sinks::loki_api_sink< Mutex >:

Classes

struct  logInfo_t
 

Public Member Functions

 loki_api_sink (const std::string &lokiAddress)
 
 ~loki_api_sink ()
 

Protected Member Functions

void sink_it_ (const details::log_msg &msg) override
 
void flush_ () override
 

Private Attributes

bool _lokiAvailable {false}
 
std::unique_ptr< HTTP_connHandler
 
std::string _basicInformation
 
std::vector< struct logInfo_t_internalLogBuffer
 

Detailed Description

template<typename Mutex>
class spdlog::sinks::loki_api_sink< Mutex >

A custom sink for spdlog that sends log messages to a Loki server.

The loki_api_sink class is a custom sink for spdlog that sends log messages to a Loki server. It provides functionality to send log messages to a specified Loki server address. The log messages are sent using HTTP requests.

Template Parameters
MutexThe type of mutex to use for thread-safety.

Definition at line 24 of file Loki.hpp.

Constructor & Destructor Documentation

◆ loki_api_sink()

template<typename Mutex>
spdlog::sinks::loki_api_sink< Mutex >::loki_api_sink ( const std::string & lokiAddress)
explicit

Constructs a loki_api_sink object with the specified Loki server address.

Parameters
lokiAddressThe address of the Loki server to send log messages to.

Definition at line 20 of file Loki.cpp.

21 {
22 if (lokiAddress.empty())
23 {
24 return;
25 }
26
28
29 // Pre-allocate buffers
35
36 // Prepare information (Loki limits maximum number of labels with 15)
38 _basicInformation += std::string(R"("release_version":"v)") + PROJECT_FULL_REVISION + "\",";
39 _basicInformation += std::string(R"("release_date":")") + PROJECT_BUILD_DATE + " " + PROJECT_BUILD_TIME + "\",";
40 _basicInformation += std::string(R"("compiler_name":")") + COMPILER_NAME + "\",";
41 _basicInformation += std::string(R"("compiler_version":")") + COMPILER_VERSION + "\",";
42 _basicInformation += std::string(R"("build":")") + BUILD_TYPE + "\",";
43
44 // Parse hostname
47 _basicInformation += std::string(R"("hostname":")") + hostBuffer.data() + "\",";
48
49 // Parse CPU information
50 const std::filesystem::path cpuInfoPath = "/proc/cpuinfo";
52
53 findFromFile(cpuInfoPath, "^siblings", word);
54 _basicInformation += std::string(R"("cpu_threadcount":")") + word + "\",";
55 findFromFile(cpuInfoPath, "^(cpu cores)", word);
56 _basicInformation += std::string(R"("cpu_corecount":")") + word + "\",";
57 findFromFile(cpuInfoPath, "^(model name)", word);
58 _basicInformation += std::string(R"("cpu_model":")") + word + "\",";
59 findFromFile(cpuInfoPath, "^vendor_id", word);
60 _basicInformation += std::string(R"("cpu_vendorid":")") + word + "\",";
61
62 _lokiAvailable = true;
63 }
std::vector< std::string > findFromFile(const std::filesystem::path &filePath, const std::string &pattern, std::string &lastWord)
std::unique_ptr< HTTP > _connHandler
Definition Loki.hpp:59
std::vector< struct logInfo_t > _internalLogBuffer
Definition Loki.hpp:66
std::string _basicInformation
Definition Loki.hpp:60

◆ ~loki_api_sink()

template<typename Mutex>
spdlog::sinks::loki_api_sink< Mutex >::~loki_api_sink ( )
default

Destroys the loki_api_sink object.

Member Function Documentation

◆ flush_()

template<typename Mutex>
void spdlog::sinks::loki_api_sink< Mutex >::flush_ ( )
overrideprotected

Flushes any buffered log messages.

This function is called by spdlog to flush any buffered log messages. It is overridden from the base_sink class.

Definition at line 82 of file Loki.cpp.

83 {
84 bool flag = false;
85
86 // Prepare JSON
88
89 sStream << "{\"streams\":[";
90 for (auto &entry : _internalLogBuffer)
91 {
92 if (entry.logs.empty())
93 {
94 continue;
95 }
96
97 if (flag)
98 {
99 sStream << ",";
100 }
101 flag = true;
102 sStream << "{\"stream\":{" << _basicInformation + R"("level":")" << entry.level << R"("},"values":[)";
103
104 bool subflag = false;
105 for (const auto &subentry : entry.logs)
106 {
107 if (subflag)
108 {
109 sStream << ",";
110 }
111 subflag = true;
112 sStream << "[\"" << subentry.first << "\",\"" << subentry.second << "\"]";
113 }
114 sStream << "]}";
115
116 entry.logs.clear();
117 }
118 sStream << "]}";
119
120 if (flag)
121 {
122 // Send request
125 _connHandler->sendPOSTRequest("/loki/api/v1/push", sStream.str(), recvData, replyCode);
126 }
127 }

◆ sink_it_()

template<typename Mutex>
void spdlog::sinks::loki_api_sink< Mutex >::sink_it_ ( const details::log_msg & msg)
overrideprotected

Sends the log message to the Loki server.

This function is called by spdlog to send the log message to the Loki server. It is overridden from the base_sink class.

Parameters
msgThe log message to be sent.

Definition at line 67 of file Loki.cpp.

68 {
69 if (!_lokiAvailable)
70 {
71 return;
72 }
73
74 if (msg.level >= spdlog::level::debug && msg.level <= spdlog::level::critical)
75 {
76 _internalLogBuffer[static_cast<size_t>(msg.level) - 1].logs.push_back(
77 {std::to_string(msg.time.time_since_epoch().count()),
78 std::string(msg.payload.data(), msg.payload.size())});
79 }
80 }

Member Data Documentation

◆ _basicInformation

template<typename Mutex>
std::string spdlog::sinks::loki_api_sink< Mutex >::_basicInformation
private

Definition at line 60 of file Loki.hpp.

◆ _connHandler

template<typename Mutex>
std::unique_ptr<HTTP> spdlog::sinks::loki_api_sink< Mutex >::_connHandler
private

Definition at line 59 of file Loki.hpp.

◆ _internalLogBuffer

template<typename Mutex>
std::vector<struct logInfo_t> spdlog::sinks::loki_api_sink< Mutex >::_internalLogBuffer
private

Definition at line 66 of file Loki.hpp.

◆ _lokiAvailable

template<typename Mutex>
bool spdlog::sinks::loki_api_sink< Mutex >::_lokiAvailable {false}
private

Definition at line 58 of file Loki.hpp.

58{false};

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