-
Notifications
You must be signed in to change notification settings - Fork 5.5k
logger: Make log prefix configurable #10693
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
Changes from 3 commits
0216fd8
b868adb
8b15eb6
4231460
d377f95
a6adf91
eae658a
8db544f
db22fff
0c1145e
4a687ea
ad8d42b
59f4b75
cf502d7
0155c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,15 +47,28 @@ void DelegatingLogSink::set_formatter(std::unique_ptr<spdlog::formatter> formatt | |
| formatter_ = std::move(formatter); | ||
| } | ||
|
|
||
| void DelegatingLogSink::log(const spdlog::details::log_msg& msg) { | ||
| absl::ReleasableMutexLock lock(&format_mutex_); | ||
| absl::string_view msg_view = absl::string_view(msg.payload.data(), msg.payload.size()); | ||
| void DelegatingLogSink::log(const spdlog::details::log_msg& msg_candidate) { | ||
| const spdlog::details::log_msg* msg = &msg_candidate; | ||
| // This memory buffer is used for compatibility and has to be in the scope of the function to | ||
| // avoid use-after-free. | ||
| // TODO(euroelessar): Delete this logic after after 0.16 release. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0.16 release of what? spdlog? Envoy 1.16? Also, what is expected to change at that time which makes it ok to delete this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two envoy releases from now, based on the breaking change policy). This logic can be deleted if it is acceptable to not preserve old behavior (and not provide an ability to revert to old behavior). |
||
| std::string msg_payload_with_prefix; | ||
| spdlog::details::log_msg msg_with_prefix = msg_candidate; | ||
| if (prefix_with_location_) { | ||
| msg_payload_with_prefix = | ||
| fmt::format("[{}:{}] {}", msg->source.filename, msg->source.line, | ||
| absl::string_view(msg->payload.data(), msg->payload.size())); | ||
| msg_with_prefix.payload = msg_payload_with_prefix; | ||
| msg = &msg_with_prefix; | ||
| } | ||
|
|
||
| absl::ReleasableMutexLock lock(&format_mutex_); | ||
| absl::string_view msg_view = absl::string_view(msg->payload.data(), msg->payload.size()); | ||
| // This memory buffer must exist in the scope of the entire function, | ||
| // otherwise the string_view will refer to memory that is already free. | ||
| spdlog::memory_buf_t formatted; | ||
| if (formatter_) { | ||
| formatter_->format(msg, formatted); | ||
| formatter_->format(*msg, formatted); | ||
| msg_view = absl::string_view(formatted.data(), formatted.size()); | ||
| } | ||
| lock.Release(); | ||
|
|
@@ -87,9 +100,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 prefix_with_location) | ||
| : log_level_(log_level), log_format_(log_format), lock_(lock), should_escape_(should_escape), | ||
| save_context_(current_context) { | ||
| prefix_with_location_(prefix_with_location), save_context_(current_context) { | ||
| current_context = this; | ||
| activate(); | ||
| } | ||
|
|
@@ -106,6 +119,7 @@ Context::~Context() { | |
| void Context::activate() { | ||
| Registry::getSink()->setLock(lock_); | ||
| Registry::getSink()->set_should_escape(should_escape_); | ||
| Registry::getSink()->set_prefix_with_location(prefix_with_location_); | ||
| Registry::setLogLevel(log_level_); | ||
| Registry::setLogFormat(log_format_); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be inverted, so that the default unset value of false keeps the same behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a temporary flag, I plan to invert the default in 1 release from now and delete in 2 releases from now (following the breaking change policy).