Skip to content

Commit

Permalink
logs: making the admin 'set log level' function a util (envoyproxy#29196
Browse files Browse the repository at this point in the history
)

Risk Level: low (minor refactor)
Testing: existing admin tests, new E-M test
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk authored Aug 24, 2023
1 parent 5cf06ec commit d269666
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
31 changes: 31 additions & 0 deletions mobile/test/common/common/lambda_logger_delegate_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using testing::_;
using testing::HasSubstr;
using testing::Not;

namespace Envoy {
namespace Logger {
Expand Down Expand Up @@ -38,6 +39,36 @@ TEST_F(LambdaDelegateTest, LogCb) {
EXPECT_THAT(actual_msg, HasSubstr(expected_msg));
}

TEST_F(LambdaDelegateTest, LogCbWithLevels) {
std::string unexpected_msg = "Hello NoLambdaDelegate";
std::string expected_msg = "Hello LambdaDelegate";
std::string actual_msg;

LambdaDelegate delegate({[](envoy_data data, const void* context) -> void {
auto* actual_msg =
static_cast<std::string*>(const_cast<void*>(context));
*actual_msg = Data::Utility::copyToString(data);
release_envoy_data(data);
},
[](const void*) -> void {}, &actual_msg},
Registry::getSink());

// Set the log to critical. The message should not be logged.
Context::changeAllLogLevels(spdlog::level::critical);
ENVOY_LOG_MISC(error, unexpected_msg);
EXPECT_THAT(actual_msg, Not(HasSubstr(unexpected_msg)));

// Change to error. The message should be logged.
Context::changeAllLogLevels(spdlog::level::err);
ENVOY_LOG_MISC(error, expected_msg);
EXPECT_THAT(actual_msg, HasSubstr(expected_msg));

// Change back to critical and test one more time.
Context::changeAllLogLevels(spdlog::level::critical);
ENVOY_LOG_MISC(error, expected_msg);
EXPECT_THAT(actual_msg, Not(HasSubstr(unexpected_msg)));
}

TEST_F(LambdaDelegateTest, ReleaseCb) {
bool released = false;

Expand Down
13 changes: 13 additions & 0 deletions source/common/common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ bool Context::useFineGrainLogger() {
return false;
}

void Context::changeAllLogLevels(spdlog::level::level_enum level) {
if (!useFineGrainLogger()) {
ENVOY_LOG_MISC(info, "change all log levels: level='{}'",
spdlog::level::level_string_views[level]);
Registry::setLogLevel(level);
} else {
// Level setting with Fine-Grain Logger.
FINE_GRAIN_LOG(info, "change all log levels: level='{}'",
spdlog::level::level_string_views[level]);
getFineGrainLogContext().setAllFineGrainLoggers(level);
}
}

void Context::enableFineGrainLogger() {
if (current_context) {
current_context->enable_fine_grain_logging_ = true;
Expand Down
3 changes: 3 additions & 0 deletions source/common/common/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ class Context {
*/
static bool useFineGrainLogger();

// Change the log level for all loggers (fine grained or otherwise) to the level provided.
static void changeAllLogLevels(spdlog::level::level_enum level);

static void enableFineGrainLogger();
static void disableFineGrainLogger();

Expand Down
14 changes: 1 addition & 13 deletions source/server/admin/logs_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ absl::Status LogsHandler::changeLogLevel(Http::Utility::QueryParams& params) {
return level_to_use.status();
}

changeAllLogLevels(*level_to_use);
Logger::Context::changeAllLogLevels(*level_to_use);
return absl::OkStatus();
}

Expand Down Expand Up @@ -163,18 +163,6 @@ absl::Status LogsHandler::changeLogLevel(Http::Utility::QueryParams& params) {
return changeLogLevels(name_levels);
}

void LogsHandler::changeAllLogLevels(spdlog::level::level_enum level) {
if (!Logger::Context::useFineGrainLogger()) {
ENVOY_LOG(info, "change all log levels: level='{}'", spdlog::level::level_string_views[level]);
Logger::Registry::setLogLevel(level);
} else {
// Level setting with Fine-Grain Logger.
FINE_GRAIN_LOG(info, "change all log levels: level='{}'",
spdlog::level::level_string_views[level]);
getFineGrainLogContext().setAllFineGrainLoggers(level);
}
}

absl::Status LogsHandler::changeLogLevels(
const absl::flat_hash_map<absl::string_view, spdlog::level::level_enum>& changes) {
if (!Logger::Context::useFineGrainLogger()) {
Expand Down
1 change: 0 additions & 1 deletion source/server/admin/logs_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class LogsHandler : public HandlerContextBase, Logger::Loggable<Logger::Id::admi
* @param params supplies the incoming endpoint query or post params.
*/
absl::Status changeLogLevel(Http::Utility::QueryParams& params);
void changeAllLogLevels(spdlog::level::level_enum level);
absl::Status
changeLogLevels(const absl::flat_hash_map<absl::string_view, spdlog::level::level_enum>& changes);

Expand Down

0 comments on commit d269666

Please sign in to comment.