1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "common/Formatter.h"
6 #include "common/LogEntry.h"
8 #include "log/SubsystemMap.h"
13 Graylog::Graylog(const SubsystemMap * const s, std::string logger)
15 m_log_dst_valid(false),
18 m_logger(std::move(logger)),
19 m_ostream_compressed(std::stringstream::in |
20 std::stringstream::out |
21 std::stringstream::binary)
23 m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
24 m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
27 Graylog::Graylog(std::string logger)
29 m_log_dst_valid(false),
32 m_logger(std::move(logger)),
33 m_ostream_compressed(std::stringstream::in |
34 std::stringstream::out |
35 std::stringstream::binary)
37 m_formatter = std::unique_ptr<Formatter>(Formatter::create("json"));
38 m_formatter_section = std::unique_ptr<Formatter>(Formatter::create("json"));
45 void Graylog::set_destination(const std::string& host, int port)
48 boost::asio::ip::udp::resolver resolver(m_io_service);
49 boost::asio::ip::udp::resolver::query query(host, std::to_string(port));
50 m_endpoint = *resolver.resolve(query);
51 m_log_dst_valid = true;
52 } catch (boost::system::system_error const& e) {
53 cerr << "Error resolving graylog destination: " << e.what() << std::endl;
54 m_log_dst_valid = false;
58 void Graylog::set_hostname(const std::string& host)
63 void Graylog::set_fsid(const uuid_d& fsid)
65 std::vector<char> buf(40);
67 m_fsid = std::string(&buf[0]);
70 void Graylog::log_entry(Entry const * const e)
72 if (m_log_dst_valid) {
73 std::string s = e->get_str();
75 m_formatter->open_object_section("");
76 m_formatter->dump_string("version", "1.1");
77 m_formatter->dump_string("host", m_hostname);
78 m_formatter->dump_string("short_message", s);
79 m_formatter->dump_string("_app", "ceph");
80 m_formatter->dump_float("timestamp", e->m_stamp.sec() + (e->m_stamp.usec() / 1000000.0));
81 m_formatter->dump_unsigned("_thread", (uint64_t)e->m_thread);
82 m_formatter->dump_int("_level", e->m_prio);
84 m_formatter->dump_string("_subsys_name", m_subs->get_name(e->m_subsys));
85 m_formatter->dump_int("_subsys_id", e->m_subsys);
86 m_formatter->dump_string("_fsid", m_fsid);
87 m_formatter->dump_string("_logger", m_logger);
88 m_formatter->close_section();
90 m_ostream_compressed.clear();
91 m_ostream_compressed.str("");
95 m_ostream.push(m_compressor);
96 m_ostream.push(m_ostream_compressed);
98 m_formatter->flush(m_ostream);
99 m_ostream << std::endl;
104 boost::asio::ip::udp::socket socket(m_io_service);
105 socket.open(m_endpoint.protocol());
106 socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
107 } catch (boost::system::system_error const& e) {
108 cerr << "Error sending graylog message: " << e.what() << std::endl;
113 void Graylog::log_log_entry(LogEntry const * const e)
115 if (m_log_dst_valid) {
116 m_formatter->open_object_section("");
117 m_formatter->dump_string("version", "1.1");
118 m_formatter->dump_string("host", m_hostname);
119 m_formatter->dump_string("short_message", e->msg);
120 m_formatter->dump_float("timestamp", e->stamp.sec() + (e->stamp.usec() / 1000000.0));
121 m_formatter->dump_string("_app", "ceph");
123 m_formatter_section->open_object_section("");
124 e->who.addr.dump(m_formatter_section.get());
125 e->who.name.dump(m_formatter_section.get());
126 m_formatter_section->close_section();
128 m_ostream_section.clear();
129 m_ostream_section.str("");
130 m_formatter_section->flush(m_ostream_section);
131 m_formatter->dump_string("_who", m_ostream_section.str());
133 m_formatter->dump_int("_seq", e->seq);
134 m_formatter->dump_string("_prio", clog_type_to_string(e->prio));
135 m_formatter->dump_string("_channel", e->channel);
136 m_formatter->dump_string("_fsid", m_fsid);
137 m_formatter->dump_string("_logger", m_logger);
138 m_formatter->close_section();
140 m_ostream_compressed.clear();
141 m_ostream_compressed.str("");
145 m_ostream.push(m_compressor);
146 m_ostream.push(m_ostream_compressed);
148 m_formatter->flush(m_ostream);
149 m_ostream << std::endl;
154 boost::asio::ip::udp::socket socket(m_io_service);
155 socket.open(m_endpoint.protocol());
156 socket.send_to(boost::asio::buffer(m_ostream_compressed.str()), m_endpoint);
157 } catch (boost::system::system_error const& e) {
158 cerr << "Error sending graylog message: " << e.what() << std::endl;