global_log_handle_test.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <cstring>
  5. #include "opentelemetry/nostd/shared_ptr.h"
  6. #include "opentelemetry/sdk/common/attribute_utils.h"
  7. #include "opentelemetry/sdk/common/global_log_handler.h"
  8. class CustomLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler
  9. {
  10. public:
  11. void Handle(opentelemetry::sdk::common::internal_log::LogLevel level,
  12. const char *,
  13. int,
  14. const char *msg,
  15. const opentelemetry::sdk::common::AttributeMap &) noexcept override
  16. {
  17. if (level == opentelemetry::sdk::common::internal_log::LogLevel::Debug)
  18. {
  19. EXPECT_EQ(0, strncmp(msg, "Debug message", 13));
  20. }
  21. else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Error)
  22. {
  23. EXPECT_EQ(0, strncmp(msg, "Error message", 13));
  24. }
  25. else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Info)
  26. {
  27. EXPECT_EQ(0, strncmp(msg, "Info message", 12));
  28. }
  29. else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Warning)
  30. {
  31. EXPECT_EQ(0, strncmp(msg, "Warning message", 15));
  32. }
  33. ++count;
  34. }
  35. size_t count = 0;
  36. };
  37. TEST(GlobalLogHandleTest, CustomLogHandler)
  38. {
  39. using opentelemetry::sdk::common::internal_log::LogHandler;
  40. auto backup_log_handle =
  41. opentelemetry::sdk::common::internal_log::GlobalLogHandler::GetLogHandler();
  42. auto backup_log_level = opentelemetry::sdk::common::internal_log::GlobalLogHandler::GetLogLevel();
  43. auto custom_log_handler = opentelemetry::nostd::shared_ptr<LogHandler>(new CustomLogHandler{});
  44. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(custom_log_handler);
  45. auto before_count = static_cast<CustomLogHandler *>(custom_log_handler.get())->count;
  46. opentelemetry::sdk::common::AttributeMap attributes = {
  47. {"url", "https://opentelemetry.io/"}, {"content-length", 0}, {"content-type", "text/html"}};
  48. OTEL_INTERNAL_LOG_ERROR("Error message");
  49. OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes);
  50. EXPECT_EQ(before_count + 1, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);
  51. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(
  52. opentelemetry::sdk::common::internal_log::LogLevel::Debug);
  53. OTEL_INTERNAL_LOG_ERROR("Error message");
  54. OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes);
  55. OTEL_INTERNAL_LOG_INFO("Info message");
  56. OTEL_INTERNAL_LOG_WARN("Warning message");
  57. EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);
  58. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(
  59. opentelemetry::sdk::common::internal_log::LogLevel::None);
  60. OTEL_INTERNAL_LOG_ERROR("Error message");
  61. OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes);
  62. OTEL_INTERNAL_LOG_INFO("Info message");
  63. OTEL_INTERNAL_LOG_WARN("Warning message");
  64. EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);
  65. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(backup_log_handle);
  66. opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(backup_log_level);
  67. }