|
|
@@ -5,10 +5,26 @@
|
|
|
// Created by Sam Jaffe on 4/7/19.
|
|
|
//
|
|
|
|
|
|
+#include "resource_factory/prototype_factory.hpp"
|
|
|
+
|
|
|
+#include "logger/detail/layout.h"
|
|
|
+#include "logger/log_manager.h"
|
|
|
+#include "logger/logpacket.h"
|
|
|
#include "logger/properties.h"
|
|
|
|
|
|
using namespace logging;
|
|
|
|
|
|
+class json_layout : public layout {
|
|
|
+public:
|
|
|
+ static std::shared_ptr<layout> create(properties const &);
|
|
|
+
|
|
|
+ json_layout(properties const & props);
|
|
|
+ void format(std::ostream & os, logpacket const & pkt) const override;
|
|
|
+
|
|
|
+private:
|
|
|
+ bool compact_, eol_, log_as_json_;
|
|
|
+};
|
|
|
+
|
|
|
using namespace logging::property;
|
|
|
properties const DEFAULT_JSON_LAYOUT{_obj({
|
|
|
{"charset", _v("en_US.UTF-8")},
|
|
|
@@ -19,3 +35,36 @@ properties const DEFAULT_JSON_LAYOUT{_obj({
|
|
|
{"includeNullDelimiter", _v(false)},
|
|
|
{"objectMessageAsJsonObject", _v(false)}
|
|
|
})};
|
|
|
+
|
|
|
+std::shared_ptr<layout> json_layout::create(properties const & props) {
|
|
|
+ properties const actual = DEFAULT_JSON_LAYOUT.mergedWith(props);
|
|
|
+ return std::make_shared<json_layout>(actual);
|
|
|
+}
|
|
|
+
|
|
|
+json_layout::json_layout(properties const & props)
|
|
|
+: compact_(props["compact"]),
|
|
|
+ eol_(props["eventEol"]),
|
|
|
+ log_as_json_(props["objectMessageAsJsonObject"]) {
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+static void write_timestamp(std::ostream & os, struct timeval time) {
|
|
|
+ os << "\"instant\": {\n \"epochSecond\": " << time.tv_sec;
|
|
|
+ os << ",\n \"nanoOfSecond\": " << (time.tv_usec * 1000) << "\n }";
|
|
|
+}
|
|
|
+
|
|
|
+void json_layout::format(std::ostream & os, logpacket const & pkt) const {
|
|
|
+ os << "{\n ";
|
|
|
+ write_timestamp(os, pkt.time);
|
|
|
+ os << "\n \"level\": \"" << pkt.level << "\",";
|
|
|
+ os << "\n \"loggerName\": \"" << pkt.logger << "\",";
|
|
|
+ os << "\n \"message\": \"" << pkt.message.str() << "\"";
|
|
|
+ os << "\n}";
|
|
|
+ if (eol_) {
|
|
|
+ os << NEWLINE_TOKEN;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+namespace {
|
|
|
+ bool _ = layouts::instance().bind("JsonLayout", json_layout::create);
|
|
|
+}
|