Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] DefaultLogHandler to print errors to std::cerr, add LogLevel::None #2622

Merged
merged 9 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Increment the:
[#2530](https://github.com/open-telemetry/opentelemetry-cpp/pull/2530)
* [EXPORTER] Support URL-encoded values for `OTEL_EXPORTER_OTLP_HEADERS`
[#2579](https://github.com/open-telemetry/opentelemetry-cpp/pull/2579)
* [SDK] DefaultLogHandler to print to std::cerr, add LogLevel::None
[#2622](https://github.com/open-telemetry/opentelemetry-cpp/pull/2622)

Important changes:

Expand All @@ -32,6 +34,22 @@ Important changes:
dependency on zlib.
* [SDK] Change OTLP HTTP content_type default to binary
[#2558](https://github.com/open-telemetry/opentelemetry-cpp/pull/2558)
* [SDK] DefaultLogHandler to print to std::cerr, add LogLevel::None
[#2622](https://github.com/open-telemetry/opentelemetry-cpp/pull/2622)
* Change DefaultLogHandler output
* Before, the default internal logger, DefaultLogHandler,
used to print to std::cout, interfering with the user application.
* Now, DefaultLogHandler prints to std::cerr, as expected.
* Applications that expected to find the opentelemetry-cpp internal
error log in std::cout may need adjustments, either by looking
at std::cerr instead, or by using a custom log handler.
* Additional LogLevel::None
* LogLevel::None is a new supported log level, which does not print
any message.
* Custom log handlers may need to implement a new case, to avoid
compiler warnings.
* Numbering of log levels like OTEL_INTERNAL_LOG_LEVEL_ERROR
has changed, which requires to rebuild, as the SDK ABI differs.

## [1.14.2] 2024-02-27

Expand Down
10 changes: 5 additions & 5 deletions exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ TEST(OStreamLogRecordExporter, Shutdown)
auto exporter =
std::unique_ptr<sdklogs::LogRecordExporter>(new exporterlogs::OStreamLogRecordExporter);

// Save cout's original buffer here
std::streambuf *original = std::cout.rdbuf();
// Save cerr original buffer here
std::streambuf *original = std::cerr.rdbuf();

// Redirect cout to our stringstream buffer
// Redirect cerr to our stringstream buffer
std::stringstream output;
std::cout.rdbuf(output.rdbuf());
std::cerr.rdbuf(output.rdbuf());

EXPECT_TRUE(exporter->Shutdown());

Expand All @@ -64,7 +64,7 @@ TEST(OStreamLogRecordExporter, Shutdown)
exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&record, 1));

// Restore original stringstream buffer
std::cout.rdbuf(original);
std::cerr.rdbuf(original);
std::string err_message =
"[Ostream Log Exporter] Exporting 1 log(s) failed, exporter is shutdown";
EXPECT_TRUE(output.str().find(err_message) != std::string::npos);
Expand Down
4 changes: 2 additions & 2 deletions exporters/ostream/test/ostream_span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ TEST(OStreamSpanExporter, Shutdown)
auto recordable = processor->MakeRecordable();
recordable->SetName("Test Span");

// Capture the output of cout
const auto captured = WithOStreamCapture(std::cout, [&]() {
// Capture the output of cerr
const auto captured = WithOStreamCapture(std::cerr, [&]() {
EXPECT_TRUE(processor->Shutdown());
processor->OnEnd(std::move(recordable));
});
Expand Down
2 changes: 2 additions & 0 deletions functional/otlp/func_http_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class TestLogHandler : public opentelemetry::sdk::common::internal_log::LogHandl

switch (level)
{
case opentelemetry::sdk::common::internal_log::LogLevel::None:
break;
case opentelemetry::sdk::common::internal_log::LogLevel::Error:
std::cout << " - [E] " << msg << std::endl;
parse_error_msg(&g_test_result, msg);
Expand Down
20 changes: 12 additions & 8 deletions sdk/include/opentelemetry/sdk/common/global_log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/version.h"

#define OTEL_INTERNAL_LOG_LEVEL_ERROR 0
#define OTEL_INTERNAL_LOG_LEVEL_WARN 1
#define OTEL_INTERNAL_LOG_LEVEL_INFO 2
#define OTEL_INTERNAL_LOG_LEVEL_DEBUG 3
#define OTEL_INTERNAL_LOG_LEVEL_NONE 0
#define OTEL_INTERNAL_LOG_LEVEL_ERROR 1
#define OTEL_INTERNAL_LOG_LEVEL_WARN 2
#define OTEL_INTERNAL_LOG_LEVEL_INFO 3
#define OTEL_INTERNAL_LOG_LEVEL_DEBUG 4
#ifndef OTEL_INTERNAL_LOG_LEVEL
// DEBUG by default, we can change log level on runtime
# define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_DEBUG
Expand All @@ -29,16 +30,19 @@ namespace internal_log

enum class LogLevel
{
Error = 0,
Warning,
Info,
Debug
None = OTEL_INTERNAL_LOG_LEVEL_NONE,
marcalff marked this conversation as resolved.
Show resolved Hide resolved
Error = OTEL_INTERNAL_LOG_LEVEL_ERROR,
Warning = OTEL_INTERNAL_LOG_LEVEL_WARN,
Info = OTEL_INTERNAL_LOG_LEVEL_INFO,
Debug = OTEL_INTERNAL_LOG_LEVEL_DEBUG
};

inline std::string LevelToString(LogLevel level)
{
switch (level)
{
case LogLevel::None:
return "None";
case LogLevel::Error:
return "Error";
case LogLevel::Warning:
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/common/global_log_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void DefaultLogHandler::Handle(LogLevel level,
}
output_s << std::endl;
// TBD - print attributes
std::cout << output_s.str(); // thread safe.
std::cerr << output_s.str(); // thread safe.
marcalff marked this conversation as resolved.
Show resolved Hide resolved
}

void NoopLogHandler::Handle(LogLevel,
Expand Down
8 changes: 8 additions & 0 deletions sdk/test/common/global_log_handle_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ TEST(GlobalLogHandleTest, CustomLogHandler)
OTEL_INTERNAL_LOG_WARN("Warning message");
EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);

opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(
opentelemetry::sdk::common::internal_log::LogLevel::None);
OTEL_INTERNAL_LOG_ERROR("Error message");
OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes);
OTEL_INTERNAL_LOG_INFO("Info message");
OTEL_INTERNAL_LOG_WARN("Warning message");
EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);

opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(backup_log_handle);
opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(backup_log_level);
}