Crow  0.0.4
log4cplus.hpp
1 #ifndef NLOHMANN_CROW_LOG4CPLUS_HPP
2 #define NLOHMANN_CROW_LOG4CPLUS_HPP
3 
4 #include <string>
5 #include <unordered_map>
6 #include <crow/crow.hpp>
7 #include <crow/integrations/loggers.hpp>
8 #include <log4cplus/log4cplus.h>
9 #include <log4cplus/clogger.h>
10 
11 namespace nlohmann
12 {
13 namespace crow_integrations
14 {
15 
21 class log4cplus_appender : public log4cplus::Appender
22 {
23  public:
31  explicit log4cplus_appender(crow& client,
32  const std::string& appender_name = "sentry appender",
33  const std::unordered_map<log4cplus_loglevel_t, log_action>& config = std::unordered_map<log4cplus_loglevel_t, log_action>())
34  : m_client(client)
35  {
36  setName(appender_name);
37  m_log_actions[L4CP_FATAL_LOG_LEVEL] = log_action::message_fatal;
38  m_log_actions[L4CP_ERROR_LOG_LEVEL] = log_action::message_error;
39  m_log_actions[L4CP_WARN_LOG_LEVEL] = log_action::breadcrumb_warning;
40  m_log_actions[L4CP_INFO_LOG_LEVEL] = log_action::breadcrumb_info;
41  m_log_actions[L4CP_DEBUG_LOG_LEVEL] = log_action::breadcrumb_debug;
42  m_log_actions[L4CP_TRACE_LOG_LEVEL] = log_action::breadcrumb_debug;
43 
44  for (const auto& config_entry : config)
45  {
46  m_log_actions[config_entry.first] = config_entry.second;
47  }
48  }
49 
55  {
56  // properly clean up appender
57  destructorImpl();
58  }
59 
64  void close() override {};
65 
66  protected:
72  void append(const log4cplus::spi::InternalLoggingEvent& event) override
73  {
74  // look up action in configuration or use default action
75  const auto lookup_action = m_log_actions.find(event.getLogLevel());
76  const log_action action = (lookup_action != m_log_actions.end())
77  ? lookup_action->second
79 
80  switch (action)
81  {
82  case message_fatal:
83  case message_error:
84  case message_warning:
85  case message_info:
86  case message_debug:
87  {
88  m_client.capture_message(event.getMessage(),
89  {
90  {"logger", event.getLoggerName()},
91  {"level", log_action_level(action)},
92  {
93  "extra", {
94  {"location", event.getFile() + ":" + std::to_string(event.getLine())},
95  {"function", event.getFunction()}
96  }
97  }
98  });
99  return;
100  }
101 
102  case breadcrumb_fatal:
103  case breadcrumb_error:
104  case breadcrumb_warning:
105  case breadcrumb_info:
106  case breadcrumb_debug:
107  {
108  m_client.add_breadcrumb(event.getMessage(),
109  {
110  {"category", event.getLoggerName()},
111  {"level", log_action_level(action)},
112  {
113  "data", {
114  {"location", event.getFile() + ":" + std::to_string(event.getLine())},
115  {"function", event.getFunction()}
116  }
117  }
118  });
119 
120  return;
121  }
122 
123  case ignore:
124  return;
125  }
126  }
127 
128  private:
130  std::unordered_map<log4cplus_loglevel_t, log_action> m_log_actions;
132  crow& m_client;
133 };
134 
135 
136 }
137 }
138 
139 #endif
send a message with level "debug"
Definition: loggers.hpp:19
add a breadcrumb with level "info"
Definition: loggers.hpp:23
send a message with level "info"
Definition: loggers.hpp:18
add a breadcrumb with level "debug"
Definition: loggers.hpp:24
add a breadcrumb with level "warning"
Definition: loggers.hpp:22
add a breadcrumb with level "fatal"
Definition: loggers.hpp:20
const char * log_action_level(const log_action action) noexcept
get Sentry level for each log action
Definition: loggers.hpp:36
log4cplus_appender(crow &client, const std::string &appender_name="sentry appender", const std::unordered_map< log4cplus_loglevel_t, log_action > &config=std::unordered_map< log4cplus_loglevel_t, log_action >())
create the appender
Definition: log4cplus.hpp:31
namespace for Niels Lohmann
Definition: crow.hpp:40
send a message with level "warning"
Definition: loggers.hpp:17
void close() override
closing the appender
Definition: log4cplus.hpp:64
log_action
different actions to be executed for a log event
Definition: loggers.hpp:13
void append(const log4cplus::spi::InternalLoggingEvent &event) override
the actual appender implementation
Definition: log4cplus.hpp:72
void add_breadcrumb(const std::string &message, const json &attributes=nullptr)
add a breadcrumb to the current context
Definition: crow.cpp:181
send a message with level "error"
Definition: loggers.hpp:16
a C++ client for Sentry
Definition: crow.hpp:47
add a breadcrumb with level "error"
Definition: loggers.hpp:21
~log4cplus_appender() override
clean up appender state
Definition: log4cplus.hpp:54
void capture_message(const std::string &message, const json &attributes=nullptr, bool asynchronous=true)
capture a message
Definition: crow.cpp:118
send a message with level "fatal"
Definition: loggers.hpp:15