Problem/Motivation

Event logs are currently purged on cron by record count only, via the max_records setting (Logger::purgeOldLogs() keeps the N most recent records and deletes the rest).

On busy sites the number of logged events per day varies a lot, so a fixed record cap is a poor proxy for retention: it can keep months of data or only a few hours depending on traffic. Administrators usually reason about log retention in terms of time ("keep the last 7 days"), and an unbounded table grows the database with little operational value. There is no way to say "delete anything older than X days", and no schema entry for it.

Proposed resolution

Add an optional max_days setting that purges any event log whose created timestamp is older than max_days days. It works independently of, and alongside, max_records:

- max_records (existing): keep only the most recent N records.
- max_days (new): delete records older than N days.

When both are set, both passes run on cron. Setting either to 0 disables that pass; setting both to 0 disables purging entirely (current default behaviour is preserved).

User interface changes

A new "Days of eventlog messages to keep" field on the Events Logging settings form, below "Eventlog messages to keep". Help text: "The maximum age, in days, of messages to keep in the database event log. Messages older than this will be removed. Set to 0 to disable age-based purging. Requires a cron maintenance task."

API changes

- Drupal\events_logging\Logger::__construct() gains a new last argument \Drupal\Component\Datetime\TimeInterface $time (services.yml updated to pass @datetime.time). This is a constructor signature change; sites that instantiate the service directly rather than via the container should update accordingly.
- New events_logging.config:max_days config key (integer, default 0).

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

paranojik created an issue. See original summary.

paranojik’s picture

Status: Active » Needs review
alecsmrekar’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thank you!