File indexing completed on 2024-11-15 09:58:15
0001
0002
0003
0004 #pragma once
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "spdlog/common.h"
0014 #include "spdlog/details/log_msg.h"
0015 #include "spdlog/sinks/base_sink.h"
0016 #include <spdlog/details/synchronous_factory.h>
0017
0018 #include <bsoncxx/builder/stream/document.hpp>
0019 #include <bsoncxx/types.hpp>
0020 #include <bsoncxx/view_or_value.hpp>
0021
0022 #include <mongocxx/client.hpp>
0023 #include <mongocxx/instance.hpp>
0024 #include <mongocxx/uri.hpp>
0025
0026 namespace spdlog {
0027 namespace sinks {
0028 template<typename Mutex>
0029 class mongo_sink : public base_sink<Mutex>
0030 {
0031 public:
0032 mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
0033 try : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri)
0034 {}
0035 catch (const std::exception &e)
0036 {
0037 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
0038 }
0039
0040 mongo_sink(std::shared_ptr<mongocxx::instance> instance, const std::string &db_name, const std::string &collection_name,
0041 const std::string &uri = "mongodb://localhost:27017")
0042 : instance_(std::move(instance))
0043 , db_name_(db_name)
0044 , coll_name_(collection_name)
0045 {
0046 try
0047 {
0048 client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
0049 }
0050 catch (const std::exception &e)
0051 {
0052 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
0053 }
0054 }
0055
0056 ~mongo_sink()
0057 {
0058 flush_();
0059 }
0060
0061 protected:
0062 void sink_it_(const details::log_msg &msg) override
0063 {
0064 using bsoncxx::builder::stream::document;
0065 using bsoncxx::builder::stream::finalize;
0066
0067 if (client_ != nullptr)
0068 {
0069 auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data()
0070 << "level_num" << msg.level << "message" << std::string(msg.payload.begin(), msg.payload.end())
0071 << "logger_name" << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
0072 << static_cast<int>(msg.thread_id) << finalize;
0073 client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
0074 }
0075 }
0076
0077 void flush_() override {}
0078
0079 private:
0080 std::shared_ptr<mongocxx::instance> instance_;
0081 std::string db_name_;
0082 std::string coll_name_;
0083 std::unique_ptr<mongocxx::client> client_ = nullptr;
0084 };
0085
0086 #include "spdlog/details/null_mutex.h"
0087 #include <mutex>
0088 using mongo_sink_mt = mongo_sink<std::mutex>;
0089 using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
0090
0091 }
0092
0093 template<typename Factory = spdlog::synchronous_factory>
0094 inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name, const std::string &db_name,
0095 const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
0096 {
0097 return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
0098 }
0099
0100 template<typename Factory = spdlog::synchronous_factory>
0101 inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name, const std::string &db_name,
0102 const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
0103 {
0104 return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
0105 }
0106
0107 }