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: support log control in admin interface and command line option for Fancy Logger #12369

Merged
merged 95 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
d618b96
Latest change from patch; fix DCO.
PrinceS17 Jul 10, 2020
37a250d
Merge branch 'master' into fancy-pr
PrinceS17 Jul 10, 2020
7a19aec
Polich 2nd round: comments.
PrinceS17 Jul 11, 2020
f8557e2
Second round polish: thread annotation & context.
PrinceS17 Jul 14, 2020
d75359f
Second round: polish some missing comments.
PrinceS17 Jul 14, 2020
e86ec61
Add listFancyLoggers() for all loggers information.
PrinceS17 Jul 15, 2020
688f9ce
Fix FANCY_LOG comment.
PrinceS17 Jul 15, 2020
2c33098
Add comment for getFancyLogEntry.
PrinceS17 Jul 15, 2020
17f843a
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 15, 2020
50a1b13
Add list loggers & admin hook.
PrinceS17 Jul 17, 2020
5a30d45
Third round: default fancy level, etc.
PrinceS17 Jul 17, 2020
c1bac45
Format fix in fancy_logger.cc.
PrinceS17 Jul 18, 2020
0eef816
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 18, 2020
79de63b
Add test for setting & getting default level & format.
PrinceS17 Jul 18, 2020
be431c3
Change _ to tmp for clang-tidy.
PrinceS17 Jul 18, 2020
dcfdc53
Add log setting test & setAllFancyLoggers.
PrinceS17 Jul 18, 2020
d6eaf5c
Add & in for loop for clang-tidy.
PrinceS17 Jul 18, 2020
6d48e60
Modify logs handler and test.
PrinceS17 Jul 18, 2020
7567782
Use const auto& for perf_test.
PrinceS17 Jul 18, 2020
500bde0
Add void for _ usage.
PrinceS17 Jul 18, 2020
7222924
Add unit tests for FancyContext and Context; fix test for logs handler.
PrinceS17 Jul 20, 2020
30233a2
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 20, 2020
b2d2ab1
Add test for logger listing api.
PrinceS17 Jul 21, 2020
6b0c7ee
Add compile option for ENVOY_LOG.
PrinceS17 Jul 22, 2020
e6cdcff
Trivial change to test again..
PrinceS17 Jul 22, 2020
af43922
Merge branch 'master' into fancy-pr
PrinceS17 Jul 23, 2020
1b996a6
Use setShouldEscape to comply with new master.
PrinceS17 Jul 23, 2020
df470d2
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 23, 2020
3aa900e
Hook compile option with main_common.
PrinceS17 Jul 23, 2020
5c3f6d6
Set logger mode along with compile option (testing).
PrinceS17 Jul 23, 2020
aa69a58
Trivial comment change to retest.
PrinceS17 Jul 23, 2020
f86e8d0
Fourth round: convert FancyContext into singleton.
PrinceS17 Jul 23, 2020
1410fa6
Convert fancy default to non-static.
PrinceS17 Jul 23, 2020
e4d8b0e
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 23, 2020
1ba45df
Fix format.
PrinceS17 Jul 23, 2020
64b7dcc
Adjust macro.h for format.
PrinceS17 Jul 24, 2020
c07c27b
Merge branch 'master' into fancy-pr
PrinceS17 Jul 24, 2020
8e176bf
Fix get default level/format when Context is not instantiated.
PrinceS17 Jul 24, 2020
371a241
Add do{} while(0) for flush.
PrinceS17 Jul 24, 2020
1284d63
Add do{} while(0) for flush.
PrinceS17 Jul 24, 2020
09d487c
Merge branch 'fancy-pr' into fancy-admin
PrinceS17 Jul 24, 2020
73170e0
Change logger_mode_ to static int to avoid null current_context.
PrinceS17 Jul 24, 2020
4a34abd
Merge branch 'master' into fancy-pr
PrinceS17 Jul 24, 2020
28b83cb
Merge branch 'master' into fancy-admin
PrinceS17 Jul 24, 2020
c0f99f9
Polish macros test.
PrinceS17 Jul 24, 2020
fa6592b
Comment filter log.
PrinceS17 Jul 24, 2020
8283b20
Merge branch 'master' into fancy-pr
PrinceS17 Jul 24, 2020
aad7d0d
Merge branch 'master' into fancy-pr
PrinceS17 Jul 25, 2020
ab8c66d
Merge branch 'master' into fancy-admin
PrinceS17 Jul 25, 2020
1767fcf
Compile option added & fix deadlock of setLoggerMode().
PrinceS17 Jul 25, 2020
22640ac
Fix test of default level setting & add test for compile option.
PrinceS17 Jul 25, 2020
b71317c
Manually fix main_common.cc.
PrinceS17 Jul 27, 2020
1331889
Add level & format update in activate().
PrinceS17 Jul 27, 2020
aadd93e
Split the build target.
PrinceS17 Jul 27, 2020
458011f
Fix format.
PrinceS17 Jul 27, 2020
0446dcd
Merge branch 'master' into fancy-pr
PrinceS17 Jul 27, 2020
d5f006e
Trivial change to retest.
PrinceS17 Jul 28, 2020
e791d56
Remove benchmark dependency for test's BUILD; rename & polish perform…
PrinceS17 Jul 28, 2020
f4270c2
Merge branch 'master' into fancy-admin
PrinceS17 Jul 28, 2020
8e6c5da
Merge branch 'fancy-pr' into fancy-admin, resolve BUILD & fix compile…
PrinceS17 Jul 28, 2020
ad10f32
Overwrite more log macros & test.
PrinceS17 Jul 28, 2020
f9d965f
Merge branch 'master' into fancy-admin, fix conflict & add comments i…
PrinceS17 Jul 29, 2020
19766ee
Add some comments & fancy logger doc.
PrinceS17 Jul 30, 2020
259035d
Merge branch 'master' into fancy-admin & fix format.
PrinceS17 Jul 30, 2020
62c9303
Fix build bug: add comma.
PrinceS17 Jul 30, 2020
cc45a39
Fix format.
PrinceS17 Jul 31, 2020
5ec2ebc
Fix log handler test.
PrinceS17 Jul 31, 2020
d80b910
Remove some default related test.
PrinceS17 Jul 31, 2020
ffd180f
Merge branch 'master' into fancy-admin
PrinceS17 Jul 31, 2020
c9420af
Merge branch 'master' into fancy-admin
PrinceS17 Jul 31, 2020
0bd0bfa
Merge branch 'master' into fancy-admin
PrinceS17 Aug 1, 2020
26f5835
Second PR 1st round: polish test, doc, etc.
PrinceS17 Aug 4, 2020
a374840
Merge branch 'master' into fancy-admin
PrinceS17 Aug 4, 2020
ea0d546
Second PR: cmd option draft.
PrinceS17 Aug 11, 2020
e0f2428
Second PR: fix proto, tests added.
PrinceS17 Aug 11, 2020
56af4a6
Second PR: add cmd option test in log_macros_test.
PrinceS17 Aug 11, 2020
f9b2420
Add changes in generated_api_shadow.
PrinceS17 Aug 11, 2020
6e31c22
Fix format.
PrinceS17 Aug 11, 2020
32065ab
Fix format again.
PrinceS17 Aug 11, 2020
8b2c134
Second PR: fix API names.
PrinceS17 Aug 11, 2020
b1844c7
Fix logMode.
PrinceS17 Aug 11, 2020
b90fcd2
Second PR: fix logger format, doc, and macro.
PrinceS17 Aug 11, 2020
687e2a9
Fix format.
PrinceS17 Aug 12, 2020
a7f9fd7
Merge branch 'master' into fancy-admin
PrinceS17 Aug 12, 2020
c3fd993
Add enableFancyLog in options.h.
PrinceS17 Aug 12, 2020
1f76b0b
Fix wrong mode in logs handler.
PrinceS17 Aug 12, 2020
ac3944c
Fix sigfault caused by null current_context in benchmark tests.
PrinceS17 Aug 13, 2020
17008e9
Fix the wrong description of path in doc.
PrinceS17 Aug 13, 2020
f575fe8
Add Initialization of enable_fancy_log_ to fix asan & compile time test.
PrinceS17 Aug 13, 2020
d8d6b2a
Change all enable*fancy*log to enable*fine*grain*logging.
PrinceS17 Aug 13, 2020
1e8ff0a
Fix format.
PrinceS17 Aug 13, 2020
a433f44
Add docs.
PrinceS17 Aug 13, 2020
56711db
Fix format of current.rst.
PrinceS17 Aug 13, 2020
c3668ec
Fix MockOptions of enable fine grain logging.
PrinceS17 Aug 13, 2020
3774f27
Merge branch 'master' into fancy-admin
PrinceS17 Aug 14, 2020
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
5 changes: 4 additions & 1 deletion api/envoy/admin/v3/server_info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ message ServerInfo {
CommandLineOptions command_line_options = 6;
}

