Problem/Motivation
There is a line of code $context['user'] = $this->currentUser; inside of LoggerChannel::log() that was introduced as part of #1289536: Switch Watchdog to a PSR-3 logging framework.
When the log message includes some type of wildcard referencing {user*}, then \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders() converts {users*} to @users* and then, because we have a context key 'user', it tries to put the AccountProxy object in there, which gives you this recoverable fatal error like: AccountProxy could not be converted to string
I tracked the code of the dblog and syslog and there is no use of the user object from the context array, just the uid. This makes sense since the username of a user can change, the only thing that does not vary is the user id.
Proposed resolution
- Remove the non used line.
- Create a change record that explicit indicates this lines was removed in case a contrib module was depending on this.
Please also credit to @penyaskito and @berdir in this issue.
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | 2986294-20.patch | 801 bytes | dagmar |
| #10 | interdiff.txt | 540 bytes | pritishkumar |
| #10 | 2986294-10.patch | 1.18 KB | pritishkumar |
| #6 | 2986294-6.patch | 1.17 KB | dagmar |
| #6 | 2986294-6-test-only.patch | 656 bytes | dagmar |
Comments
Comment #2
dagmarComment #3
dagmarChange record added.
Comment #4
zuhair_akAdded the patch removing the line
Comment #5
dagmarThanks @zuhair_ak.
Here is a test only version that exposes the problem.
Please add @berdir and @penyaskito to issue credits since they reported the problem before in #2980802 and #2703877.
Comment #6
dagmarThat didn't work as expected.
Comment #8
dagmarComment #9
msankhala commentedI have two small observations. One line function summary is exceeding 80 characters. Changing the
variable => keywill reduce the length.Same as above.
Comment #10
pritishkumar commented"Test that 'user' variable is no longer available by default in context array."
I could see this comment summary only in core/modules/syslog/tests/src/Kernel/SyslogTest.php file.
Changed accordingly.
Comment #11
zuhair_akChanging the status to 'Needs Review'
Comment #12
dagmarComment #13
dagmarThe change record is done. The patch looks good, and I checked a few logging modules (mongodb, monolog, redis_watchdog, simple_access_log) and it seems they are not using $context['user'] as part of the logger.
Comment #14
catchI don't think we need to add a test here. It's useful to have the failing test in this issue to show the bug, but in terms of adding the code to core, it's adding a test for something that no-longer exists in the code base.
Comment #15
dagmarThanks @catch. In that case patch #4 is the one to go.
Comment #16
catchThanks. From #13 it looks like contrib loggers are using this? If they are then we might not be able to remove this.
Comment #17
dagmarSorry I meant to say they are not using this feature.
Comment #18
alexpottIn \Drupal\Core\Logger\LoggerChannel::log() we're still merging in a
userdefault. Should we be doing that? In some ways it acts as a BC layer but, on the other hand, it is super confusing.Comment #19
alexpottAlso, whilst we are here can we create a follow up to discuss removing the try{ } catch {}
As far as I can see there is never a situation anymore when getting the ID from the current_user service can throw an exception. This was resolved in #2753733: AccountProxy can do unnecessary user loads to get an ID
Comment #20
dagmarLet's see if the removal of 'user' from $context doesn't have side effects with the current tests.
Comment #21
msankhala commentedI can confirm that the patch #20 applies cleanly and remove the $context['user']. After applying the patch I was not able to find any reference to user in $context.
Comment #22
catchThe follow-up: #2998727: Remove unnecessary try/catch from logger.
Comment #23
catchCommitted 3fa89d6 and pushed to 8.7.x. Thanks!
Comment #25
longwaveAlso opened #2998731: Remove $context['user'] from DbLogTest and FakeLogEntries as a followup for some test classes that still include the 'user' parameter.
Comment #27
quietone commentedpublish the change record