Skip to content

Commit baa07ff

Browse files
committed
Package renaming
1 parent 6d606a3 commit baa07ff

20 files changed

Lines changed: 345 additions & 194 deletions

README.md

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,45 @@ Logger is a lightweight, opinionated activity logging package for Laravel that a
88
You can install the package via composer:
99

1010
```bash
11-
composer require gott/logger
11+
composer require gottvergessen/activity
1212
```
1313

1414
You can publish and run the migrations with:
1515

1616
```bash
17-
php artisan logger:install
17+
php artisan activity:install
1818
```
1919

20-
21-
### config/logger.php
20+
### config/activity.php
2221

2322
```php
24-
return [
25-
'enabled' => env('LOGGER_ENABLED', true),
26-
27-
'table' => 'activity_logs',
28-
29-
'default_log' => env('LOGGER_DEFAULT_LOG', 'default'),
30-
31-
'origin' => env('LOGGER_ORIGIN', 'web'),
32-
33-
'events' => [
34-
'created',
35-
'updated',
36-
'deleted',
37-
'restored',
38-
],
39-
40-
'ignore_attributes' => [
41-
'created_at',
42-
'updated_at',
43-
'deleted_at',
44-
'remember_token',
45-
],
23+
return [
24+
'enabled' => true,
4625

47-
'use_diffs' => true,
26+
'table' => 'activity_logs',
4827

49-
'driver' => 'database',
50-
];
28+
'default_log' => 'default',
5129

30+
'events' => [
31+
'created',
32+
'updated',
33+
'deleted',
34+
'restored',
35+
],
36+
37+
'ignore_attributes' => [
38+
'created_at',
39+
'updated_at',
40+
'deleted_at',
41+
'remember_token',
42+
],
43+
];
5244
```
5345

5446
## Basic usage
5547

5648
```php
57-
use Gottvergessen\Logger\Traits\TracksModelActivity;
49+
use Gottvergessen\Activity\Traits\TracksModelActivity;
5850

5951
class User extends Authenticatable
6052
{
@@ -64,8 +56,9 @@ class User extends Authenticatable
6456
```
6557

6658
## Ignore Specific Fields per Model
59+
6760
```php
68-
use Gottvergessen\Logger\Traits\TracksModelActivity;
61+
use Gottvergessen\Activity\Traits\TracksModelActivity;
6962

7063
class User extends Authenticatable
7164
{
@@ -77,14 +70,61 @@ class User extends Authenticatable
7770
];
7871

7972
/*
80-
* Changes to the listed attributes will not appear in the activity properties
81-
* If only ignored attributes change, no activity log entry is created
73+
* Changes to the listed attributes will not appear in the activity properties
74+
* If only ignored attributes change, no activity log entry is created
8275
*/
8376
}
8477
```
78+
79+
## Database Schema
80+
81+
The `activity_logs` table tracks the following information:
82+
83+
| Column | Type | Description |
84+
|--------|------|-------------|
85+
| id | ID | Primary key |
86+
| event | string | The event type (created, updated, deleted, restored) |
87+
| action | string | Custom semantic action defined by the model (optional) |
88+
| log | string | Log category for grouping activities |
89+
| description | string | Description of the activity (optional) |
90+
| subject_type | string | The model class that was changed |
91+
| subject_id | int | The ID of the model that was changed |
92+
| causer_type | string | The model class of the user who made the change (optional) |
93+
| causer_id | int | The ID of the user who made the change (optional) |
94+
| properties | json | The model attributes that changed |
95+
| meta | json | Metadata including IP, user agent, HTTP method, and host |
96+
| batch_id | string | Batch ID for grouping related changes (optional) |
97+
| created_at | timestamp | When the activity was recorded |
98+
| updated_at | timestamp | When the activity record was last updated |
99+
100+
## Eloquent Friendly
101+
102+
```php
103+
use Gottvergessen\Activity\Traits\TracksModelActivity;
104+
use Gottvergessen\Activity\Traits\InteractsWithActivity;
105+
106+
class User extends Authenticatable
107+
{
108+
use TracksModelActivity, InteractsWithActivity;
109+
}
110+
```
111+
112+
With the `InteractsWithActivity` trait, you can easily query a model's activities:
113+
114+
```php
115+
$users = User::with('activities')->get();
116+
117+
// Access activities for a specific model
118+
$userActivities = $user->activities()->get();
119+
$userActivities = $user->activities()->where('event', 'updated')->get();
120+
```
121+
122+
The `InteractsWithActivity` trait is optional and only required if you want to access `$model->activities()`.
123+
85124
## Per-Model Event Control
125+
86126
```php
87-
use Gottvergessen\Logger\Traits\TracksModelActivity;
127+
use Gottvergessen\Activity\Traits\TracksModelActivity;
88128

89129
class User extends Authenticatable
90130
{
@@ -96,29 +136,63 @@ class User extends Authenticatable
96136
];
97137

98138
/*
99-
* Limit which events are tracked:
100-
*
139+
* Limit which events are tracked for this model
101140
*/
102141
}
103142
```
104-
### Custom description per Model
143+
## Advanced Usage
144+
145+
### Custom Log Name per Model
146+
105147
```php
148+
use Gottvergessen\Activity\Traits\TracksModelActivity;
149+
150+
class Invoice extends Model
151+
{
152+
use TracksModelActivity;
153+
154+
public function activityLog(): string
155+
{
156+
return 'invoices';
157+
}
158+
}
159+
```
160+
161+
### Custom Action per Model
162+
163+
```php
164+
use Gottvergessen\Activity\Traits\TracksModelActivity;
165+
106166
class Appointment extends Model
107167
{
108168
use TracksModelActivity;
109169

110-
public function activityDescription(string $event): string
170+
public function activityAction(string $event): string
111171
{
112172
return match ($event) {
113-
'created' => "Appointment scheduled for {$this->scheduled_at}",
114-
'updated' => "Appointment updated for {$this->scheduled_at}",
115-
'deleted' => "Appointment cancelled",
116-
default => "Appointment {$event}",
173+
'created' => "Appointment was scheduled for {$this->scheduled_at}",
174+
'updated' => "Appointment has been updated to {$this->new_appointment_date}",
175+
default => $event,
117176
};
118177
}
119178
}
120179
```
121180

181+
### Batch Operations
182+
183+
Feature not yet Implement but it will be:
184+
Group multiple model changes under a single batch ID:
185+
186+
```php
187+
use Gottvergessen\Activity\Activity;
188+
189+
Activity::batch(function () {
190+
$user->update(['name' => 'John']);
191+
$user->profile()->update(['bio' => 'Updated bio']);
192+
// Both changes share the same batch_id
193+
});
194+
```
195+
122196

123197

124198
## Contributing

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "gott/logger",
2+
"name": "gottvergessen/activity",
33
"description": "This is my package logger",
44
"keywords": [
55
"Gottvergessen",
@@ -29,13 +29,13 @@
2929
},
3030
"autoload": {
3131
"psr-4": {
32-
"Gottvergessen\\Logger\\": "src/",
33-
"Gottvergessen\\Logger\\Database\\Factories\\": "database/factories/"
32+
"Gottvergessen\\Activity\\": "src/",
33+
"Gottvergessen\\Activity\\Database\\Factories\\": "database/factories/"
3434
}
3535
},
3636
"autoload-dev": {
3737
"psr-4": {
38-
"Gottvergessen\\Logger\\Tests\\": "tests/",
38+
"Gottvergessen\\Activity\\Tests\\": "tests/",
3939
"Workbench\\App\\": "workbench/app/"
4040
}
4141
},
@@ -57,10 +57,10 @@
5757
"extra": {
5858
"laravel": {
5959
"providers": [
60-
"Gottvergessen\\Logger\\LoggerServiceProvider"
60+
"Gottvergessen\\Activity\\ActivityServiceProvider"
6161
],
6262
"aliases": {
63-
"Logger": "Gottvergessen\\Logger\\Facades\\Logger"
63+
"Activity": "Gottvergessen\\Activity\\Facades\\Activity"
6464
}
6565
}
6666
},
File renamed without changes.

database/migrations/create_logger_table.php.stub

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,18 @@ return new class extends Migration
1010
{
1111
Schema::create('activity_logs', function (Blueprint $table) {
1212
$table->id();
13-
14-
/**
15-
* WHAT happened
16-
*/
17-
$table->string('event', 32); // created | updated | deleted | restored
18-
$table->string('action', 64)->nullable(); // optional semantic action (approved, assigned, paid)
19-
$table->string('log', 64)->default('default'); // grouping / channel
20-
$table->text('description')->nullable(); // human-readable sentence
21-
22-
/**
23-
* SUBJECT (model being acted on)
24-
*/
25-
$table->string('subject_type'); // App\Models\Order
26-
$table->unsignedBigInteger('subject_id'); // 123
27-
28-
/**
29-
* ACTOR (who caused it)
30-
*/
31-
$table->string('causer_type')->nullable(); // App\Models\User | system | job
13+
$table->string('event', 32);
14+
$table->string('action', 64)->nullable();
15+
$table->string('log', 64)->default('default');
16+
$table->text('description')->nullable();
17+
$table->string('subject_type');
18+
$table->unsignedBigInteger('subject_id');
19+
$table->string('causer_type')->nullable();
3220
$table->unsignedBigInteger('causer_id')->nullable();
33-
34-
/**
35-
* CONTEXT / METADATA
36-
*/
37-
$table->json('properties')->nullable(); // before/after values
38-
$table->json('meta')->nullable(); // ip, ua, route, request id
39-
40-
/**
41-
* SYSTEM
42-
*/
43-
$table->string('origin', 32)->nullable(); // web | api | console | queue
44-
$table->string('batch_id', 36)->nullable(); // UUID for grouped actions
45-
21+
$table->json('properties')->nullable();
22+
$table->json('meta')->nullable();
23+
$table->string('batch_id', 36)->nullable();
4624
$table->timestamps();
47-
4825
$table->index(['subject_type', 'subject_id']);
4926
$table->index(['causer_type', 'causer_id']);
5027
$table->index(['event', 'created_at']);
@@ -53,6 +30,6 @@ return new class extends Migration
5330
}
5431

5532
public function down(){
56-
Schema::dropIfExists(config('logger.table'));
33+
Schema::dropIfExists('activity_logs');
5734
}
5835
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

3-
namespace Gottvergessen\Logger;
3+
namespace Gottvergessen\Activity;
44

55
use Illuminate\Support\Str;
66

7-
class Logger {
7+
class Activity {
88
public static function batch(callable $callback): mixed
99
{
10-
app()->instance('logger.batch', (string) Str::uuid());
10+
app()->instance('activity.batch', (string) Str::uuid());
1111

1212
try {
1313
return $callback();
1414
} finally {
15-
app()->forgetInstance('logger.batch');
15+
app()->forgetInstance('activity.batch');
1616
}
1717
}
1818
}

src/ActivityServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Gottvergessen\Activity;
4+
5+
6+
use Spatie\LaravelPackageTools\Package;
7+
use Spatie\LaravelPackageTools\PackageServiceProvider;
8+
use Spatie\LaravelPackageTools\Commands\InstallCommand;
9+
use Gottvergessen\Activity\Commands\ActivityInstallCommand;
10+
11+
class ActivityServiceProvider extends PackageServiceProvider
12+
{
13+
public function configurePackage(Package $package): void
14+
{
15+
$package
16+
->name('activity')
17+
->hasConfigFile()
18+
->hasMigration('create_logger_table')
19+
->hasCommand(ActivityInstallCommand::class);
20+
}
21+
22+
23+
}

0 commit comments

Comments
 (0)