// [#next-free-field: 34]
// [#next-free-field: 35]
message CommandLineOptions {
option (udpa.annotations.versioning).previous_message_type =
"envoy.admin.v2alpha.CommandLineOptions";
Expand Down Expand Up @@ -176,4 +176,7 @@ message CommandLineOptions {

// See :option:`--bootstrap-version` for details.
uint32 bootstrap_version = 29;

// See :option:`--enable-fine-grain-logging` for details.
bool enable_fine_grain_logging = 34;
}
5 changes: 4 additions & 1 deletion api/envoy/admin/v4alpha/server_info.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/root/operations/admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ modify different aspects of the server:

.. note::

Generally only used during development.
Generally only used during development. With `--enable-fine-grain-logging` being set, the logger is represented
by the path of the file it belongs to (to be specific, the path determined by `__FILE__`), so the logger list
will show a list of file paths, and the specific path should be used as <logger_name> to change the log level.

.. http:get:: /memory

Expand Down
7 changes: 7 additions & 0 deletions docs/root/operations/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ following are the command line options that Envoy supports.
The :ref:`hot restart wrapper <operations_hot_restarter>` sets the *RESTART_EPOCH* environment
variable which should be passed to this option in most cases.

.. option:: --enable-fine-grain-logging
*(optional)* Enables fine-grain logger with file level log control and runtime update at administration
interface. If enabled, main log macros including `ENVOY_LOG`, `ENVOY_CONN_LOG`, `ENVOY_STREAM_LOG` and
`ENVOY_FLUSH_LOG` will use a per-file logger, and the usage doesn't need `Envoy::Logger::Loggable` any
more. The administration interface usage is similar. Please see `Administration interface
<https://www.envoyproxy.io/docs/envoy/latest/operations/admin>`_ for more detail.

.. option:: --hot-restart-version

*(optional)* Outputs an opaque hot restart compatibility version for the binary. This can be
Expand Down
1 change: 1 addition & 0 deletions docs/root/version_history/current.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Minor Behavior Changes
* http: fixed the 100-continue response path to properly handle upstream failure by sending 5xx responses. This behavior can be temporarily reverted by setting `envoy.reloadable_features.allow_500_after_100` to false.
* http: the per-stream FilterState maintained by the HTTP connection manager will now provide read/write access to the downstream connection FilterState. As such, code that relies on interacting with this might
see a change in behavior.
* logging: add fine-grain logging for file level log control with logger management at administration interface. It can be enabled by option `--enable-fine-grain-logging`.
* logging: change default log format to `"[%Y-%m-%d %T.%e][%t][%l][%n] [%g:%#] %v"` and default value of :option:`--log-format-prefix-with-location` to `0`.
* logging: nghttp2 log messages no longer appear at trace level unless `ENVOY_NGHTTP2_TRACE` is set
in the environment.
Expand Down
5 changes: 4 additions & 1 deletion generated_api_shadow/envoy/admin/v3/server_info.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion generated_api_shadow/envoy/admin/v4alpha/server_info.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions include/envoy/server/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ class Options {
*/
virtual bool logFormatEscaped() const PURE;

/**
* @return const bool logger mode: whether to use Fancy Logger.
*/
virtual bool enableFineGrainLogging() const PURE;

/**
* @return const std::string& the log file path.
*/
Expand Down
27 changes: 8 additions & 19 deletions source/common/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,19 @@ envoy_cc_library(
# Contains minimal code for logging to stderr.
envoy_cc_library(
name = "minimal_logger_lib",
srcs = ["logger.cc"],
hdrs = ["logger.h"],
external_deps = ["abseil_synchronization"],
deps = [
":base_logger_lib",
":lock_guard_lib",
":macros",
":non_copyable",
] + select({
"//bazel:android_logger": ["logger_impl_lib_android"],
"//conditions:default": ["logger_impl_lib_standard"],
}),
)

envoy_cc_library(
name = "fancy_logger_lib",
srcs = ["fancy_logger.cc"],
hdrs = ["fancy_logger.h"],
srcs = [
"fancy_logger.cc",
"logger.cc",
],
hdrs = [
"fancy_logger.h",
"logger.h",
],
external_deps = ["abseil_synchronization"],
deps = [
":base_logger_lib",
":lock_guard_lib",
":macros",
":minimal_logger_lib",
":non_copyable",
] + select({
"//bazel:android_logger": ["logger_impl_lib_android"],
Expand Down
23 changes: 22 additions & 1 deletion source/common/common/fancy_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class FancyBasicLockable : public Thread::BasicLockable {
SpdLoggerSharedPtr FancyContext::getFancyLogEntry(std::string key)
ABSL_LOCKS_EXCLUDED(fancy_log_lock_) {
absl::ReaderMutexLock l(&fancy_log_lock_);
return fancy_log_map_->find(key)->second;
auto it = fancy_log_map_->find(key);
if (it != fancy_log_map_->end()) {
return it->second;
}
return nullptr;
}

void FancyContext::initFancyLogger(std::string key, std::atomic<spdlog::logger*>& logger)
Expand Down Expand Up @@ -69,6 +73,23 @@ void FancyContext::setDefaultFancyLevelFormat(spdlog::level::level_enum level, s
}
}

std::string FancyContext::listFancyLoggers() ABSL_LOCKS_EXCLUDED(fancy_log_lock_) {
std::string info = "";
absl::ReaderMutexLock l(&fancy_log_lock_);
for (const auto& it : *fancy_log_map_) {
info += fmt::format(" {}: {}\n", it.first, static_cast<int>(it.second->level()));
}
return info;
}

void FancyContext::setAllFancyLoggers(spdlog::level::level_enum level)
ABSL_LOCKS_EXCLUDED(fancy_log_lock_) {
absl::ReaderMutexLock l(&fancy_log_lock_);
for (const auto& it : *fancy_log_map_) {
it.second->set_level(level);
}
}

void FancyContext::initSink() {
spdlog::sink_ptr sink = Logger::Registry::getSink();
Logger::DelegatingLogSinkSharedPtr sp = std::static_pointer_cast<Logger::DelegatingLogSink>(sink);
Expand Down
14 changes: 13 additions & 1 deletion source/common/common/fancy_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ class FancyContext {
ABSL_LOCKS_EXCLUDED(fancy_log_lock_);

/**
* Sets the default logger level and format when updating context.
* Sets the default logger level and format when updating context. It should only be used in
* Context, otherwise the fancy_default_level will possibly be inconsistent with the actual
* logger level.
*/
void setDefaultFancyLevelFormat(spdlog::level::level_enum level, std::string format)
ABSL_LOCKS_EXCLUDED(fancy_log_lock_);

/**
* Lists keys and levels of all loggers in a string for admin page usage.
*/
std::string listFancyLoggers() ABSL_LOCKS_EXCLUDED(fancy_log_lock_);

/**
* Sets the levels of all loggers.
*/
void setAllFancyLoggers(spdlog::level::level_enum level) ABSL_LOCKS_EXCLUDED(fancy_log_lock_);

private:
/**
* Initializes sink for the initialization of loggers, needed only in benchmark test.
Expand Down
48 changes: 41 additions & 7 deletions source/common/common/logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "absl/strings/ascii.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/strip.h"
#include "spdlog/spdlog.h"

Expand Down Expand Up @@ -110,9 +111,9 @@ DelegatingLogSinkSharedPtr DelegatingLogSink::init() {
static Context* current_context = nullptr;

Context::Context(spdlog::level::level_enum log_level, const std::string& log_format,
Thread::BasicLockable& lock, bool should_escape)
Thread::BasicLockable& lock, bool should_escape, bool enable_fine_grain_logging)
: log_level_(log_level), log_format_(log_format), lock_(lock), should_escape_(should_escape),
save_context_(current_context) {
enable_fine_grain_logging_(enable_fine_grain_logging), save_context_(current_context) {
current_context = this;
activate();
}
Expand All @@ -126,21 +127,54 @@ Context::~Context() {
}
}

void Context::activate(LoggerMode mode) {
void Context::activate() {
Registry::getSink()->setLock(lock_);
Registry::getSink()->setShouldEscape(should_escape_);
Registry::setLogLevel(log_level_);
Registry::setLogFormat(log_format_);

if (mode == LoggerMode::Fancy) {
fancy_default_level_ = log_level_;
fancy_log_format_ = log_format_;
// sets level and format for Fancy Logger
fancy_default_level_ = log_level_;
fancy_log_format_ = log_format_;
if (enable_fine_grain_logging_) {
// loggers with default level before are set to log_level_ as new default
getFancyContext().setDefaultFancyLevelFormat(log_level_, log_format_);
if (log_format_ == Logger::Logger::DEFAULT_LOG_FORMAT) {
fancy_log_format_ = absl::StrReplaceAll(log_format_, {{"[%n]", ""}});
}
}
}

bool Context::useFancyLogger() {
if (current_context) {
return current_context->enable_fine_grain_logging_;
}
return false;
}

void Context::enableFancyLogger() {
current_context->enable_fine_grain_logging_ = true;
if (current_context) {
getFancyContext().setDefaultFancyLevelFormat(current_context->log_level_,
current_context->log_format_);
current_context->fancy_default_level_ = current_context->log_level_;
current_context->fancy_log_format_ = current_context->log_format_;
if (current_context->log_format_ == Logger::Logger::DEFAULT_LOG_FORMAT) {
current_context->fancy_log_format_ =
absl::StrReplaceAll(current_context->log_format_, {{"[%n]", ""}});
}
}
}

void Context::disableFancyLogger() {
if (current_context) {
current_context->enable_fine_grain_logging_ = false;
}
}

std::string Context::getFancyLogFormat() {
if (!current_context) { // Context is not instantiated in benchmark test
return "[%Y-%m-%d %T.%e][%t][%l][%n] %v";
return "[%Y-%m-%d %T.%e][%t][%l] %v";
}
return current_context->fancy_log_format_;
}
Expand Down
Loading