Rework AI Logging to use the standard PSR Logger interface with passing metadata in context

Created on 9 July 2025, about 1 month ago

Problem/Motivation

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.

Keeping the custom log implementation is not good for several reasons:

  • Performance: Storing logs in SQL is good for quick start and local testing, but not good for performance.
  • Support: If we keep the current solution, we have to support and manage a lot of logging and displaying logs related code.
  • 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.

Proposed resolution

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.

So, from the "ai_logging" module site (or maybe even do not create a separate module for this?), we just create a default Drupal log entry, which displays the most useful log data in the "message" text, that can be viewed and managed via any standard way in Drupal. And just pass all the additional metadata as context, which can be processed and stored in different ways.

A good candidate for storing custom data together with the PSR log entry is the Extended Logger module that allows storing any metadata together with the log record. But this module is not required, so we can not introduce a hard dependency on it, but just recommend it as an option, if someone wants to get more detailed logs. And for most cases, the default Drupal Watchdog should be enough.

Remaining tasks

  • Decide on the solution and finalize it.
Feature request
Status

Active

Version

1.1

Component

AI Logging

Created by

🇦🇲Armenia murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @murz
  • 🇦🇲Armenia murz Yerevan, Armenia
  • 🇦🇲Armenia murz Yerevan, Armenia

    I extended the log displaying styles to allow display of AI Logging logs exactly like in the current module, here are the screenshots to compare:
    AI Logging Logs page:

    Extended Logger logs page:

    This, for now, requires a patch from #3534928: [DB] Allow customizing the output of JSON fields on the logs page but looks promising. So, we can just submit logs to the Drupal Logger, then clone and customize a Views page, provided by Extended Logger, to display the metadata as fields in nthe eeded format.

  • 🇦🇲Armenia murz Yerevan, Armenia

    The logs are rendered from the same source and the output is controlled by the Views field settings with no code changes, so here are two examples of how we can customize and beautify them:
    - Just beautify the response object:

    - Extract the response data into two separate fields (Response, Token usage):

  • 🇦🇲Armenia murz Yerevan, Armenia

    Decided to create a new module "ai_logger" that will log AI events to the standard PSR logger, and in another task, will introduce a separate module that will render the database-stored logs in the admin panel using Extended Logger and Views.

  • Pipeline finished with Failed
    21 days ago
    #554114
  • Pipeline finished with Failed
    21 days ago
    Total: 210s
    #554145
  • Pipeline finished with Canceled
    21 days ago
    Total: 88s
    #554166
  • Pipeline finished with Failed
    21 days ago
    Total: 195s
    #554167
  • Pipeline finished with Canceled
    21 days ago
    Total: 87s
    #554182
  • Pipeline finished with Failed
    21 days ago
    Total: 302s
    #554183
  • Pipeline finished with Running
    20 days ago
    #554721
  • 🇦🇲Armenia murz Yerevan, Armenia

    Updated the implementation of the "AI Observability" module, which is now more or less ready for local testing and debugging, but the code and implementation are not clean enough for review.

    Also, updated the issue description to match the current plan.

    To simplify working with AI events, I also introduced a base class AiProviderRequestBaseEvent, which handles base event properties and getters-setters.

  • Pipeline finished with Failed
    20 days ago
    Total: 244s
    #555090
  • Pipeline finished with Failed
    20 days ago
    Total: 224s
    #555096
  • Pipeline finished with Canceled
    18 days ago
    #556959
  • Pipeline finished with Failed
    18 days ago
    #556960
  • Pipeline finished with Running
    18 days ago
    #556999
  • First commit to issue fork.
  • Pipeline finished with Failed
    6 days ago
    #566382
  • Pipeline finished with Failed
    6 days ago
    #566392
  • Pipeline finished with Failed
    1 day ago
    #569689
  • Pipeline finished with Failed
    1 day ago
    #569792
  • Pipeline finished with Success
    1 day ago
    #569797
  • Pipeline finished with Success
    1 day ago
    #569802
  • 🇦🇲Armenia murz Yerevan, Armenia

    Finally stabilized the feature! It should work well now, so please review it and make manual tests to ensure that everything works well.
    Test steps:

    1. Using the default Drupal DBLog logger:
    1.1. Make several different requests to AI providers.
    1.2. See that the logs appeared on the "Recent log messages" page:

    2. Using the Extended Logger:
    2.1. Install the Extended Logger module version 1.3.0-beta2
    2.2. Make several different requests to AI providers.
    2.3. See that the logs appeared in a suitable way in both modules: Drupal DBLog logger and Extended Logger.
    2.4. On the settings page /admin/config/ai/observability - uncheck the checkbox "Fallback mode for the log message placeholders"
    2.5. See that in the Drupal DBLog logger - placeholders appeared in the log message, but on the Extended Logger - placeholders were replaced with the values correctly.
    2.6. Analyze the detailed data, stored in the log entry of the Extended Logger - it should store everything that we need.

  • 🇦🇲Armenia murz Yerevan, Armenia
  • Pipeline finished with Canceled
    1 day ago
    Total: 96s
    #570046
  • Pipeline finished with Success
    1 day ago
    Total: 337s
    #570047
Production build 0.71.5 2024