adachsoft / event-journal-json-file
A lock-protected JSON file repository for event journal entries.
Package info
gitlab.com/a.adach/event-journal-json-file
pkg:composer/adachsoft/event-journal-json-file
Requires
- php: ^8.3
- adachsoft/event-journal-contracts: ^0.1
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
A JSON-file implementation of JournalEntryRepositoryInterface from adachsoft/event-journal-contracts.
Requirements
- PHP 8.3 or newer
adachsoft/event-journal-contracts^0.1
Installation
composer require adachsoft/event-journal-json-file
Usage
Create a repository with the path of the journal file. Its parent directory is created when needed.
use AdachSoft\EventJournalContracts\Entry\EntryId;
use AdachSoft\EventJournalContracts\Entry\JournalEntryDto;
use AdachSoft\EventJournalContracts\Entry\JournalLevelEnum;
use AdachSoft\EventJournalContracts\Entry\SubjectReference;
use AdachSoft\EventJournalContracts\Query\JournalQueryDto;
use AdachSoft\EventJournalJsonFile\JsonJournalEntryRepository;
use DateTimeImmutable;
$repository = new JsonJournalEntryRepository(__DIR__ . '/var/event-journal.json');
$occurredAt = new DateTimeImmutable();
$entry = new JournalEntryDto(
id: new EntryId('mqtt-gateway-disconnected'),
level: JournalLevelEnum::WARNING,
source: 'zigbee.worker',
code: 'mqtt.disconnected',
message: 'The MQTT connection was lost.',
subject: new SubjectReference('device', 'gateway-1'),
context: ['retry' => 2],
firstOccurredAt: $occurredAt,
lastOccurredAt: $occurredAt,
occurrences: 1,
);
$repository->save($entry);
$unacknowledged = $repository->query(new JournalQueryDto(
acknowledged: false,
limit: 25,
));
The repository also supports lookup by entry identifier or fingerprint, filtered queries, counts, removal, and pruning through the contract interface.
File format
The file contains a versioned JSON object. Entries are stored by identifier:
{
"version": 1,
"entries": {
"mqtt-gateway-disconnected": {
"id": "mqtt-gateway-disconnected",
"level": "warning",
"source": "zigbee.worker",
"code": "mqtt.disconnected",
"message": "The MQTT connection was lost.",
"subject": {"type": "device", "id": "gateway-1"},
"context": {"retry": 2},
"firstOccurredAt": "2025-01-01T12:00:00.123456+00:00",
"lastOccurredAt": "2025-01-01T12:00:00.123456+00:00",
"occurrences": 1,
"acknowledgedAt": null,
"acknowledgedBy": null
}
}
}
Dates are serialized with microseconds and their UTC offset using Y-m-d\TH:i:s.uP. Severity values use the enum's string value. subject and acknowledgedBy are either { "type": "…", "id": "…" } objects or null.
Invalid JSON or an invalid journal document raises JsonFileException, which implements EventJournalExceptionInterface; its message identifies the configured file path.
Locking and atomic writes
The repository uses a sibling .lock file and flock: reads acquire a shared lock, while updates acquire an exclusive lock. Updates encode the complete document into a temporary file in the same directory and rename it over the journal file before releasing the lock. This prevents readers from observing a partially written document. The parent directory is created with mode 0775 when it is missing; the process must have permission to create and write the directory and files.
Retention and eviction
The repository retains at most 10,000 entries by default. Configure another limit with the constructor's maxEntries argument; it must be at least one. When saving an entry takes the journal over the limit, acknowledged entries are evicted before unacknowledged entries, with the oldest lastOccurredAt entries removed first within each group. pruneOlderThan() independently removes entries whose lastOccurredAt is strictly earlier than the supplied timestamp.