spdlog
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 {
27namespace sinks {
28template<typename Mutex>
29class mongo_sink : public base_sink<Mutex>
30{
31public:
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
51protected:
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
69private:
70 static mongocxx::instance instance_;
74};
75mongocxx::instance mongo_sink<std::mutex>::instance_{};
76
78#include <mutex>
81
82} // namespace sinks
83
84template<typename Factory = spdlog::synchronous_factory>
85inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name, const std::string &db_name,
86 const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
87{
88 return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
89}
90
91template<typename Factory = spdlog::synchronous_factory>
92inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name, const std::string &db_name,
93 const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
94{
95 return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
96}
97
98} // namespace spdlog
static mongocxx::instance instance_
Definition mongo_sink.h:70
void flush_() override
Definition mongo_sink.h:67
mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri="mongodb://localhost:27017")
Definition mongo_sink.h:32
void sink_it_(const details::log_msg &msg) override
Definition mongo_sink.h:52
std::unique_ptr< mongocxx::client > client_
Definition mongo_sink.h:73
SPDLOG_INLINE const string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition common-inl.h:23
Definition async.h:25
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:85
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:92
log_clock::time_point time
Definition log_msg.h:22
string_view_t payload
Definition log_msg.h:30
level::level_enum level
Definition log_msg.h:21
string_view_t logger_name
Definition log_msg.h:20