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

logger: implement Fancy Logger with fine-grained log control #11822

Merged
merged 33 commits into from
Jul 29, 2020

Conversation

PrinceS17
Copy link
Contributor

Commit Message: Implement a new logger called Fancy Logger with fine-grained control, e.g. file, function and line level.

Additional Description: Some new macros are defined for basic usage but not hooked with any existing log macros. API for the logger is still in progress.

Risk Level: Low
Testing: unit test & benchmark
Docs Changes: None
Release Notes: None

@PrinceS17 PrinceS17 marked this pull request as ready for review June 30, 2020 17:45
@PrinceS17 PrinceS17 marked this pull request as draft June 30, 2020 21:40
Copy link
Contributor

@alyssawilk alyssawilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very exciting - all us googlers miss having vlogs :-)

I think as long as benchmarks show that with fancy-mode off the performance changes are negligible we can do this inline. Meanwhile I'd encourage you to check out your CI results (doing a master merge might fix - we were having CI problems last week) @penguingao would you be up for first pass?

Copy link
Contributor

@penguingao penguingao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flushing out some readability comments first. I'll read the code more carefully later.

source/common/common/logger.cc Outdated Show resolved Hide resolved
source/common/common/logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved

extern FancyMapPtr getFancyLogMap();

extern void setFancyLogger(std::string key, spdlog::level::level_enum log_level);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need extern for these functions?

If you simply want to declare and use them in the macros, would it make sense to create a fancy_logger.h and group these just there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed I don't. Addressed now.

source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
source/common/common/logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
Signed-off-by: Jinhui Song <jinhuisong@google.com>
@PrinceS17 PrinceS17 force-pushed the fancy-pr branch 3 times, most recently from e0ed7f8 to d618b96 Compare July 10, 2020 03:55
Signed-off-by: Jinhui Song <jinhuisong@google.com>
@PrinceS17 PrinceS17 marked this pull request as ready for review July 10, 2020 14:44
@PrinceS17
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

🤷‍♀️ nothing to rebuild.

🐱

Caused by: a #11822 (comment) was created by @PrinceS17.

see: more, trace.

source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.cc Outdated Show resolved Hide resolved
Registry::getSink()->setLock(lock_);
Registry::getSink()->set_should_escape(should_escape_);
Registry::setLogLevel(log_level_);
Registry::setLogFormat(log_format_);

