logger.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/logs/logger_type_traits.h"
  5. #include "opentelemetry/logs/severity.h"
  6. #include "opentelemetry/nostd/string_view.h"
  7. #include "opentelemetry/nostd/unique_ptr.h"
  8. #include "opentelemetry/version.h"
  9. OPENTELEMETRY_BEGIN_NAMESPACE
  10. namespace common
  11. {
  12. class KeyValueIterable;
  13. } // namespace common
  14. namespace logs
  15. {
  16. class EventId;
  17. class LogRecord;
  18. /**
  19. * Handles log record creation.
  20. **/
  21. class Logger
  22. {
  23. public:
  24. virtual ~Logger() = default;
  25. /* Returns the name of the logger */
  26. virtual const nostd::string_view GetName() noexcept = 0;
  27. /**
  28. * Create a Log Record object
  29. *
  30. * @return nostd::unique_ptr<LogRecord>
  31. */
  32. virtual nostd::unique_ptr<LogRecord> CreateLogRecord() noexcept = 0;
  33. /**
  34. * Emit a Log Record object
  35. *
  36. * @param log_record Log record
  37. */
  38. virtual void EmitLogRecord(nostd::unique_ptr<LogRecord> &&log_record) noexcept = 0;
  39. /**
  40. * Emit a Log Record object with arguments
  41. *
  42. * @param log_record Log record
  43. * @param args Arguments which can be used to set data of log record by type.
  44. * Severity -> severity, severity_text
  45. * string_view -> body
  46. * AttributeValue -> body
  47. * SpanContext -> span_id,trace_id and trace_flags
  48. * SpanId -> span_id
  49. * TraceId -> trace_id
  50. * TraceFlags -> trace_flags
  51. * SystemTimestamp -> timestamp
  52. * system_clock::time_point -> timestamp
  53. * KeyValueIterable -> attributes
  54. * Key value iterable container -> attributes
  55. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  56. */
  57. template <class... ArgumentType>
  58. void EmitLogRecord(nostd::unique_ptr<LogRecord> &&log_record, ArgumentType &&...args)
  59. {
  60. if (!log_record)
  61. {
  62. return;
  63. }
  64. //
  65. // Keep the parameter pack unpacking order from left to right because left
  66. // ones are usually more important like severity and event_id than the
  67. // attributes. The left to right unpack order could pass the more important
  68. // data to processors to avoid caching and memory allocating.
  69. //
  70. #if __cplusplus <= 201402L
  71. // C++14 does not support fold expressions for parameter pack expansion.
  72. int dummy[] = {(detail::LogRecordSetterTrait<typename std::decay<ArgumentType>::type>::Set(
  73. log_record.get(), std::forward<ArgumentType>(args)),
  74. 0)...};
  75. IgnoreTraitResult(dummy);
  76. #else
  77. IgnoreTraitResult((detail::LogRecordSetterTrait<typename std::decay<ArgumentType>::type>::Set(
  78. log_record.get(), std::forward<ArgumentType>(args)),
  79. ...));
  80. #endif
  81. EmitLogRecord(std::move(log_record));
  82. }
  83. /**
  84. * Emit a Log Record object with arguments
  85. *
  86. * @param args Arguments which can be used to set data of log record by type.
  87. * Severity -> severity, severity_text
  88. * string_view -> body
  89. * AttributeValue -> body
  90. * SpanContext -> span_id,trace_id and trace_flags
  91. * SpanId -> span_id
  92. * TraceId -> trace_id
  93. * TraceFlags -> trace_flags
  94. * SystemTimestamp -> timestamp
  95. * system_clock::time_point -> timestamp
  96. * KeyValueIterable -> attributes
  97. * Key value iterable container -> attributes
  98. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  99. */
  100. template <class... ArgumentType>
  101. void EmitLogRecord(ArgumentType &&...args)
  102. {
  103. nostd::unique_ptr<LogRecord> log_record = CreateLogRecord();
  104. EmitLogRecord(std::move(log_record), std::forward<ArgumentType>(args)...);
  105. }
  106. /**
  107. * Writes a log with a severity of trace.
  108. * @param args Arguments which can be used to set data of log record by type.
  109. * string_view -> body
  110. * AttributeValue -> body
  111. * SpanContext -> span_id,trace_id and trace_flags
  112. * SpanId -> span_id
  113. * TraceId -> trace_id
  114. * TraceFlags -> trace_flags
  115. * SystemTimestamp -> timestamp
  116. * system_clock::time_point -> timestamp
  117. * KeyValueIterable -> attributes
  118. * Key value iterable container -> attributes
  119. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  120. */
  121. template <class... ArgumentType>
  122. void Trace(ArgumentType &&...args) noexcept
  123. {
  124. static_assert(
  125. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  126. "Severity is already set.");
  127. this->EmitLogRecord(Severity::kTrace, std::forward<ArgumentType>(args)...);
  128. }
  129. /**
  130. * Writes a log with a severity of debug.
  131. * @param args Arguments which can be used to set data of log record by type.
  132. * string_view -> body
  133. * AttributeValue -> body
  134. * SpanContext -> span_id,trace_id and trace_flags
  135. * SpanId -> span_id
  136. * TraceId -> trace_id
  137. * TraceFlags -> trace_flags
  138. * SystemTimestamp -> timestamp
  139. * system_clock::time_point -> timestamp
  140. * KeyValueIterable -> attributes
  141. * Key value iterable container -> attributes
  142. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  143. */
  144. template <class... ArgumentType>
  145. void Debug(ArgumentType &&...args) noexcept
  146. {
  147. static_assert(
  148. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  149. "Severity is already set.");
  150. this->EmitLogRecord(Severity::kDebug, std::forward<ArgumentType>(args)...);
  151. }
  152. /**
  153. * Writes a log with a severity of info.
  154. * @param args Arguments which can be used to set data of log record by type.
  155. * string_view -> body
  156. * AttributeValue -> body
  157. * SpanContext -> span_id,trace_id and trace_flags
  158. * SpanId -> span_id
  159. * TraceId -> trace_id
  160. * TraceFlags -> trace_flags
  161. * SystemTimestamp -> timestamp
  162. * system_clock::time_point -> timestamp
  163. * KeyValueIterable -> attributes
  164. * Key value iterable container -> attributes
  165. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  166. */
  167. template <class... ArgumentType>
  168. void Info(ArgumentType &&...args) noexcept
  169. {
  170. static_assert(
  171. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  172. "Severity is already set.");
  173. this->EmitLogRecord(Severity::kInfo, std::forward<ArgumentType>(args)...);
  174. }
  175. /**
  176. * Writes a log with a severity of warn.
  177. * @param args Arguments which can be used to set data of log record by type.
  178. * string_view -> body
  179. * AttributeValue -> body
  180. * SpanContext -> span_id,trace_id and trace_flags
  181. * SpanId -> span_id
  182. * TraceId -> trace_id
  183. * TraceFlags -> trace_flags
  184. * SystemTimestamp -> timestamp
  185. * system_clock::time_point -> timestamp
  186. * KeyValueIterable -> attributes
  187. * Key value iterable container -> attributes
  188. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  189. */
  190. template <class... ArgumentType>
  191. void Warn(ArgumentType &&...args) noexcept
  192. {
  193. static_assert(
  194. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  195. "Severity is already set.");
  196. this->EmitLogRecord(Severity::kWarn, std::forward<ArgumentType>(args)...);
  197. }
  198. /**
  199. * Writes a log with a severity of error.
  200. * @param args Arguments which can be used to set data of log record by type.
  201. * string_view -> body
  202. * AttributeValue -> body
  203. * SpanContext -> span_id,trace_id and trace_flags
  204. * SpanId -> span_id
  205. * TraceId -> trace_id
  206. * TraceFlags -> trace_flags
  207. * SystemTimestamp -> timestamp
  208. * system_clock::time_point -> timestamp
  209. * KeyValueIterable -> attributes
  210. * Key value iterable container -> attributes
  211. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  212. */
  213. template <class... ArgumentType>
  214. void Error(ArgumentType &&...args) noexcept
  215. {
  216. static_assert(
  217. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  218. "Severity is already set.");
  219. this->EmitLogRecord(Severity::kError, std::forward<ArgumentType>(args)...);
  220. }
  221. /**
  222. * Writes a log with a severity of fatal.
  223. * @param args Arguments which can be used to set data of log record by type.
  224. * string_view -> body
  225. * AttributeValue -> body
  226. * SpanContext -> span_id,trace_id and trace_flags
  227. * SpanId -> span_id
  228. * TraceId -> trace_id
  229. * TraceFlags -> trace_flags
  230. * SystemTimestamp -> timestamp
  231. * system_clock::time_point -> timestamp
  232. * KeyValueIterable -> attributes
  233. * Key value iterable container -> attributes
  234. * span<pair<string_view, AttributeValue>> -> attributes(return type of MakeAttributes)
  235. */
  236. template <class... ArgumentType>
  237. void Fatal(ArgumentType &&...args) noexcept
  238. {
  239. static_assert(
  240. !detail::LogRecordHasType<Severity, typename std::decay<ArgumentType>::type...>::value,
  241. "Severity is already set.");
  242. this->EmitLogRecord(Severity::kFatal, std::forward<ArgumentType>(args)...);
  243. }
  244. //
  245. // OpenTelemetry C++ user-facing Logs API
  246. //
  247. inline bool Enabled(Severity severity, const EventId &event_id) const noexcept
  248. {
  249. if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
  250. {
  251. return false;
  252. }
  253. return EnabledImplementation(severity, event_id);
  254. }
  255. inline bool Enabled(Severity severity, int64_t event_id) const noexcept
  256. {
  257. if OPENTELEMETRY_LIKELY_CONDITION (!Enabled(severity))
  258. {
  259. return false;
  260. }
  261. return EnabledImplementation(severity, event_id);
  262. }
  263. inline bool Enabled(Severity severity) const noexcept
  264. {
  265. return static_cast<uint8_t>(severity) >= OPENTELEMETRY_ATOMIC_READ_8(&minimum_severity_);
  266. }
  267. /**
  268. * Log an event
  269. *
  270. * @severity severity of the log
  271. * @event_id event identifier of the log
  272. * @format an utf-8 string following https://messagetemplates.org/
  273. * @attributes key value pairs of the log
  274. */
  275. virtual void Log(Severity severity,
  276. const EventId &event_id,
  277. nostd::string_view format,
  278. const common::KeyValueIterable &attributes) noexcept
  279. {
  280. this->EmitLogRecord(severity, event_id, format, attributes);
  281. }
  282. virtual void Log(Severity severity,
  283. int64_t event_id,
  284. nostd::string_view format,
  285. const common::KeyValueIterable &attributes) noexcept
  286. {
  287. this->EmitLogRecord(severity, EventId{event_id}, format, attributes);
  288. }
  289. virtual void Log(Severity severity,
  290. nostd::string_view format,
  291. const common::KeyValueIterable &attributes) noexcept
  292. {
  293. this->EmitLogRecord(severity, format, attributes);
  294. }
  295. virtual void Log(Severity severity, nostd::string_view message) noexcept
  296. {
  297. this->EmitLogRecord(severity, message);
  298. }
  299. // Convenient wrappers based on virtual methods Log().
  300. // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
  301. inline void Trace(const EventId &event_id,
  302. nostd::string_view format,
  303. const common::KeyValueIterable &attributes) noexcept
  304. {
  305. this->Log(Severity::kTrace, event_id, format, attributes);
  306. }
  307. inline void Trace(int64_t event_id,
  308. nostd::string_view format,
  309. const common::KeyValueIterable &attributes) noexcept
  310. {
  311. this->Log(Severity::kTrace, EventId{event_id}, format, attributes);
  312. }
  313. inline void Trace(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  314. {
  315. this->Log(Severity::kTrace, format, attributes);
  316. }
  317. inline void Trace(nostd::string_view message) noexcept { this->Log(Severity::kTrace, message); }
  318. inline void Debug(const EventId &event_id,
  319. nostd::string_view format,
  320. const common::KeyValueIterable &attributes) noexcept
  321. {
  322. this->Log(Severity::kDebug, event_id, format, attributes);
  323. }
  324. inline void Debug(int64_t event_id,
  325. nostd::string_view format,
  326. const common::KeyValueIterable &attributes) noexcept
  327. {
  328. this->Log(Severity::kDebug, EventId{event_id}, format, attributes);
  329. }
  330. inline void Debug(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  331. {
  332. this->Log(Severity::kDebug, format, attributes);
  333. }
  334. inline void Debug(nostd::string_view message) noexcept { this->Log(Severity::kDebug, message); }
  335. inline void Info(const EventId &event_id,
  336. nostd::string_view format,
  337. const common::KeyValueIterable &attributes) noexcept
  338. {
  339. this->Log(Severity::kInfo, event_id, format, attributes);
  340. }
  341. inline void Info(int64_t event_id,
  342. nostd::string_view format,
  343. const common::KeyValueIterable &attributes) noexcept
  344. {
  345. this->Log(Severity::kInfo, EventId{event_id}, format, attributes);
  346. }
  347. inline void Info(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  348. {
  349. this->Log(Severity::kInfo, format, attributes);
  350. }
  351. inline void Info(nostd::string_view message) noexcept { this->Log(Severity::kInfo, message); }
  352. inline void Warn(const EventId &event_id,
  353. nostd::string_view format,
  354. const common::KeyValueIterable &attributes) noexcept
  355. {
  356. this->Log(Severity::kWarn, event_id, format, attributes);
  357. }
  358. inline void Warn(int64_t event_id,
  359. nostd::string_view format,
  360. const common::KeyValueIterable &attributes) noexcept
  361. {
  362. this->Log(Severity::kWarn, EventId{event_id}, format, attributes);
  363. }
  364. inline void Warn(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  365. {
  366. this->Log(Severity::kWarn, format, attributes);
  367. }
  368. inline void Warn(nostd::string_view message) noexcept { this->Log(Severity::kWarn, message); }
  369. inline void Error(const EventId &event_id,
  370. nostd::string_view format,
  371. const common::KeyValueIterable &attributes) noexcept
  372. {
  373. this->Log(Severity::kError, event_id, format, attributes);
  374. }
  375. inline void Error(int64_t event_id,
  376. nostd::string_view format,
  377. const common::KeyValueIterable &attributes) noexcept
  378. {
  379. this->Log(Severity::kError, EventId{event_id}, format, attributes);
  380. }
  381. inline void Error(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  382. {
  383. this->Log(Severity::kError, format, attributes);
  384. }
  385. inline void Error(nostd::string_view message) noexcept { this->Log(Severity::kError, message); }
  386. inline void Fatal(const EventId &event_id,
  387. nostd::string_view format,
  388. const common::KeyValueIterable &attributes) noexcept
  389. {
  390. this->Log(Severity::kFatal, event_id, format, attributes);
  391. }
  392. inline void Fatal(int64_t event_id,
  393. nostd::string_view format,
  394. const common::KeyValueIterable &attributes) noexcept
  395. {
  396. this->Log(Severity::kFatal, EventId{event_id}, format, attributes);
  397. }
  398. inline void Fatal(nostd::string_view format, const common::KeyValueIterable &attributes) noexcept
  399. {
  400. this->Log(Severity::kFatal, format, attributes);
  401. }
  402. inline void Fatal(nostd::string_view message) noexcept { this->Log(Severity::kFatal, message); }
  403. //
  404. // End of OpenTelemetry C++ user-facing Log API.
  405. //
  406. protected:
  407. // TODO: discuss with community about naming for internal methods.
  408. virtual bool EnabledImplementation(Severity /*severity*/,
  409. const EventId & /*event_id*/) const noexcept
  410. {
  411. return false;
  412. }
  413. virtual bool EnabledImplementation(Severity /*severity*/, int64_t /*event_id*/) const noexcept
  414. {
  415. return false;
  416. }
  417. void SetMinimumSeverity(uint8_t severity_or_max) noexcept
  418. {
  419. OPENTELEMETRY_ATOMIC_WRITE_8(&minimum_severity_, severity_or_max);
  420. }
  421. private:
  422. template <class... ValueType>
  423. void IgnoreTraitResult(ValueType &&...)
  424. {}
  425. //
  426. // minimum_severity_ can be updated concurrently by multiple threads/cores, so race condition on
  427. // read/write should be handled. And std::atomic can not be used here because it is not ABI
  428. // compatible for OpenTelemetry C++ API.
  429. //
  430. mutable uint8_t minimum_severity_{kMaxSeverity};
  431. };
  432. } // namespace logs
  433. OPENTELEMETRY_END_NAMESPACE