NopeCharon opened a new pull request, #829: URL: https://github.com/apache/logging-log4cxx/pull/829
Fixes #828 ## Problem When several processes write to the same log file, there is no way to tell which process wrote a logging event. The thread identifier (`%t`) is only unique within a process. The workaround is for each application to put its process id into the MDC or into a configuration variable before configuring Log4cxx, and that value is wrong in a child process created by `fork()`. Also, because `PatternParser` falls back to the longest known prefix, a `%pid` pattern was silently accepted and output the level followed by the text `id` (e.g. `INFOid`). ## Change A new `pattern::ProcessIdPatternConverter` is registered in `PatternLayout` as `pid` and `processId`, the names the Log4j 2 PatternLayout uses: ```properties log4j.appender.A1.layout.ConversionPattern=%d [%pid:%t] %-5p %c - %m%n ``` - It outputs `getpid()` on POSIX systems and `GetCurrentProcessId()` on Windows (including MSYS2). - The identifier is read each time an event is formatted, not cached when the layout is configured. This way a child process created by `fork()` after Log4cxx is configured outputs its own identifier instead of its parent's. - Like other converters, it supports the usual formatting modifiers (e.g. `%-8pid`). Options in braces are ignored. Documentation: - `PatternLayout` class reference: a new row in the conversion word table. - Multiple process logging page: shows how to include `%pid` in the pattern. - Change log entry under 2.0.0 new features. ## Testing New `PatternLayoutTest::testProcessId`: formats `[%pid] [%processId] %p %m` and `[%-12pid]` and compares the output with the process identifier of the test. Without the registration in `patternlayout.cpp`, the test fails. On Ubuntu 24.04 (GCC 13), the full `ctest` suite passes (70/70) and the build has no new warnings. `patternlayouttest` also builds and passes with `-DCMAKE_CXX_STANDARD=11 -DLOG4CXX_CHAR=wchar_t`. End to end: a program configured by `PropertyConfigurator` with the pattern `[%pid] [%-8processId] %p %m%n` logs a message, calls `fork()`, and logs again in the child. Each line shows the `getpid()` value of the process that wrote it: ``` parent getpid=3264 [3264] [3264 ] INFO from parent child getpid=3265 [3265] [3265 ] INFO from child ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