if (mode == LoggerMode::Fancy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is the right place to set your default log level. class Context is used to override logging behavior when it is created and restore the previous behavior when it is destructed.

But getFancyDefaultLevel is only used once for each log key (file name), meaning the Context class does not affect fancy log's behavior once fancy log is initialized.

I think the better way is to provide a pair of function in FancyContext that iterates through the loggers and change their log level and format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is added in FancyContext to enable later update of default level/format of different Context, and it's called in activate() now. But I think it's still necessary to set the default level in Context() as the map isn't complete when the 1st Context is initialized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make fancy_default_level_ and fancy_log_format_ non-static member? You can access them via the current_context_ global var. I think this makes it clearer about the life span of the fancy_default_level_ and fancy_log_format_.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems making them non-static needs another variable to represent the previous default level. Currently I set default before setting fancy_default_level so that I can compare new level with the old one.

Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
@PrinceS17
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

🤷‍♀️ nothing to rebuild.

🐱

Caused by: a #11822 (comment) was created by @PrinceS17.

see: more, trace.

Signed-off-by: Jinhui Song <jinhuisong@google.com>
@PrinceS17

This comment has been minimized.

penguingao
penguingao previously approved these changes Jul 16, 2020
Copy link
Contributor

@penguingao penguingao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! modulo some more nits.

source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
spdlog::logger* local_flogger = flogger.load(std::memory_order_relaxed); \
if (!local_flogger) { \
FancyContext::initFancyLogger(FANCY_KEY, flogger); \
flogger.load(std::memory_order_relaxed) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If you want to avoid the duplicated log(spdlog::source_loc{}...) call, you can do local_flogger = flogger.load() here. Remove the else condition. So:

if (!local_flogger) {
FancyContext::initFancyLogger(FANCY_KEY, flogger);
local_flogger = flogger.load(std::memory_order_relaxed);
}
local_flogger.log(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. I didn't thought of that

return false;
}

void FancyContext::setDefaultFancyLevelFormat(spdlog::level::level_enum level, std::string format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to compare the previous default level vs the new default level. So that you can avoid the for loop if nothing changes.

Also, you can compare the logger's log level vs default log level so that you can avoid overriding the log level set to non default value.

Copy link
Contributor Author

@PrinceS17 PrinceS17 Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. About the later comment: if fancy_default_level & fancy_log_format are made non-static, it seems I need another variable for previous default level & format. So I keep them static for now...

Registry::getSink()->setLock(lock_);
Registry::getSink()->set_should_escape(should_escape_);
Registry::setLogLevel(log_level_);
Registry::setLogFormat(log_format_);

if (mode == LoggerMode::Fancy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make fancy_default_level_ and fancy_log_format_ non-static member? You can access them via the current_context_ global var. I think this makes it clearer about the life span of the fancy_default_level_ and fancy_log_format_.

test/common/common/log_macros_test.cc Show resolved Hide resolved
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
@@ -137,8 +137,14 @@ envoy_cc_library(
# Contains minimal code for logging to stderr.
envoy_cc_library(
name = "minimal_logger_lib",
srcs = ["logger.cc"],
hdrs = ["logger.h"],
srcs = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split this to a separate target? this is no longer minimal with fancy logger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the next PR I plan to hook ENVOY_LOG with FANCY_LOG (with option provided to developers), and logically I think Fancy logger will be part of the logger as we need to include fancy_logger.h in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still, in the next PR you can just change their names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean using another name like "fancy_logger_lib" for readability and keep current lib structure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lizan Could you please give some clarification on which names you want me to change (or the lib structure)? Because the change of "minimal_logger_lib" seems unnecessary and very dirty. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this is now looks better.

source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
source/common/common/logger.h Outdated Show resolved Hide resolved
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
source/common/common/fancy_logger.h Outdated Show resolved Hide resolved
@@ -137,8 +137,14 @@ envoy_cc_library(
# Contains minimal code for logging to stderr.
envoy_cc_library(
name = "minimal_logger_lib",
srcs = ["logger.cc"],
hdrs = ["logger.h"],
srcs = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still, in the next PR you can just change their names?

Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: Jinhui Song <jinhuisong@google.com>
Copy link
Member

@lizan lizan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last set of nit, otherwise LGTM

@@ -105,7 +105,9 @@ envoy_cc_test(
envoy_cc_test(
name = "log_macros_test",
srcs = ["log_macros_test.cc"],
external_deps = ["benchmark"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted now.

@@ -114,6 +116,21 @@ envoy_cc_test(
],
)

envoy_cc_benchmark_binary(
name = "logger_perf_test",
srcs = ["logger_perf_test.cc"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We name this *_speed_test for rest benchmarks, can you rename this one too? same for below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

FANCY_LOG(info, "Slow path test begins.");
std::atomic<spdlog::logger*> logger;
for (auto _ : state) {
(void)_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(void)_;
UNREFERENCED_PARAMETER(_);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

…ance test.

Signed-off-by: Jinhui Song <jinhuisong@google.com>
@PrinceS17
Copy link
Contributor Author

@lizan Could you please help merge my branch as all checks have passed? Thanks!

@lizan lizan merged commit c1d116e into envoyproxy:master Jul 29, 2020
chaoqin-li1123 pushed a commit to chaoqin-li1123/envoy that referenced this pull request Aug 7, 2020
…oxy#11822)

Implement a new logger called Fancy Logger with fine-grained control, e.g. file, function and line level.

Additional Description: Some new macros are defined for basic usage but not hooked with any existing log macros. API for the logger is still in progress.

Risk Level: Low
Testing: unit test & benchmark
Docs Changes: None
Release Notes: None

Signed-off-by: Jinhui Song <jinhuisong@google.com>
chaoqin-li1123 pushed a commit to chaoqin-li1123/envoy that referenced this pull request Aug 7, 2020
…oxy#11822)

Implement a new logger called Fancy Logger with fine-grained control, e.g. file, function and line level.

Additional Description: Some new macros are defined for basic usage but not hooked with any existing log macros. API for the logger is still in progress.

Risk Level: Low
Testing: unit test & benchmark
Docs Changes: None
Release Notes: None

Signed-off-by: Jinhui Song <jinhuisong@google.com>
Signed-off-by: chaoqinli <chaoqinli@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants