Advanced MongoDB-based user activity logging system Designed for tracking, querying, and archiving user/admin actions in a performant, structured, and easily searchable format.
- β Structured activity tracking β supports CRUD + view actions
- β Enum-based configuration β roles, modules, and types
- β Optimized indexes for performance
- β Advanced filtering (user, role, module, type, keyword, date range)
- β Pagination-ready search results
- β Quarter-based archiving system (auto CRON support)
- β Environment-independent β use via DI container
- β Dual database mode (active + archive)
maatify-mongo-activity/
βββ src/
β βββ Contract/
β β βββ AppModuleInterface.php
β β βββ ActivityTypeInterface.php
β β βββ UserRoleInterface.php
β βββ Enum/
β β βββ AppModulesEnum.php
β β βββ ActivityTypeEnum.php
β β βββ UserRoleEnum.php
β βββ DTO/
β β βββ ActivityRecordDTO.php
β βββ Repository/
β β βββ ActivityRepository.php
β β βββ ArchiveRepository.php
β β βββ PeriodResolverRepository.php
β βββ Manager/
β β βββ ActivityArchiveManager.php
β βββ Helpers/
β βββ ActivityPeriodResolver.php
βββ scripts/
β βββ mongo-activity-ensure-indexes.php
β βββ mongo-activity-archive.php
βββ .env.example
βββ composer.json
composer require maatify/mongo-activityIf the library is private and hosted on GitHub, GitLab, or Bitbucket:
{
"repositories": [
{
"type": "vcs",
"url": "git@github.com:maatify/mongo-activity.git"
}
],
"require": {
"maatify/mongo-activity": "dev-main"
}
}Then install:
composer update maatify/mongo-activity
β οΈ Make sure your user has access to the private repository via SSH or a valid Personal Access Token.
If you are developing both the project and the library locally:
{
"repositories": [
{
"type": "path",
"url": "../maatify/mongo-activity",
"options": { "symlink": true }
}
],
"require": {
"maatify/mongo-activity": "dev-main"
}
}Install with:
composer require maatify/mongo-activity:dev-mainβ Any change you make inside the library will instantly reflect in your project (no reinstall required).
If you prefer HTTPS authentication instead of SSH:
composer config --global github-oauth.github.com ghp_yourAccessTokenHereThen reference your repository as:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/maatify/mongo-activity.git"
}
],
"require": {
"maatify/mongo-activity": "dev-main"
}
}# Mongo Connection
MONGO_URI=mongodb://127.0.0.1:27017
# Databases
MONGO_DB_ACTIVITY=maatify_activity
MONGO_DB_ACTIVITY_ARCHIVE=maatify_activity_archive
# Collections
MONGO_COLLECTION_ACTIVITY=user_activities
# Feature toggle
MONGO_ACTIVITY_ENABLED=trueuse MongoDB\Client;
use Maatify\MongoActivity\Repository\ActivityRepository;
use Maatify\MongoActivity\DTO\ActivityRecordDTO;
use Maatify\MongoActivity\Enum\AppModulesEnum;
use Maatify\MongoActivity\Enum\UserRoleEnum;
use Maatify\MongoActivity\Enum\ActivityTypeEnum;
$client = new Client($_ENV['MONGO_URI']);
$repo = new ActivityRepository($client);
$repo->insert([
'user_id' => 501,
'role' => UserRoleEnum::ADMIN->value,
'type' => ActivityTypeEnum::UPDATE->value,
'module' => AppModulesEnum::PRODUCT->value,
'action' => 'edit_price',
'description' => 'Updated product #312',
'ref_id' => 312,
'created_at' => new MongoDB\BSON\UTCDateTime(),
]);$result = $repo->search(
userId: 501,
module: AppModulesEnum::PRODUCT,
keyword: 'price',
from: '2025-11-01T00:00:00',
to: '2025-11-05T23:59:59',
perPage: 10
);To move logs older than 6 months to quarterly archive collections:
php scripts/mongo-activity-archive.phpIt automatically moves data to collections such as:
user_activities_archive_2025_0103
user_activities_archive_2025_0406
user_activities_archive_2025_0709
user_activities_archive_2025_1012
To (re)create indexes for faster queries:
php scripts/mongo-activity-ensure-indexes.phpThis ensures indexes for:
user_idcreated_atmoduleroletype
use MongoDB\Client;
use Maatify\MongoActivity\Repository\ActivityRepository;
use Maatify\MongoActivity\Enum\AppModulesEnum;
use Maatify\MongoActivity\Enum\UserRoleEnum;
use Maatify\MongoActivity\Enum\ActivityTypeEnum;
$client = new Client($_ENV['MONGO_URI']);
$repo = new ActivityRepository($client);
$repo->insert([
'user_id' => 501,
'role' => UserRoleEnum::ADMIN->value,
'type' => ActivityTypeEnum::UPDATE->value,
'module' => AppModulesEnum::PRODUCT->value,
'action' => 'edit_price',
'description' => 'Updated product #312',
'ref_id' => 312,
'created_at' => new MongoDB\BSON\UTCDateTime(),
]);$result = $repo->search(
userId: 501,
module: AppModulesEnum::PRODUCT,
keyword: 'price',
from: '2025-11-01T00:00:00',
to: '2025-11-05T23:59:59',
perPage: 10
);Move logs older than 6 months to quarterly archive collections:
php scripts/mongo-activity-archive.phpCreates collections such as:
user_activities_archive_2025_0103
user_activities_archive_2025_0406
user_activities_archive_2025_0709
user_activities_archive_2025_1012
To (re)create performance indexes:
php scripts/mongo-activity-ensure-indexes.phpCreates indexes on:
user_idcreated_atmoduleroletype
use MongoDB\Client;
use DI\ContainerBuilder;
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
Client::class => fn() => new Client($_ENV['MONGO_URI']),
]);
$container = $containerBuilder->build();use Maatify\MongoActivity\Repository\ActivityRepository;
$containerBuilder->addDefinitions([
ActivityRepository::class => fn($c) =>
new ActivityRepository(
$c->get(Client::class),
$_ENV['MONGO_DB_ACTIVITY'],
$_ENV['MONGO_COLLECTION_ACTIVITY']
),
]);$app->post('/log', function ($request, $response) use ($container) {
$repo = $container->get(ActivityRepository::class);
$repo->insert([
'user_id' => 123,
'role' => 'admin',
'type' => 'update',
'module' => 'settings',
'action' => 'change_password',
'description' => 'Admin updated user password',
'created_at' => new MongoDB\BSON\UTCDateTime(),
]);
$response->getBody()->write('Activity logged.');
return $response;
});| Script | Purpose | Schedule |
|---|---|---|
scripts/mongo-activity-archive.php |
Archive logs older than 6 months | Every 6 months |
scripts/mongo-activity-ensure-indexes.php |
Verify indexes for all collections | Once after deployment |
| Dependency | Minimum Version | Notes |
|---|---|---|
| PHP | 8.4 | Native enums & readonly props |
mongodb/mongodb |
2+ | Official MongoDB driver |
vlucas/phpdotenv |
5.6+ | For .env loading (optional) |
- PHP β₯ 8.4
mongodb/mongodbβ₯ 2vlucas/phpdotenvβ₯ 5.6
Youβre free to use, modify, and distribute this library with attribution.
Maatify.dev https://www.Maatify.dev