spatie / laravel-activitylog

Log activity inside your Laravel app
https://docs.spatie.be/laravel-activitylog
MIT License
5.34k stars 714 forks source link

Log everything #372

Closed leopinzon closed 6 years ago

leopinzon commented 6 years ago

Hi guys, in advanced thanks for the effort, really.

I need to log everything that my users do in the app... How can I achieve that without writing model per model the fields to be listened?

thanks a lot

Gummibeer commented 6 years ago

https://docs.spatie.be/laravel-activitylog/v2/advanced-usage/logging-model-events

Just use $logFillables.

leopinzon commented 6 years ago

@Gummibeer Thanks a lot for the response Gummibeer. That is exactly my question; following the documentation and your link, we all have to write each column to listen at for changes. Isn't there an option to listen just ALL the columns in all the models?

Gummibeer commented 6 years ago

@leopinzon no, after the first code-block it says:

If you want to log changes to all the $fillable attributes of the model, you can specify protected static $logFillable = true; on the model. Let's see what gets logged when creating an instance of that model.

With this it will log changes on every fillable attribute. If you want to log every-everything you can also use the wildcard * - just to say it: this will also log potential private informations (passwords, tokens and other sensitive data).

The attributes that need to be logged can be defined either by their name or you can put in a wildcard '*' to log any attribute that has changed.

Beware that $ignoreChangedAttributes https://docs.spatie.be/laravel-activitylog/v2/advanced-usage/logging-model-events#ignoring-changes-to-certain-attributes doesn't blacklist the attribute - it just don't triggers a log event if only these attributes are changed. #347 So if you have to exclude some attributes you can add your own trait that overrides partial logic or have to list all whitelisted ones.

leopinzon commented 6 years ago

@Gummibeer You're a saint! I tried these options without much luck. Maybe (and I apologize for not having mentioned before) it is because I'm using the 1.* version of the package since my Laravel version is 5.3. Anyway, writing each important column to listen at in the $logAttributes variable is working like a swiss clock. More writing at the beginning but awesome reward in the long run. Thanks gummi!

Gummibeer commented 6 years ago

That's true - the mentioned variables aren't available in v1 - related to: https://docs.spatie.be/laravel-activitylog/v1/advanced-usage/logging-model-events

I recommend you to upgrade to latest laravel version - you will stay in software-version of now for ever if you don't and it's pretty hard to upgrade 2 or more versions at the same time but isn't that much effort to upgrade a single one in the moment they get released: 5.3 => 5.4: https://laravel.com/docs/5.4/upgrade 5.4 => 5.5: https://laravel.com/docs/5.5/upgrade (now you are able to use this package in v2) 5.5 => 5.6: https://laravel.com/docs/5.6/upgrade (this I also have to do in my projects, a bigger one but it's worth)

To reduce the copy'n'paste amount but put all fillable columns into the protected static $logAttributes is to create your own trait that uses the packe LogsActivity one and use the trait boot method https://tighten.co/blog/laravel-tip-bootable-model-traits/ and copy the fillable array onto the logAttributes variable. With this you have a simple trait that does all the work for you - in this one you also can use array_diff to remove the attributes that are fillable but listet in the hidden property. I'm using this way and got away from all the flags & arrays this package introduces and use my own variables to simply set the logAttributes by combining my attribute lists in the way I want/need.

leopinzon commented 6 years ago

You couldn't be more right, the only problem in my case is that the system is built on a LaraAdmin system that supports - at the most- Laravel 5.3. Not happy with that but trying to make the best of it.

JoseDguez commented 6 years ago

Hi @leopinzon, this is what I did (laravel 5.6), I hope it helps you on 5.3...

I created a Class named BaseModel which extends from Eloquent's Model and added the attributes for log there:

protected static $logAttributes = ['*']; protected static $ignoreChangedAttributes = ['updated_by','updated_at','last_action']; protected static $logOnlyDirty = true;

The simply extend this Class in all of your models instead of Eloquent's Model

Gummibeer commented 6 years ago

@JoseDguez the wildcard isn't available, following the docs, in v1. But the idea with the BaseModel is good - I've switched to traits cause they are also bootable and more modular. If I need one model without a single feature but with all other I can simply don't use the particular trait. With the BaseModel I had to copy all needed features or add feature switch properties.