File indexing completed on 2025-07-09 08:47:31
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 public:
0031 mongo_sink(const std::string &db_name,
0032 const std::string &collection_name,
0033 const std::string &uri = "mongodb://localhost:27017") try
0034 : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri) {
0035 } catch (const std::exception &e) {
0036 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
0037 }
0038
0039 mongo_sink(std::shared_ptr<mongocxx::instance> instance,
0040 const std::string &db_name,
0041 const std::string &collection_name,
0042 const std::string &uri = "mongodb://localhost:27017")
0043 : instance_(std::move(instance)),
0044 db_name_(db_name),
0045 coll_name_(collection_name) {
0046 try {
0047 client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
0048 } catch (const std::exception &e) {
0049 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
0050 }
0051 }
0052
0053 ~mongo_sink() { flush_(); }
0054
0055 protected:
0056 void sink_it_(const details::log_msg &msg) override {
0057 using bsoncxx::builder::stream::document;
0058 using bsoncxx::builder::stream::finalize;
0059
0060 if (client_ != nullptr) {
0061 auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level"
0062 << level::to_string_view(msg.level).data() << "level_num"
0063 << msg.level << "message"
0064 << std::string(msg.payload.begin(), msg.payload.end())
0065 << "logger_name"
0066 << std::string(msg.logger_name.begin(), msg.logger_name.end())
0067 << "thread_id" << static_cast<int>(msg.thread_id) << finalize;
0068 client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
0069 }
0070 }
0071
0072 void flush_() override {}
0073
0074 private:
0075 std::shared_ptr<mongocxx::instance> instance_;
0076 std::string db_name_;
0077 std::string coll_name_;
0078 std::unique_ptr<mongocxx::client> client_ = nullptr;
0079 };
0080
0081 #include "spdlog/details/null_mutex.h"
0082 #include <mutex>
0083 using mongo_sink_mt = mongo_sink<std::mutex>;
0084 using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
0085
0086 }
0087
0088 template <typename Factory = spdlog::synchronous_factory>
0089 inline std::shared_ptr<logger> mongo_logger_mt(
0090 const std::string &logger_name,
0091 const std::string &db_name,
0092 const std::string &collection_name,
0093 const std::string &uri = "mongodb://localhost:27017") {
0094 return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name,
0095 uri);
0096 }
0097
0098 template <typename Factory = spdlog::synchronous_factory>
0099 inline std::shared_ptr<logger> mongo_logger_st(
0100 const std::string &logger_name,
0101 const std::string &db_name,
0102 const std::string &collection_name,
0103 const std::string &uri = "mongodb://localhost:27017") {
0104 return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name,
0105 uri);
0106 }
0107
0108 }