surgery3/db-event-log

Log events to a flexible database table within the application

5.0.1 2025-06-04 20:23 UTC

This package is not auto-updated.

Last update: 2025-06-05 18:31:46 UTC


README

Helpers and classes to store an event log in your database for auditing.

To add the entity to doctrine's entity manager in config/packages/doctrine.yaml:

doctrine:
    dbal:
        # ...
    orm:
        # ...
        mappings:
            App:
                # ...
            DbEventLog:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/vendor/surgery3/db-event-log/src/Entity'
                prefix: 'Surgery3\DbEventLog\Entity'
                alias: DbEventLog

Event Subscriber Setup

To add the event subscriber in config/services.yml:

services:
    # ...

    Surgery3\DbEventLog\EventSubscriber\EventLogSubscriber:
        tags:
            - { name: doctrine.event_subscriber, connection: default }

The IDbEventLogUserInfo works similarly to the RequestUserInfoInterface from surgery3/request-log. You can implement them both with the same service.

<?php

class AppUserInfo extends TokenStorageUserInfo implements RequestUserInfoInterface, IDbEventLogUserInfo
{
    public function __construct(LoggerInterface $logger, TokenStorageInterface $tokenStorage)
    {
        parent::__construct($logger, $tokenStorage, ["app"]);
    }
}

Entities that you want to log must implement ILoggedEntity. For convenience, there is a LoggedEntity class that they can inherit from to reduce some duplication.

Then register these services in services.yaml:

Surgery3\DbEventLog\Service\IDbEventLogUserInfo: '@App\Service\AppUserInfo'
<?php

class FundingRound extends LoggedEntity
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer", unique=true)
     */
    private $id;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEvents()
    {
        return $this->events;
    }
}

Run tests with:

vendor/bin/phpunit --bootstrap vendor/autoload.php tests