Skip to content

Commit 78947b2

Browse files
authored
[SDK] DefaultLogHandler to print errors to std::cerr, add LogLevel::None (#2622)
1 parent cd22f0f commit 78947b2

File tree

7 files changed

+63
-16
lines changed

7 files changed

+63
-16
lines changed

CHANGELOG.md

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Increment the:
2525
[#2627](https://github.com/open-telemetry/opentelemetry-cpp/pull/2627)
2626
* [PROTO] Upgrade to opentelemetry-proto v1.2.0
2727
[#2631](https://github.com/open-telemetry/opentelemetry-cpp/pull/2631)
28+
* [SDK] DefaultLogHandler to print errors to std::cerr, add LogLevel::None
29+
[#2622](https://github.com/open-telemetry/opentelemetry-cpp/pull/2622)
2830

2931
Important changes:
3032

@@ -45,6 +47,23 @@ Important changes:
4547
* As part of this change, the script `ci/setup_cmake.sh` was renamed
4648
to `ci/setup_googletest.sh`, for clarity, now that this script
4749
only installs googletest.
50+
* [SDK] DefaultLogHandler to print to std::cerr, add LogLevel::None
51+
[#2622](https://github.com/open-telemetry/opentelemetry-cpp/pull/2622)
52+
* Change DefaultLogHandler output
53+
* Before, the default internal logger, DefaultLogHandler,
54+
used to print to std::cout.
55+
* Now, DefaultLogHandler prints errors and warnings to std::cerr,
56+
as expected, while printing info and debug messages to std::cout.
57+
* Applications that expected to find the opentelemetry-cpp internal
58+
error log in std::cout may need adjustments, either by looking
59+
at std::cerr instead, or by using a custom log handler.
60+
* Additional LogLevel::None
61+
* LogLevel::None is a new supported log level, which does not print
62+
any message.
63+
* Custom log handlers may need to implement a new case, to avoid
64+
compiler warnings.
65+
* Numbering of log levels like OTEL_INTERNAL_LOG_LEVEL_ERROR
66+
has changed, which requires to rebuild, as the SDK ABI differs.
4867

4968
## [1.14.2] 2024-02-27
5069

exporters/ostream/test/ostream_log_test.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ TEST(OStreamLogRecordExporter, Shutdown)
4949
auto exporter =
5050
std::unique_ptr<sdklogs::LogRecordExporter>(new exporterlogs::OStreamLogRecordExporter);
5151

52-
// Save cout's original buffer here
53-
std::streambuf *original = std::cout.rdbuf();
52+
// Save cerr original buffer here
53+
std::streambuf *original = std::cerr.rdbuf();
5454

55-
// Redirect cout to our stringstream buffer
55+
// Redirect cerr to our stringstream buffer
5656
std::stringstream output;
57-
std::cout.rdbuf(output.rdbuf());
57+
std::cerr.rdbuf(output.rdbuf());
5858

5959
EXPECT_TRUE(exporter->Shutdown());
6060

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

6666
// Restore original stringstream buffer
67-
std::cout.rdbuf(original);
67+
std::cerr.rdbuf(original);
6868
std::string err_message =
6969
"[Ostream Log Exporter] Exporting 1 log(s) failed, exporter is shutdown";
7070
EXPECT_TRUE(output.str().find(err_message) != std::string::npos);

exporters/ostream/test/ostream_span_test.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ TEST(OStreamSpanExporter, Shutdown)
4646
auto recordable = processor->MakeRecordable();
4747
recordable->SetName("Test Span");
4848

49-
// Capture the output of cout
50-
const auto captured = WithOStreamCapture(std::cout, [&]() {
49+
// Capture the output of cerr
50+
const auto captured = WithOStreamCapture(std::cerr, [&]() {
5151
EXPECT_TRUE(processor->Shutdown());
5252
processor->OnEnd(std::move(recordable));
5353
});

functional/otlp/func_http_main.cc

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class TestLogHandler : public opentelemetry::sdk::common::internal_log::LogHandl
128128

129129
switch (level)
130130
{
131+
case opentelemetry::sdk::common::internal_log::LogLevel::None:
132+
break;
131133
case opentelemetry::sdk::common::internal_log::LogLevel::Error:
132134
std::cout << " - [E] " << msg << std::endl;
133135
parse_error_msg(&g_test_result, msg);

sdk/include/opentelemetry/sdk/common/global_log_handler.h

+12-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
#include "opentelemetry/sdk/common/attribute_utils.h"
1111
#include "opentelemetry/version.h"
1212

13-
#define OTEL_INTERNAL_LOG_LEVEL_ERROR 0
14-
#define OTEL_INTERNAL_LOG_LEVEL_WARN 1
15-
#define OTEL_INTERNAL_LOG_LEVEL_INFO 2
16-
#define OTEL_INTERNAL_LOG_LEVEL_DEBUG 3
13+
#define OTEL_INTERNAL_LOG_LEVEL_NONE 0
14+
#define OTEL_INTERNAL_LOG_LEVEL_ERROR 1
15+
#define OTEL_INTERNAL_LOG_LEVEL_WARN 2
16+
#define OTEL_INTERNAL_LOG_LEVEL_INFO 3
17+
#define OTEL_INTERNAL_LOG_LEVEL_DEBUG 4
1718
#ifndef OTEL_INTERNAL_LOG_LEVEL
1819
// DEBUG by default, we can change log level on runtime
1920
# define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_DEBUG
@@ -29,16 +30,19 @@ namespace internal_log
2930

3031
enum class LogLevel
3132
{
32-
Error = 0,
33-
Warning,
34-
Info,
35-
Debug
33+
None = OTEL_INTERNAL_LOG_LEVEL_NONE,
34+
Error = OTEL_INTERNAL_LOG_LEVEL_ERROR,
35+
Warning = OTEL_INTERNAL_LOG_LEVEL_WARN,
36+
Info = OTEL_INTERNAL_LOG_LEVEL_INFO,
37+
Debug = OTEL_INTERNAL_LOG_LEVEL_DEBUG
3638
};
3739

3840
inline std::string LevelToString(LogLevel level)
3941
{
4042
switch (level)
4143
{
44+
case LogLevel::None:
45+
return "None";
4246
case LogLevel::Error:
4347
return "Error";
4448
case LogLevel::Warning:

sdk/src/common/global_log_handler.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,21 @@ void DefaultLogHandler::Handle(LogLevel level,
3333
}
3434
output_s << std::endl;
3535
// TBD - print attributes
36-
std::cout << output_s.str(); // thread safe.
36+
37+
switch (level)
38+
{
39+
case LogLevel::Error:
40+
case LogLevel::Warning:
41+
std::cerr << output_s.str(); // thread safe.
42+
break;
43+
case LogLevel::Info:
44+
case LogLevel::Debug:
45+
std::cout << output_s.str(); // thread safe.
46+
break;
47+
case LogLevel::None:
48+
default:
49+
break;
50+
}
3751
}
3852

3953
void NoopLogHandler::Handle(LogLevel,

sdk/test/common/global_log_handle_test.cc

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ TEST(GlobalLogHandleTest, CustomLogHandler)
6262
OTEL_INTERNAL_LOG_WARN("Warning message");
6363
EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);
6464

65+
opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(
66+
opentelemetry::sdk::common::internal_log::LogLevel::None);
67+
OTEL_INTERNAL_LOG_ERROR("Error message");
68+
OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes);
69+
OTEL_INTERNAL_LOG_INFO("Info message");
70+
OTEL_INTERNAL_LOG_WARN("Warning message");
71+
EXPECT_EQ(before_count + 5, static_cast<CustomLogHandler *>(custom_log_handler.get())->count);
72+
6573
opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(backup_log_handle);
6674
opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogLevel(backup_log_level);
6775
}

0 commit comments

Comments
 (0)