Agora  1.2.0
Agora project
mongo_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 //
7 // Custom sink for mongodb
8 // Building and using requires mongocxx library.
9 // For building mongocxx library check the url below
10 // http://mongocxx.org/mongocxx-v3/installation/
11 //
12 
13 #include "spdlog/common.h"
14 #include "spdlog/details/log_msg.h"
15 #include "spdlog/sinks/base_sink.h"
17 
18 #include <bsoncxx/builder/stream/document.hpp>
19 #include <bsoncxx/types.hpp>
20 #include <bsoncxx/view_or_value.hpp>
21 
22 #include <mongocxx/client.hpp>
23 #include <mongocxx/instance.hpp>
24 #include <mongocxx/uri.hpp>
25 
26 namespace spdlog {
27 namespace sinks {
28 template<typename Mutex>
29 class mongo_sink : public base_sink<Mutex>
30 {
31 public:
32  mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
33  {
34  try
35  {
36  client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
37  db_name_ = db_name;
38  coll_name_ = collection_name;
39  }
40  catch (const std::exception)
41  {
42  throw spdlog_ex("Error opening database");
43  }
44  }
45 
47  {
48  flush_();
49  }
50 
51 protected:
52  void sink_it_(const details::log_msg &msg) override
53  {
54  using bsoncxx::builder::stream::document;
55  using bsoncxx::builder::stream::finalize;
56 
57  if (client_ != nullptr)
58  {
59  auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data()
60  << "message" << std::string(msg.payload.begin(), msg.payload.end()) << "logger_name"
61  << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
62  << static_cast<int>(msg.thread_id) << finalize;
63  client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
64  }
65  }
66 
67  void flush_() override {}
68 
69 private:
70  static mongocxx::instance instance_;
71  std::string db_name_;
72  std::string coll_name_;
73  std::unique_ptr<mongocxx::client> client_ = nullptr;
74 };
75 template<>
76 mongocxx::instance mongo_sink<std::mutex>::instance_{};
77 
79 #include <mutex>
82 
83 } // namespace sinks
84 
85 template<typename Factory = spdlog::synchronous_factory>
86 inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name, const std::string &db_name,
87  const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
88 {
89  return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
90 }
91 
92 template<typename Factory = spdlog::synchronous_factory>
93 inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name, const std::string &db_name,
94  const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
95 {
96  return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
97 }
98 
99 } // namespace spdlog
spdlog::level::to_string_view
const SPDLOG_INLINE string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common-inl.h:23
base_sink.h
spdlog::sinks::mongo_sink::~mongo_sink
~mongo_sink()
Definition: mongo_sink.h:46
spdlog::details::log_msg::payload
string_view_t payload
Definition: log_msg.h:30
synchronous_factory.h
spdlog::sinks::mongo_sink::instance_
static mongocxx::instance instance_
Definition: mongo_sink.h:70
spdlog::mongo_logger_mt
std::shared_ptr< logger > mongo_logger_mt(const std::string &logger_name, const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition: mongo_sink.h:86
spdlog::sinks::mongo_sink::db_name_
std::string db_name_
Definition: mongo_sink.h:71
null_mutex.h
spdlog::mongo_logger_st
std::shared_ptr< logger > mongo_logger_st(const std::string &logger_name, const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition: mongo_sink.h:93
spdlog
Definition: async.h:25
spdlog::sinks::mongo_sink::flush_
void flush_() override
Definition: mongo_sink.h:67
spdlog::sinks::base_sink
Definition: base_sink.h:19
spdlog::sinks::mongo_sink::mongo_sink
mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition: mongo_sink.h:32
spdlog::sinks::mongo_sink::coll_name_
std::string coll_name_
Definition: mongo_sink.h:72
fmt::v8::basic_string_view::end
constexpr auto end() const -> iterator
Definition: core.h:498
spdlog::details::log_msg::logger_name
string_view_t logger_name
Definition: log_msg.h:20
spdlog::details::log_msg::thread_id
size_t thread_id
Definition: log_msg.h:23
spdlog::sinks::mongo_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: mongo_sink.h:52
spdlog::sinks::mongo_sink
Definition: mongo_sink.h:29
fmt::v8::basic_string_view::begin
constexpr auto begin() const -> iterator
Definition: core.h:497
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
spdlog::spdlog_ex
Definition: common.h:276
fmt::v8::basic_string_view::data
constexpr auto data() const -> const Char *
Definition: core.h:492
common.h
spdlog::details::log_msg
Definition: log_msg.h:11
log_msg.h
spdlog::sinks::mongo_sink::client_
std::unique_ptr< mongocxx::client > client_
Definition: mongo_sink.h:73
spdlog::details::log_msg::time
log_clock::time_point time
Definition: log_msg.h:22