PHPackages                             nnt/activity-log - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. nnt/activity-log

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

nnt/activity-log
================

Package này cung cấp các chức năng dễ sử dụng để ghi nhật ký hoạt động của người dùng ứng dụng của bạn.

04PHP

Since Jul 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/nhattruong213/activity-log)[ Packagist](https://packagist.org/packages/nnt/activity-log)[ RSS](/packages/nnt-activity-log/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

### Cài đặt

[](#cài-đặt)

1. Chạy lệnh sau để cài đặt package từ đường dẫn local:

    ```
     composer require nnt/activity-log

    ```
2. Publish cấu hình

    ```
     php artisan vendor:publish --provider="NNT\ActivityLog\ActivityLogServiceProvider" --tag="config"

     php artisan config:cache

    ```
3. Publish migration:

    ```
     php artisan vendor:publish --provider="NNT\ActivityLog\ActivityLogServiceProvider" --tag="migrations"

    ```
4. Chạy *php artisan migrate* để tạo bảng activity\_logs trong DB

### Hướng dẫn dùng

[](#hướng-dẫn-dùng)

#### Manually log

[](#manually-log)

**Để log nhanh thì có thể sử dụng**

```
activity_log()->log('Hello World!');
```

**Một số tính năng nâng cao:**

1. Set log\_type column

    Sử dụng `logType` để đặt giá trị cột log\_type

    ```
    activity_log()
        ->logType($eventName)
        ->log('Hello World!');
    ```
2. Set Subject column

    Sử dụng `performedOn` để đặt giá trị cột subject. Cột này sẽ lưu giá id và model của đối tượng cần log

    ```
    activity_log()
        ->performedOn($anEloquentModel)
        ->log('Hello World!');
    ```
3. Set Subject column

    Sử dụng `causedBy` để đặt giá trị cột causer. Cột này sẽ lưu giá id và model của user đang log.

    ```
    activity_log()
        ->causedBy($userEloquentModel)
        ->log('Hello World!');
    ```

    Mặc định nếu không truyền gì vào thì sẽ lấy user đang logged in.

    Nếu bỏ trống trường này thì sử dụng `causedByAnonymous`

    ```
    activity_log()
        ->causedByAnonymous()
        ->log('Hello World!');
    ```
4. Set before\_value and after\_value column

    Sử dung `beforeValue` và `afterValue` để set giá trị cho 2 cột này, param truyền vào là array, dữ liêu sẽ được json\_encode trước khi lưu vào database

    ```
    activity_log()
        ->beforeValue($beforeValue)
        ->afterValue($afterValue)
        ->log('Hello World!');
    ```
5. Set description column

    Sử dụng `log` để set giá trị cho cột description. Phương thức này bắt buộc phải có để save data

    ```
    activity_log()
        ->log('Hello World!');
    ```
6. Set created\_at column

    Sử dụng `createdAt` để set giá trị cho cột created\_at. Param truyền vào là Carbon Object

    ```
    activity_log()
        ->createdAt($timestamp)
        ->log('Hello World!');
    ```
7. Để disable log cho request hiện tại thì sử dụng

    ```
    activity_log()->disableLogging();
    ```
8. Để enable log cho request hiện tại thì sử dụng

    ```
    activity_log()->enableLogging();
    ```

#### Model Event Log

[](#model-event-log)

Package này có thể sử dụng để autolog model event

Để sử dụng thì trong model thêm trait `LogsActivity`

```
use NNT\ActivityLog\Traits\LogsActivity;

class NewsItem extends Model
{
    use LogsActivity;
}
```

Mặc định là `['created', 'updated', 'deleted']`, nếu có sử dụng trait **SoftDelete** thì có thêm 1 event được hỗ trợ nữa là `['restored']`

Để chỉ định những model event nào cần log thì setting bằng cách thêm `$recordEvents` trong model

```
 protected static $recordEvents = ['deleted'];

```

Nếu muốn chỉ định những attributes nào sẽ không kích hoạt sự kiện log khi nó thay đổi thì sử dụng

```
protected static $ignoreChangedAttributes = ['text'];

```

Mặc định thì `updated_at` sẽ không được ignore, nên sẽ trigger khi update dữ liệu. Nếu muốn bỏ qua điều này thì bỏ nó vào trong `$ignoreChangedAttributes`

Nếu muốn loại bỏ các attributes ra khỏi giá trị log before and after value thì sử dụng

```
protected static $logAttributesToIgnore = ['password', 'updated_at'];

```

Để custom description thì sử dụng method `customActivityDescription()` trong model. Method này sẽ cho phép chỉnh sửa lại description trước khi lưu vào database

Ví dụ:

```
public function customActivityDescription($model, string $eventName) {
    if ($model->wasChanged('password')) {
        return 'Password has changed';
    }
    return __("activity_log::messages.{$eventName}");
}
```

Để set cứng cột related\_type thì thêm `$relatedType` trong model

```
 protected static $relatedType = 'User';

```

Nếu không set cứng giá trị này thì cột related\_type sẽ được thêm vơi định dạng PascalCase và dùng số ít (Job, Customer,...)

#### Config Advanced

[](#config-advanced)

Có thể setting 1 số chức năng ở trong file config. Xem chi tiết ở file activity-log.php

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/284543d23a3ddd5a8b510b673a081445e775d8827fa52729bc51ff116d276310?d=identicon)[truongnn-odinbi](/maintainers/truongnn-odinbi)

---

Top Contributors

[![nhattruong213](https://avatars.githubusercontent.com/u/55304556?v=4)](https://github.com/nhattruong213 "nhattruong213 (4 commits)")

### Embed Badge

![Health badge](/badges/nnt-activity-log/health.svg)

```
[![Health](https://phpackages.com/badges/nnt-activity-log/health.svg)](https://phpackages.com/packages/nnt-activity-log)
```

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M138](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[datadog/php-datadogstatsd

An extremely simple PHP datadogstatsd client

19124.6M15](/packages/datadog-php-datadogstatsd)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
