-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from xtuml/feature/kafka-log-appender
Feature/kafka log appender
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "LogAppender.hh" | ||
|
||
#include <log4cplus/initializer.h> | ||
#include <cppkafka/producer.h> | ||
#include <cppkafka/configuration.h> | ||
#include <nlohmann/json.hpp> | ||
#include <fmt/format.h> | ||
#include <fmt/chrono.h> | ||
#include "swa/CommandLine.hh" | ||
#include "kafka/Kafka.hh" | ||
|
||
|
||
namespace Kafka { | ||
class KafkaAppender : public log4cplus::Appender { | ||
|
||
public: | ||
explicit KafkaAppender(const log4cplus::helpers::Properties &props) | ||
: Appender(props), | ||
config({{"metadata.broker.list", props.getProperty("broker", SWA::CommandLine::getInstance().getOption(BrokersOption))}}), | ||
producer(config), | ||
messageBuilder(props.getProperty("topic", "xtuml.logging")) { | ||
} | ||
|
||
void close() override { | ||
producer.flush(); | ||
} | ||
|
||
void append(const log4cplus::spi::InternalLoggingEvent &event) override { | ||
std::string message = nlohmann::json( | ||
{ | ||
{"timestamp", fmt::format("{:%FT%TZ}", event.getTimestamp())}, | ||
{"logger", event.getLoggerName()}, | ||
{"level", log4cplus::getLogLevelManager().toString(event.getLogLevel())}, | ||
{"file", event.getFile()}, | ||
{"function", event.getFunction()}, | ||
{"line", event.getLine()}, | ||
{"message", event.getMessage()}, | ||
} | ||
).dump(); | ||
producer.produce(messageBuilder.payload(message)); | ||
} | ||
|
||
~KafkaAppender() override { | ||
destructorImpl(); | ||
} | ||
|
||
private: | ||
cppkafka::Configuration config; | ||
cppkafka::Producer producer; | ||
cppkafka::MessageBuilder messageBuilder; | ||
}; | ||
|
||
log4cplus::SharedAppenderPtr KafkaAppenderFactory::createObject(const log4cplus::helpers::Properties &props) { | ||
return log4cplus::SharedAppenderPtr(new KafkaAppender(props)); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef Kafka_LogAppender_HH | ||
#define Kafka_LogAppender_HH | ||
|
||
#include <log4cplus/log4cplus.h> | ||
#include <string> | ||
|
||
namespace Kafka { | ||
|
||
class KafkaAppenderFactory : public log4cplus::spi::AppenderFactory { | ||
public: | ||
[[nodiscard]] const log4cplus::tstring &getTypeName() const override { | ||
return name; | ||
} | ||
|
||
log4cplus::SharedAppenderPtr createObject(const log4cplus::helpers::Properties &props) override; | ||
|
||
private: | ||
std::string name = "KafkaAppender"; | ||
}; | ||
|
||
|
||
} | ||
|
||
#endif |