adachsoft / event-journal
Record, deduplicate, acknowledge, and prune operational events with a PSR-3 logger adapter.
Requires
- php: ^8.3
- adachsoft/event-journal-contracts: ^0.1
- psr/clock: ^1.0
- psr/log: ^3.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Event Journal is a PHP library for recording operational events in a repository-backed journal. It helps applications retain an auditable event history without coupling their business logic to a specific persistence implementation.
Features
- Record and acknowledge journal entries through a small service API.
- Deduplicate repeated unacknowledged events using a configurable time window.
- Notify listeners when entries are created or updated.
- Adapt PSR-3 log messages into journal events and optionally forward them to another logger.
- Prune entries older than a configured retention period.
- Inject clock and identifier-generator implementations for deterministic behavior and application-specific needs.
Requirements
- PHP 8.3 or newer
- Composer
- An implementation of
JournalEntryRepositoryInterface
The Composer package installs the event-journal contracts and PSR interfaces as dependencies.
Installation
composer require adachsoft/event-journal
Basic usage
Create the journal with a repository implementation, then record an event:
use AdachSoft\EventJournal\EventJournalFactory;
use AdachSoft\EventJournalContracts\Entry\JournalEventDto;
use AdachSoft\EventJournalContracts\Entry\JournalLevelEnum;
use AdachSoft\EventJournalContracts\Repository\JournalEntryRepositoryInterface;
/** @var JournalEntryRepositoryInterface $repository */
$journal = EventJournalFactory::create($repository);
$entry = $journal->record(new JournalEventDto(
level: JournalLevelEnum::WARNING,
source: 'worker',
code: 'device.offline',
message: 'Device went offline.',
subject: $deviceReference,
context: ['attempt' => 3],
occurredAt: new DateTimeImmutable(),
));
The factory supplies a system clock, a random entry identifier generator, and a default deduplication window. You can provide custom implementations or settings when constructing the journal. See the usage example for logger and retention integration.
Deduplication
Events are matched by source, event code, subject type, and subject identifier. A matching unacknowledged entry is updated when the new event occurs within the configured window; the boundary is inclusive. The occurrence count increases, the latest message and context are stored, the occurrence time remains monotonic, and the more severe level is retained. Acknowledged entries are not deduplicated. Set the window to 0 to disable deduplication.
PSR-3 logger
EventJournalFactory::createLogger() creates a PSR-3 logger that can forward calls to an existing logger and record messages at or above a configured minimum level (warning by default). debug and info map to journal info; notice, warning, and error retain their corresponding levels; and critical, alert, and emergency map to journal critical.
The logger interpolates {placeholder} values from the context. Optional journal_code and journal_subject context entries control the event code and subject and are excluded from the stored context. Throwable values are stored as class, message, file, and line; other objects are stored by class name; resources are omitted.
Retention
Use JournalPruner with the same clock as the journal when deterministic retention behavior is required. It asks the repository to delete entries whose last occurrence is strictly older than the calculated cutoff.