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

Logging MDC Entries in the Patternformatter writes the parameter as a literal, not the value #79

Open
forestphoenix opened this issue May 10, 2024 · 0 comments

Comments

@forestphoenix
Copy link

forestphoenix commented May 10, 2024

Hi,

I'm trying to log an application-specific context parameter using the MDC API.

However, when I try to log an MDC entry using the PatternFormatter, like so:

Log4Qt::PatternLayout* layout
        = new Log4Qt::PatternLayout( "%d{yyyy-MM-dd HH:mm:ss.zzz} [%X{requestId}] %p %c %x - %m%n" );

I get output like this:

2024-05-10 13:03:43.524 [{requestId}] DEBUG ProgramName - <Application-Specific Log>

I would expect the output to contain the actual requestId from the MDC, not the placeholder.

Digging a bit into the source code, it seems the parser for the PatternFormatter does not parse an option in the pattern for the MDCPatternConverter.

I managed to get it to produce the expected output with just the following change in patterformatter.cpp:

@@ -327,8 +327,8 @@ LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::PatternFormatter)
 
 PatternFormatter::PatternFormatter(const QString &rPattern) :
     mIgnoreCharacters(QLatin1String("CFlLM")),
-    mConversionCharacters(QLatin1String("cdmprtxX")),
-    mOptionCharacters(QLatin1String("cd")),
+    mConversionCharacters(QLatin1String("cdmprtx")),
+    mOptionCharacters(QLatin1String("cdX")),
     mPattern(rPattern),
     mPatternConverters()
 {

If this change is correct, it'd be great to have this brought upstream.
I'm happy to create a merge request, if needed. Even if it is just these two lines.

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

No branches or pull requests

1 participant