- 
                Notifications
    You must be signed in to change notification settings 
- Fork 74
PHP 8.2 Migration Prep: Logging Changes to support latest version of Monolog #2086
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
Conversation
c1efe66    to
    1e2a1be      
    Compare
  
    | This seems like a lot of code changes. What was the reason for not putting the adapter code in the log class instead of changing all the files that call the log functions? | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just need explaination of why the code changes were not put in our existing custom shim class that already acts as the layer between out code and monolog code..
| @jpwhite4 lol, I wrote out all the changes except for that one facepalm It definitely is, the reason I didn't put the update in the shim class is because when we finally upgrade to the up to date monolog version w/ Symfony + PHP 8.2 == the function signature for the log methods of  | 
In newer versions of Monolog the shortened versions of the error and critical log methods have been removed so I've updated those usages to the currently supported full name versions
These updates are to prepare for the PHP 8.2 updates / Symfony Migration as the new version of Monolog we're updating to no longer allows us to provide arrays as messages. Thankfully the new version of Monolog does allow for classes that implement `\Stringable` to be provided as a message. To take advantage of this I created a new class called `LogOutput` that implements `\Stringable` and takes / provides as a public member variable an array. This array is used when `__toString()` is called and outputs information as we expect when logging to the console. When logging to the database we instead expect to output json_encoded data. To accomplish this I've added a new "Processor" ( right now this is just an anonymous function, in the Symfony Migration they have a class for these ) to the `CCRDBHandler` that will "enrich" the record being logged by setting a new property called 'formatted' (for the new version of Monolog) prior to CCRDBHandler writing the record to the db. The changes were originally included in the Symfony Migration PR but they don't actually require Symfony to function and will make the Symfony PR smaller.
4dcccbf    to
    f2606ef      
    Compare
  
    | ports: | ||
| - 9006:443 | ||
| - 7000:7000 | ||
| - "8080:443" | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change intended to be in this pull request?
Motivation and Context
The latest version of Monolog utilizes more of PHP's type system which is awesome in general, but causes us some issues since we log arrays instead of strings in many places.
Thankfully, Monolog made the argument typestring|\Stringableand not juststringso there's a way forward that enables us to maintain the logging output we expect to the console, files, and the database while also allowing us to be compliant with the new version of Monolog and keeping the required changes to our code base as minimal as possible. Oh, and all of this is backwards compatible with our current version of Monolog / PHP, hence why this PR can go in prior to the Symfony changes.We're going ahead with making all of our logging calls PSR3 compliant, i.e. no more directly logging arrays as messages. Instead if the previously logged array has a
messageproperty, that is now supplied as the$messageargument, and the rest of the array is provided as the$context.To accommodate these changes
CCRLineFormatterwas updated to to ensure that the$formatwill always have a%context%variable which will in turn be replaced with the same stringified output that was previously used for the array messages, thus maintaining our required formatting for console and file output.Description of Changes
Here are the functional code changes I made, after this block I've listed all the other files that were only changed to either
Wrap the logging of an array with LogOutputupdate the log call w/ the argument arrangement (as described above), or update the old short version of the log function calls (err/crit) to the current standard long versions (error/critical).classes/CCR/CCRDBHandler:messagecolumn to beformattedas opposed tomessage. This is so that the we can be sure that we're writing the json_encode'd message that was populated by the new processor.classes/CCR/Log:getDbHandlerfunction.LineFormatterwith a format of%formatted%to the handler so that the value that will be associated the$record['formatted'].$value = isset($conf['value']) ? $conf['value] : $defaultValueto the much more compact$value = $conf['value'] ?? $defaultValue.classes/CCR/CCRLineFormatter:After going through the Monolog LineFormatter architecture it turns out that we really only need to overwrite one function instead of two. I chose to make these changes in part because in the updated version of Monolog they've moved to using a classLogRecordinstead of just usingarrays, in addition they've made it so that you can only update particular properties of said record. This is why theCCR\Loggerclass adds the raw$messageto theextrasection as it's only one of a couple properties we can update.%context%variable that we can replace w/ the array provided as the$contextargument.toJsonthat was already present / used to format arrays. By overriding this function I was able to achieve the log output we expect for console logging and it ends up being less code.classes/CCR/LogOutput:This is the only completely new class and it's there to serve as a wrapper around array's of data passed to loggers. In addition to storing the original array for later use by our Processor, it also implements \Stringable so if you cast the object to a string you'll end up with what we'd expect for console or file logging.tests/integration/lib/Logging/CCRDBHandlerTest:CCRDBHandleras it's already present when we get a Logger w/ a DB Handler.$logger->debugcall.Important changes but does not affect the way the code ultimately functions:
tests/playwright/Docker/docker-compose.yml:Tests performed
All Automated Tests
Checklist: