Compare commits

...

11 Commits

Author SHA1 Message Date
Gabi Melman 036cc5d575
Merge pull request #1982 from mguludag/v1.x
ignore pattern formatting
2021-06-27 23:11:39 +03:00
Muhammed Galib Uludag 14950926ed
ignore pattern formatting for message section in mongodb 2021-06-27 22:50:31 +03:00
Muhammed Galib Uludag baa3b1a07e
Merge branch 'gabime:v1.x' into v1.x 2021-06-27 20:32:52 +03:00
Gabi Melman 2a09f66a44
Remove un needed functions and added override keyword 2021-06-27 20:32:01 +03:00
Muhammed Galib Uludag e50b62c770
suppressed unused var 2021-06-27 20:30:41 +03:00
Gabi Melman 13d8b0f17f
Merge pull request #1981 from mguludag/v1.x
added mongodb sink
2021-06-27 20:30:08 +03:00
Muhammed Galib Uludag 9e0c658b29
factory functions namespace fix 2021-06-27 20:17:19 +03:00
Muhammed Galib Uludag 74fec56927
Changed base class to base_sink and added factory functions 2021-06-27 19:59:07 +03:00
Muhammed Galib Uludag 514f304a47
changed license template for compability to other spdlog headers 2021-06-27 18:06:44 +03:00
Muhammed Galib Uludag 7f85a5c988
change license to mit 2021-06-27 17:53:19 +03:00
Muhammed Galib Uludag 14d626d961
added mongodb sink 2021-06-27 16:16:39 +03:00
1 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,96 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
//
// Custom sink for mongodb
// Building and using requires mongocxx library.
// For building mongocxx library check the url below
// http://mongocxx.org/mongocxx-v3/installation/
//
#include "spdlog/common.h"
#include "spdlog/details/log_msg.h"
#include "spdlog/sinks/base_sink.h"
#include <spdlog/details/synchronous_factory.h>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/view_or_value.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
namespace spdlog {
namespace sinks {
template <typename Mutex> class mongo_sink : public base_sink<Mutex> {
public:
mongo_sink(const std::string &db_name, const std::string &collection_name,
const std::string &uri = "mongodb://localhost:27017") {
try {
client_ = std::make_unique<mongocxx::client>(mongocxx::uri{uri});
db_name_ = db_name;
coll_name_ = collection_name;
} catch (const std::exception) {
throw spdlog_ex("Error opening database");
}
}
~mongo_sink() { flush_(); }
protected:
void sink_it_(const details::log_msg &msg) override {
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
if (client_ != nullptr) {
auto doc = document{}
<< "timestamp" << bsoncxx::types::b_date(msg.time) << "level"
<< level::to_string_view(msg.level).data() << "message"
<< std::string(msg.payload.begin(), msg.payload.end())
<< "logger_name"
<< std::string(msg.logger_name.begin(), msg.logger_name.end())
<< "thread_id" << static_cast<int>(msg.thread_id) << finalize;
client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
}
}
void flush_() override {}
private:
static mongocxx::instance instance_;
std::string db_name_;
std::string coll_name_;
std::unique_ptr<mongocxx::client> client_ = nullptr;
};
mongocxx::instance mongo_sink<std::mutex>::instance_{};
#include "spdlog/details/null_mutex.h"
#include <mutex>
using mongo_sink_mt = mongo_sink<std::mutex>;
using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
} // namespace sinks
template <typename Factory = spdlog::synchronous_factory>
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") {
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name,
collection_name, uri);
}
template <typename Factory = spdlog::synchronous_factory>
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") {
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name,
collection_name, uri);
}
} // namespace spdlog