Photon 1.0.0
Loading...
Searching...
No Matches
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"
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
26namespace spdlog
27{
28 namespace sinks
29 {
30 template <typename Mutex>
31 class mongo_sink : public base_sink<Mutex>
32 {
33 public:
34 mongo_sink(const std::string& db_name, const std::string& collection_name, const std::string& uri = "mongodb://localhost:27017")
35 try : mongo_sink(std::make_shared<mongocxx::instance>(), db_name, collection_name, uri)
36 {
37 }
38 catch (const std::exception& e)
39 {
40 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
41 }
42
43 mongo_sink(std::shared_ptr<mongocxx::instance> instance, const std::string& db_name, const std::string& collection_name, const std::string& uri = "mongodb://localhost:27017")
44 : instance_(std::move(instance)), db_name_(db_name), coll_name_(collection_name)
45 {
46 try
47 {
48 client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
49 }
50 catch (const std::exception& e)
51 {
52 throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what()));
53 }
54 }
55
57 {
58 flush_();
59 }
60
61 protected:
62 void sink_it_(const details::log_msg& msg) override
63 {
64 using bsoncxx::builder::stream::document;
65 using bsoncxx::builder::stream::finalize;
66
67 if (client_ != nullptr)
68 {
69 auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data()
70 << "level_num" << msg.level << "message" << std::string(msg.payload.begin(), msg.payload.end())
71 << "logger_name" << std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
72 << static_cast<int>(msg.thread_id) << finalize;
73 client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
74 }
75 }
76
77 void flush_() override
78 {
79 }
80
81 private:
82 std::shared_ptr<mongocxx::instance> instance_;
83 std::string db_name_;
84 std::string coll_name_;
85 std::unique_ptr<mongocxx::client> client_ = nullptr;
86 };
87
89#include <mutex>
92
93 } // namespace sinks
94
95 template <typename Factory = spdlog::synchronous_factory>
96 inline 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")
97 {
98 return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
99 }
100
101 template <typename Factory = spdlog::synchronous_factory>
102 inline 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")
103 {
104 return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
105 }
106
107} // namespace spdlog
Definition base_sink.h:22
Definition mongo_sink.h:32
std::string coll_name_
Definition mongo_sink.h:84
mongo_sink(std::shared_ptr< mongocxx::instance > instance, const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition mongo_sink.h:43
void flush_() override
Definition mongo_sink.h:77
std::shared_ptr< mongocxx::instance > instance_
Definition mongo_sink.h:82
~mongo_sink()
Definition mongo_sink.h:56
mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition mongo_sink.h:34
std::string db_name_
Definition mongo_sink.h:83
void sink_it_(const details::log_msg &msg) override
Definition mongo_sink.h:62
std::unique_ptr< mongocxx::client > client_
Definition mongo_sink.h:85
SPDLOG_INLINE const string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition common-inl.h:25
mongo_sink< std::mutex > mongo_sink_mt
Definition mongo_sink.h:90
mongo_sink< spdlog::details::null_mutex > mongo_sink_st
Definition mongo_sink.h:91
Definition async.h:26
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:96
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
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:102
Definition uuid.h:926
Definition log_msg.h:14
log_clock::time_point time
Definition log_msg.h:24
string_view_t payload
Definition log_msg.h:32
level::level_enum level
Definition log_msg.h:23
string_view_t logger_name
Definition log_msg.h:22
size_t thread_id
Definition log_msg.h:25