MEONMedical / Log4Qt

Log4Qt - Logging for the Qt cross-platform application framework
Apache License 2.0
584 stars 234 forks source link

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

Open forestphoenix opened 6 months ago

forestphoenix commented 6 months ago

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.