Rework AI logging to use the default Drupal Logger with passing metadata in context
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3534876. -->
Reported by: [murz](https://www.drupal.org/user/157092)
Related to !786
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>The current AI Logging submodule implementation invented a custom logging solution, with a custom log entity type that uses separate fields to store data, and a Views UI to display it. </p>
<p>Keeping the custom log implementation is not good for several reasons:</p>
<ul>
<li>Performance: Storing logs in SQL is good for quick start and local testing, but not good for performance.</li>
<li>Support: If we keep the current solution, we have to support and manage a lot of logging and displaying logs related code.</li>
<li>Custom logging: A lot of sites use custom logging to not overload the database with log records and not decrease the logging performance. So they use logging to syslog, files, stdout, custom filtering, aggregating, etc. And covering all these in the scope of the "ai_logging" module will be quite a challenge.</li>
</ul>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>I believe that we can just use the standard PSR Logger interface to pass all the metadata to the log entry, and handle storing the metadata on the PSR Logger implementation side, using just the current default Drupal Logger service.</p>
<p>So, instead of the "AI Logging" module, which stores log items as a full Drupal Entity, we will split the task into two parts:</p>
<p>- "AI Observability" module, which will subscribe to AI events and generate Drupal Logger entries, passing all the required information and metadata in the log context array. Later, this module will be enabled to support creating OpenTelemetry traces, spans, and metrics; because of this, the module name is more general than just "AI Logs".</p>
<p>- Something like "AI Usage Reports" or "AI Statistics", "AI Usage Summary", etc - a separate module that will rely on the database storage of AI logs and visualise the usage statistics in the Drupal admin panel in a user-friendly way.</p>
<p>For now, if a website uses the default Drupal Logger and enables the "AI Observability" module, it will require no additional dependencies, and users will see all AI events in the logs represented as a regular message with the main values of the event (operation type, provider, model, etc), together with other logs. </p>
<p>But for users who want to have more detailed logs from AI, we will recommend installing any module that supports storing structured logs, which store not only the message string but also all the metadata attached to the log entry.</p>
<p>A good candidate for storing structured logs together with the PSR log entry is the <a href="https://www.drupal.org/project/extended_logger/">Extended Logger</a>, but any other solution should work well. So the "Extended Logger" module will be only a recommendation, not a hard dependency, and the module will work well with the "Database Logging" and "Syslog" modules from Drupal Core too. </p>
<p>Implementing the visualization of the detailed AI logs is out of the scope for this issue and will be handled in a separate issue.</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ul>
<li>Finalize the "AI Observability" module.</li>
</ul>
> Related issue: [Issue #3533109](https://www.drupal.org/node/3533109)
issue