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

Added Mapped Diagnostic Context (MDC) support #2907

Merged
merged 6 commits into from
Mar 18, 2024

Conversation

massimiliano96
Copy link
Contributor

@massimiliano96 massimiliano96 commented Oct 13, 2023

Description

This pull request introduces support for Mapped Diagnostic Context (MDC). MDC allows users to associate contextual information with log events, providing valuable insights into specific execution flows.

Changes:

Added mdc_formatter class to handle MDC formatting.
Introduced MDC class to manage contextual information.
Provided test cases to validate MDC functionality.

Usage:

// Setting MDC values
spdlog::mdc::put("mdc_key_1", "mdc_value_1");
spdlog::mdc::put("mdc_key_2", "mdc_value_2");

// Creating a logger with MDC support
auto formatter = std::make_shared<spdlog::pattern_formatter>("\n");
formatter->set_pattern("[%n] [%l] [%&] %v");

// Logging a message
memory_buf_t formatted;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);

// Expected output: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message

// Removal of an MDC value
spdlog::mdc::remove("mdc_key_2");

Test Cases:

  • Validated MDC formatting with multiple values.
  • Checked MDC value update.
  • Tested MDC behavior across different threads.
  • Verified MDC key removal.
  • Empty MDC

@massimiliano96
Copy link
Contributor Author

massimiliano96 commented Oct 23, 2023

@gabime Can I do something about this PR?

@gabime
Copy link
Owner

gabime commented Oct 23, 2023

@massimiliano96 I don't have time to look at it these days, since I am under missile attacks 24x7 launched by the Hamas terrorists.

@massimiliano96
Copy link
Contributor Author

@gabime I'm really sorry, I had no idea you were in Israel. My thoughts are with you and your family during this terrible situation. Stay safe.

@massimiliano96 massimiliano96 changed the base branch from v1.x to v2.x March 6, 2024 07:36
@massimiliano96 massimiliano96 changed the base branch from v2.x to v1.x March 6, 2024 07:37
include/spdlog/mdc.h Show resolved Hide resolved
include/spdlog/pattern_formatter-inl.h Show resolved Hide resolved
include/spdlog/pattern_formatter.h Outdated Show resolved Hide resolved
@gabime gabime merged commit d03eb40 into gabime:v1.x Mar 18, 2024
8 checks passed
@gabime
Copy link
Owner

gabime commented Mar 18, 2024

Thanks @massimiliano96 !

@massimiliano96
Copy link
Contributor Author

Thanks to you @gabime ! I'm going to create another PR for example.cpp and updated docs.

gabime added a commit that referenced this pull request Mar 29, 2024
@bachittle bachittle mentioned this pull request Apr 26, 2024
7 tasks
@sketch34
Copy link

@massimiliano96 just checking, will this work correctly with async_logger? Since the formatting will occur on a thread in the thread pool which will be different from the thread that spdlog::mdc::put() was called on.

@gabime
Copy link
Owner

gabime commented Apr 30, 2024

You are right @sketch34. It is not supported in async loggers. I updated the docs.

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.

None yet

3 participants