1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifdef ENABLE_LOGS_PREVIEW
11 # include "opentelemetry/common/attribute_value.h"
12 # include "opentelemetry/common/key_value_iterable.h"
13 # include "opentelemetry/common/key_value_iterable_view.h"
14 # include "opentelemetry/common/macros.h"
15 # include "opentelemetry/common/timestamp.h"
16 # include "opentelemetry/logs/severity.h"
17 # include "opentelemetry/nostd/shared_ptr.h"
18 # include "opentelemetry/nostd/span.h"
19 # include "opentelemetry/nostd/string_view.h"
20 # include "opentelemetry/nostd/type_traits.h"
21 # include "opentelemetry/trace/span_id.h"
22 # include "opentelemetry/trace/trace_flags.h"
23 # include "opentelemetry/trace/trace_id.h"
24 # include "opentelemetry/version.h"
26 OPENTELEMETRY_BEGIN_NAMESPACE
30 * Handles log record creation.
35 virtual ~Logger() = default;
37 /* Returns the name of the logger */
38 virtual const nostd::string_view
GetName() noexcept
= 0;
41 * Each of the following overloaded Log(...) methods
42 * creates a log message with the specific parameters passed.
44 * @param severity the severity level of the log event.
45 * @param message the string message of the log (perhaps support std::fmt or fmt-lib format).
46 * @param attributes the attributes, stored as a 2D list of key/value pairs, that are associated
48 * @param trace_id the trace id associated with the log event.
49 * @param span_id the span id associate with the log event.
50 * @param trace_flags the trace flags associated with the log event.
51 * @param timestamp the timestamp the log record was created.
52 * @throws No exceptions under any circumstances.
56 * The base Log(...) method that all other Log(...) overloaded methods will eventually call,
57 * in order to create a log record.
59 virtual void Log(Severity severity
,
60 nostd::string_view body
,
61 const common::KeyValueIterable
&attributes
,
62 trace::TraceId trace_id
,
63 trace::SpanId span_id
,
64 trace::TraceFlags trace_flags
,
65 common::SystemTimestamp timestamp
) noexcept
= 0;
68 * Each of the following overloaded Log(...) methods
69 * creates a log message with the specific parameters passed.
71 * @param severity the severity level of the log event.
72 * @param name the name of the log event.
73 * @param message the string message of the log (perhaps support std::fmt or fmt-lib format).
74 * @param attributes the attributes, stored as a 2D list of key/value pairs, that are associated
76 * @param trace_id the trace id associated with the log event.
77 * @param span_id the span id associate with the log event.
78 * @param trace_flags the trace flags associated with the log event.
79 * @param timestamp the timestamp the log record was created.
80 * @throws No exceptions under any circumstances.
82 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
83 virtual void Log(Severity severity
,
84 OPENTELEMETRY_MAYBE_UNUSED
nostd::string_view name
,
85 nostd::string_view body
,
86 const common::KeyValueIterable
&attributes
,
87 trace::TraceId trace_id
,
88 trace::SpanId span_id
,
89 trace::TraceFlags trace_flags
,
90 common::SystemTimestamp timestamp
) noexcept
92 Log(severity
, body
, attributes
, trace_id
, span_id
, trace_flags
, timestamp
);
95 /*** Overloaded methods for KeyValueIterables ***/
97 * The secondary base Log(...) method that all other Log(...) overloaded methods except the one
98 * above will eventually call, in order to create a log record.
101 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
102 void Log(Severity severity
,
103 nostd::string_view body
,
105 trace::TraceId trace_id
,
106 trace::SpanId span_id
,
107 trace::TraceFlags trace_flags
,
108 common::SystemTimestamp timestamp
) noexcept
110 Log(severity
, body
, common::KeyValueIterableView
<T
>(attributes
), trace_id
, span_id
, trace_flags
,
115 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
116 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
117 void Log(Severity severity
,
118 nostd::string_view name
,
119 nostd::string_view body
,
121 trace::TraceId trace_id
,
122 trace::SpanId span_id
,
123 trace::TraceFlags trace_flags
,
124 common::SystemTimestamp timestamp
) noexcept
126 Log(severity
, name
, body
, common::KeyValueIterableView
<T
>(attributes
), trace_id
, span_id
,
127 trace_flags
, timestamp
);
130 void Log(Severity severity
,
131 nostd::string_view body
,
132 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>> attributes
,
133 trace::TraceId trace_id
,
134 trace::SpanId span_id
,
135 trace::TraceFlags trace_flags
,
136 common::SystemTimestamp timestamp
) noexcept
138 return this->Log(severity
, body
,
139 nostd::span
<const std::pair
<nostd::string_view
, common::AttributeValue
>>{
140 attributes
.begin(), attributes
.end()},
141 trace_id
, span_id
, trace_flags
, timestamp
);
144 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
145 void Log(Severity severity
,
146 nostd::string_view name
,
147 nostd::string_view body
,
148 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>> attributes
,
149 trace::TraceId trace_id
,
150 trace::SpanId span_id
,
151 trace::TraceFlags trace_flags
,
152 common::SystemTimestamp timestamp
) noexcept
154 return this->Log(severity
, name
, body
,
155 nostd::span
<const std::pair
<nostd::string_view
, common::AttributeValue
>>{
156 attributes
.begin(), attributes
.end()},
157 trace_id
, span_id
, trace_flags
, timestamp
);
160 /** Wrapper methods that the user could call for convenience when logging **/
164 * @param severity The severity of the log
165 * @param message The message to log
167 void Log(Severity severity
, nostd::string_view message
) noexcept
169 this->Log(severity
, message
, {}, {}, {}, {}, std::chrono::system_clock::now());
172 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
173 void Log(Severity severity
, nostd::string_view name
, nostd::string_view message
) noexcept
175 this->Log(severity
, name
, message
, {}, {}, {}, {}, std::chrono::system_clock::now());
180 * @param severity The severity of the log
181 * @param attributes The attributes of the log as a key/value object
184 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
185 void Log(Severity severity
, const T
&attributes
) noexcept
187 this->Log(severity
, "", attributes
, {}, {}, {}, std::chrono::system_clock::now());
192 * @param severity The severity of the log
193 * @param message The message to log
194 * @param attributes The attributes of the log as a key/value object
197 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
198 void Log(Severity severity
, nostd::string_view message
, const T
&attributes
) noexcept
200 this->Log(severity
, message
, attributes
, {}, {}, {}, std::chrono::system_clock::now());
205 * @param severity The severity of the log
206 * @param attributes The attributes of the log as an initializer list
208 void Log(Severity severity
,
209 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
212 this->Log(severity
, "", attributes
, {}, {}, {}, std::chrono::system_clock::now());
217 * @param severity The severity of the log
218 * @param message The message to log
219 * @param attributes The attributes of the log as an initializer list
221 void Log(Severity severity
,
222 nostd::string_view message
,
223 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
226 this->Log(severity
, message
, attributes
, {}, {}, {}, std::chrono::system_clock::now());
231 * @param severity The severity of the log
232 * @param attributes The attributes, stored as a 2D list of key/value pairs, that are associated
235 void Log(Severity severity
, const common::KeyValueIterable
&attributes
) noexcept
237 this->Log(severity
, "", attributes
, {}, {}, {}, std::chrono::system_clock::now());
242 * @param severity The severity of the log
243 * @param message The message to log
244 * @param attributes The attributes, stored as a 2D list of key/value pairs, that are associated
247 void Log(Severity severity
,
248 nostd::string_view message
,
249 const common::KeyValueIterable
&attributes
) noexcept
251 this->Log(severity
, message
, attributes
, {}, {}, {}, std::chrono::system_clock::now());
254 /** Trace severity overloads **/
257 * Writes a log with a severity of trace.
258 * @param message The message to log
260 void Trace(nostd::string_view message
) noexcept
{ this->Log(Severity::kTrace
, message
); }
263 * Writes a log with a severity of trace.
264 * @param name The name of the log
265 * @param message The message to log
267 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
268 void Trace(nostd::string_view name
, nostd::string_view message
) noexcept
270 this->Log(Severity::kTrace
, name
, message
);
274 * Writes a log with a severity of trace.
275 * @param attributes The attributes of the log as a key/value object
278 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
279 void Trace(const T
&attributes
) noexcept
281 this->Log(Severity::kTrace
, attributes
);
285 * Writes a log with a severity of trace.
286 * @param message The message of the log
287 * @param attributes The attributes of the log as a key/value object
290 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
291 void Trace(nostd::string_view message
, const T
&attributes
) noexcept
293 this->Log(Severity::kTrace
, message
, attributes
);
297 * Writes a log with a severity of trace.
298 * @param attributes The attributes of the log as an initializer list
300 void Trace(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
303 this->Log(Severity::kTrace
, attributes
);
307 * Writes a log with a severity of trace.
308 * @param message The message of the log
309 * @param attributes The attributes of the log as an initializer list
311 void Trace(nostd::string_view message
,
312 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
315 this->Log(Severity::kTrace
, message
, attributes
);
318 /** Debug severity overloads **/
321 * Writes a log with a severity of debug.
322 * @param message The message to log
324 void Debug(nostd::string_view message
) noexcept
{ this->Log(Severity::kDebug
, message
); }
327 * Writes a log with a severity of debug.
328 * @param name The name of the log
329 * @param message The message to log
331 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
332 void Debug(nostd::string_view name
, nostd::string_view message
) noexcept
334 this->Log(Severity::kDebug
, name
, message
);
338 * Writes a log with a severity of debug.
339 * @param attributes The attributes of the log as a key/value object
342 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
343 void Debug(const T
&attributes
) noexcept
345 this->Log(Severity::kDebug
, attributes
);
349 * Writes a log with a severity of debug.
350 * @param message The message of the log
351 * @param attributes The attributes of the log as a key/value object
354 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
355 void Debug(nostd::string_view message
, const T
&attributes
) noexcept
357 this->Log(Severity::kDebug
, message
, attributes
);
361 * Writes a log with a severity of debug.
362 * @param attributes The attributes of the log as an initializer list
364 void Debug(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
367 this->Log(Severity::kDebug
, attributes
);
371 * Writes a log with a severity of debug.
372 * @param message The message of the log
373 * @param attributes The attributes of the log as an initializer list
375 void Debug(nostd::string_view message
,
376 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
379 this->Log(Severity::kDebug
, message
, attributes
);
382 /** Info severity overloads **/
385 * Writes a log with a severity of info.
386 * @param message The message to log
388 void Info(nostd::string_view message
) noexcept
{ this->Log(Severity::kInfo
, message
); }
391 * Writes a log with a severity of info.
392 * @param name The name of the log
393 * @param message The message to log
395 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
396 void Info(nostd::string_view name
, nostd::string_view message
) noexcept
398 this->Log(Severity::kInfo
, name
, message
);
402 * Writes a log with a severity of info.
403 * @param attributes The attributes of the log as a key/value object
406 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
407 void Info(const T
&attributes
) noexcept
409 this->Log(Severity::kInfo
, attributes
);
413 * Writes a log with a severity of info.
414 * @param message The message of the log
415 * @param attributes The attributes of the log as a key/value object
418 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
419 void Info(nostd::string_view message
, const T
&attributes
) noexcept
421 this->Log(Severity::kInfo
, message
, attributes
);
425 * Writes a log with a severity of info.
426 * @param attributes The attributes of the log as an initializer list
428 void Info(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
431 this->Log(Severity::kInfo
, attributes
);
435 * Writes a log with a severity of info.
436 * @param message The message of the log
437 * @param attributes The attributes of the log as an initializer list
439 void Info(nostd::string_view message
,
440 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
443 this->Log(Severity::kInfo
, message
, attributes
);
446 /** Warn severity overloads **/
449 * Writes a log with a severity of warn.
450 * @param message The message to log
452 void Warn(nostd::string_view message
) noexcept
{ this->Log(Severity::kWarn
, message
); }
455 * Writes a log with a severity of warn.
456 * @param name The name of the log
457 * @param message The message to log
459 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
460 void Warn(nostd::string_view name
, nostd::string_view message
) noexcept
462 this->Log(Severity::kWarn
, name
, message
);
466 * Writes a log with a severity of warn.
467 * @param attributes The attributes of the log as a key/value object
470 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
471 void Warn(const T
&attributes
) noexcept
473 this->Log(Severity::kWarn
, attributes
);
477 * Writes a log with a severity of warn.
478 * @param message The message of the log
479 * @param attributes The attributes of the log as a key/value object
482 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
483 void Warn(nostd::string_view message
, const T
&attributes
) noexcept
485 this->Log(Severity::kWarn
, message
, attributes
);
489 * Writes a log with a severity of warn.
490 * @param attributes The attributes of the log as an initializer list
492 void Warn(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
495 this->Log(Severity::kWarn
, attributes
);
499 * Writes a log with a severity of warn.
500 * @param message The message of the log
501 * @param attributes The attributes of the log as an initializer list
503 void Warn(nostd::string_view message
,
504 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
507 this->Log(Severity::kWarn
, message
, attributes
);
510 /** Error severity overloads **/
513 * Writes a log with a severity of error.
514 * @param message The message to log
516 void Error(nostd::string_view message
) noexcept
{ this->Log(Severity::kError
, message
); }
519 * Writes a log with a severity of error.
520 * @param name The name of the log
521 * @param message The message to log
523 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
524 void Error(nostd::string_view name
, nostd::string_view message
) noexcept
526 this->Log(Severity::kError
, name
, message
);
530 * Writes a log with a severity of error.
531 * @param attributes The attributes of the log as a key/value object
534 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
535 void Error(const T
&attributes
) noexcept
537 this->Log(Severity::kError
, attributes
);
541 * Writes a log with a severity of error.
542 * @param message The message of the log
543 * @param attributes The attributes of the log as a key/value object
546 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
547 void Error(nostd::string_view message
, const T
&attributes
) noexcept
549 this->Log(Severity::kError
, message
, attributes
);
553 * Writes a log with a severity of error.
554 * @param attributes The attributes of the log as an initializer list
556 void Error(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
559 this->Log(Severity::kError
, attributes
);
563 * Writes a log with a severity of error.
564 * @param message The message of the log
565 * @param attributes The attributes of the log as an initializer list
567 void Error(nostd::string_view message
,
568 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
571 this->Log(Severity::kError
, message
, attributes
);
574 /** Fatal severity overloads **/
577 * Writes a log with a severity of fatal.
578 * @param message The message to log
580 void Fatal(nostd::string_view message
) noexcept
{ this->Log(Severity::kFatal
, message
); }
583 * Writes a log with a severity of fatal.
584 * @param name The name of the log
585 * @param message The message to log
587 OPENTELEMETRY_DEPRECATED_MESSAGE("name will be removed in the future")
588 void Fatal(nostd::string_view name
, nostd::string_view message
) noexcept
590 this->Log(Severity::kFatal
, name
, message
);
594 * Writes a log with a severity of fatal.
595 * @param attributes The attributes of the log as a key/value object
598 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
599 void Fatal(const T
&attributes
) noexcept
601 this->Log(Severity::kFatal
, attributes
);
605 * Writes a log with a severity of fatal.
606 * @param message The message of the log
607 * @param attributes The attributes of the log as a key/value object
610 nostd::enable_if_t
<common::detail::is_key_value_iterable
<T
>::value
> * = nullptr>
611 void Fatal(nostd::string_view message
, const T
&attributes
) noexcept
613 this->Log(Severity::kFatal
, message
, attributes
);
617 * Writes a log with a severity of fatal.
618 * @param attributes The attributes of the log as an initializer list
620 void Fatal(std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
623 this->Log(Severity::kFatal
, attributes
);
627 * Writes a log with a severity of fatal.
628 * @param message The message of the log
629 * @param attributes The attributes of the log as an initializer list
631 void Fatal(nostd::string_view message
,
632 std::initializer_list
<std::pair
<nostd::string_view
, common::AttributeValue
>>
635 this->Log(Severity::kFatal
, message
, attributes
);
639 OPENTELEMETRY_END_NAMESPACE