Convert Figma logo to code with AI

spatie logolaravel-activitylog

Log activity inside your Laravel app

5,288
713
5,288
13

Top Related Projects

Record the change log from models in Laravel

:dromedary_camel: Laravel log viewer

:page_with_curl: Provides a log viewer for Laravel

Quick Overview

Laravel Activity Log is a package that provides an easy way to log activity in Laravel applications. It allows you to record user actions, model changes, and custom events, offering a flexible and powerful solution for tracking and auditing application activities.

Pros

  • Easy integration with Laravel applications
  • Highly customizable and configurable
  • Supports logging for multiple models and users
  • Provides clean API for querying and retrieving logged activities

Cons

  • Can potentially impact performance if used extensively
  • Requires additional database storage for activity logs
  • May require periodic maintenance to manage log size
  • Limited built-in visualization tools for log analysis

Code Examples

  1. Logging model changes:
use Spatie\Activitylog\Traits\LogsActivity;

class Article extends Model
{
    use LogsActivity;

    protected static $logAttributes = ['title', 'content'];
}
  1. Logging custom activity:
activity()
   ->performedOn($article)
   ->causedBy($user)
   ->withProperties(['custom' => 'data'])
   ->log('Article was viewed');
  1. Retrieving activity logs:
use Spatie\Activitylog\Models\Activity;

$lastLoggedActivities = Activity::latest()->take(10)->get();

Getting Started

  1. Install the package via Composer:
composer require spatie/laravel-activitylog
  1. Publish the configuration file:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"
  1. Run migrations to create the activity_log table:
php artisan migrate
  1. Add the LogsActivity trait to models you want to log:
use Spatie\Activitylog\Traits\LogsActivity;

class YourModel extends Model
{
    use LogsActivity;

    protected static $logAttributes = ['attribute1', 'attribute2'];
}

Competitor Comparisons

Record the change log from models in Laravel

Pros of laravel-auditing

  • More flexible configuration options for customizing audit behavior
  • Built-in support for console commands to manage audits
  • Easier integration with custom audit drivers and storage solutions

Cons of laravel-auditing

  • Slightly more complex setup process compared to laravel-activitylog
  • Less extensive documentation and community resources
  • Fewer out-of-the-box features for advanced logging scenarios

Code Comparison

laravel-auditing:

use OwenIt\Auditing\Contracts\Auditable;

class User extends Model implements Auditable
{
    use \OwenIt\Auditing\Auditable;
}

laravel-activitylog:

use Spatie\Activitylog\Traits\LogsActivity;

class User extends Model
{
    use LogsActivity;

    protected static $logAttributes = ['name', 'email'];
}

Both packages offer similar core functionality for logging model changes in Laravel applications. laravel-auditing provides more granular control over auditing behavior and supports custom drivers, while laravel-activitylog offers a simpler setup process and more extensive documentation. The choice between the two depends on specific project requirements and the desired level of customization.

:dromedary_camel: Laravel log viewer

Pros of laravel-log-viewer

  • Simple and lightweight solution for viewing Laravel logs
  • Easy to set up and use with minimal configuration
  • Provides a web-based interface for browsing log files

Cons of laravel-log-viewer

  • Limited to viewing standard Laravel logs
  • Lacks advanced features like custom activity logging and model tracking
  • Does not offer built-in filtering or search capabilities

Code Comparison

laravel-log-viewer:

Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');

laravel-activitylog:

activity()
   ->performedOn($model)
   ->causedBy($user)
   ->withProperties(['custom' => 'data'])
   ->log('Look, I logged something');

laravel-log-viewer focuses on displaying existing Laravel logs through a simple web interface, while laravel-activitylog provides a more comprehensive solution for custom activity logging and model tracking. The code examples demonstrate the simplicity of setting up laravel-log-viewer with a single route, compared to the more detailed logging capabilities of laravel-activitylog.

laravel-activitylog offers greater flexibility and control over what is logged and how, making it suitable for applications requiring detailed activity tracking. On the other hand, laravel-log-viewer is a straightforward tool for quickly viewing standard Laravel logs without additional overhead.

:page_with_curl: Provides a log viewer for Laravel

Pros of LogViewer

  • User-friendly web interface for viewing and managing logs
  • Supports multiple log files and log levels
  • Includes features like log deletion and download

Cons of LogViewer

  • Focused solely on log viewing, lacks activity tracking capabilities
  • May require additional setup for custom log formats

Code Comparison

LogViewer:

use Arcanedev\LogViewer\Facades\LogViewer;

$logs = LogViewer::all();
$entries = LogViewer::entries($file, $date, $level);

laravel-activitylog:

use Spatie\Activitylog\Models\Activity;

activity()->log('Look mum, I logged something');
$lastLoggedActivity = Activity::all()->last();

Summary

LogViewer is primarily designed for viewing and managing log files through a web interface, while laravel-activitylog focuses on tracking and logging user activities within a Laravel application. LogViewer excels in presenting log data in a user-friendly manner, supporting multiple log files and levels. However, it lacks the ability to track custom activities that laravel-activitylog provides.

laravel-activitylog offers more flexibility in logging custom events and associating them with models, users, or specific actions. It integrates seamlessly with Laravel's ORM and provides an easy way to query and retrieve logged activities.

The choice between these packages depends on the specific needs of your project. If you require a comprehensive log viewer with a web interface, LogViewer is an excellent choice. For tracking user activities and creating custom logs within your application, laravel-activitylog is more suitable.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Social Card of Laravel Activity Log

Log activity inside your Laravel app

Latest Version on Packagist GitHub Workflow Status Check & fix styling Total Downloads

The spatie/laravel-activitylog package provides easy to use functions to log the activities of the users of your app. It can also automatically log model events. The Package stores all activity in the activity_log table.

Here's a demo of how you can use it:

activity()->log('Look, I logged something');

You can retrieve all activity using the Spatie\Activitylog\Models\Activity model.

Activity::all();

Here's a more advanced example:

activity()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->log('Look, I logged something');

$lastLoggedActivity = Activity::all()->last();

$lastLoggedActivity->subject; //returns an instance of an eloquent model
$lastLoggedActivity->causer; //returns an instance of your user model
$lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue'
$lastLoggedActivity->description; //returns 'Look, I logged something'

Here's an example on event logging.

$newsItem->name = 'updated name';
$newsItem->save();

//updating the newsItem will cause the logging of an activity
$activity = Activity::all()->last();

$activity->description; //returns 'updated'
$activity->subject; //returns the instance of NewsItem that was saved

Calling $activity->changes() will return this array:

[
   'attributes' => [
        'name' => 'updated name',
        'text' => 'Lorum',
    ],
    'old' => [
        'name' => 'original name',
        'text' => 'Lorum',
    ],
];

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

You'll find the documentation on https://spatie.be/docs/laravel-activitylog/introduction.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the activity log? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

Installation

You can install the package via composer:

composer require spatie/laravel-activitylog

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"

Note: The default migration assumes you are using integers for your model IDs. If you are using UUIDs, or some other format, adjust the format of the subject_id and causer_id fields in the published migration before continuing.

After publishing the migration you can create the activity_log table by running the migrations:

php artisan migrate

You can optionally publish the config file with:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"

Changelog

Please see CHANGELOG for more information about recent changes.

Upgrading

Please see UPGRADING for details.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Credits

And a special thanks to Caneco for the logo and Ahmed Nagi for all the work he put in v4.

License

The MIT License (MIT). Please see License File for more information.