mirror of https://github.com/gabime/spdlog.git
Merge branch 'gabime:v1.x' into v1.x
commit
a406f27c65
@ -0,0 +1,133 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
//
|
||||
// Custom sink for kafka
|
||||
// Building and using requires librdkafka library.
|
||||
// For building librdkafka library check the url below
|
||||
// https://github.com/confluentinc/librdkafka
|
||||
//
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include "spdlog/details/log_msg.h"
|
||||
#include "spdlog/sinks/base_sink.h"
|
||||
#include "spdlog/details/synchronous_factory.h"
|
||||
#include "spdlog/details/null_mutex.h"
|
||||
#include "spdlog/async.h"
|
||||
#include <mutex>
|
||||
|
||||
// kafka header
|
||||
#include <librdkafka/rdkafkacpp.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
||||
struct kafka_sink_config
|
||||
{
|
||||
std::string server_addr;
|
||||
std::string produce_topic;
|
||||
int32_t flush_timeout_ms = 1000;
|
||||
|
||||
kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000)
|
||||
: server_addr{std::move(addr)}
|
||||
,produce_topic{std::move(topic)}
|
||||
,flush_timeout_ms(flush_timeout_ms)
|
||||
{}
|
||||
};
|
||||
|
||||
template<typename Mutex>
|
||||
class kafka_sink : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
kafka_sink(kafka_sink_config config)
|
||||
: config_{std::move(config)}
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string errstr;
|
||||
conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL));
|
||||
RdKafka::Conf::ConfResult confRes = conf_->set("bootstrap.servers", config_.server_addr, errstr);
|
||||
if (confRes != RdKafka::Conf::CONF_OK)
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr));
|
||||
}
|
||||
|
||||
tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC));
|
||||
if (tconf_ == nullptr)
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("create topic config failed"));
|
||||
}
|
||||
|
||||
producer_.reset(RdKafka::Producer::create(conf_.get(), errstr));
|
||||
if (producer_ == nullptr)
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr));
|
||||
}
|
||||
topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, tconf_.get(), errstr));
|
||||
if (topic_ == nullptr)
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr));
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
~kafka_sink()
|
||||
{
|
||||
producer_->flush(config_.flush_timeout_ms);
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const details::log_msg &msg) override
|
||||
{
|
||||
producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, (void *)msg.payload.data(), msg.payload.size(), NULL, NULL);
|
||||
}
|
||||
|
||||
void flush_() override
|
||||
{
|
||||
producer_->flush(config_.flush_timeout_ms);
|
||||
}
|
||||
|
||||
private:
|
||||
kafka_sink_config config_;
|
||||
std::unique_ptr<RdKafka::Producer> producer_ = nullptr;
|
||||
std::unique_ptr<RdKafka::Conf> conf_ = nullptr;
|
||||
std::unique_ptr<RdKafka::Conf> tconf_ = nullptr;
|
||||
std::unique_ptr<RdKafka::Topic> topic_ = nullptr;
|
||||
};
|
||||
|
||||
using kafka_sink_mt = kafka_sink<std::mutex>;
|
||||
using kafka_sink_st = kafka_sink<spdlog::details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> kafka_logger_mt(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> kafka_logger_st(const std::string &logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_mt(std::string logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_mt>(logger_name, config);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::async_factory>
|
||||
inline std::shared_ptr<spdlog::logger> kafka_logger_async_st(std::string logger_name, spdlog::sinks::kafka_sink_config config)
|
||||
{
|
||||
return Factory::template create<sinks::kafka_sink_st>(logger_name, config);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
File diff suppressed because it is too large
Load Diff
@ -1,23 +0,0 @@
|
||||
Boost Software License - Version 1.0 - August 17th, 2003
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
Loading…
Reference in New Issue