Laravel-activitylog: Including IP Address of the Causer

Created on 5 Aug 2016  路  16Comments  路  Source: spatie/laravel-activitylog

It would be a good feature specially when users are generally logging in from different places. And it would help with tracking as well.

Most helpful comment

put this code in boot method in the AppServiceProvider.php is work for me

Activity::saving(function (Activity $activity) {
    $activity->properties = $activity->properties->put('ip', request()->ip());
});

All 16 comments

You could use a custom property to hold the ip address.

Would it be possible with automatic logging model events anyhow?

Hmm not out of the box.

However, you could use an Eloquent saving event to add it yourself before the record gets saved.

use Spatie\Activitylog\Models\Activity;

Activity::saving(function (Activity $activity) {
    $activity->properties->put('ip', '127.0.0.1');
});

Thank you @sebastiandedeyne. This code will need to be put in every model class for which I want to store IP adress, right? Can there be any way to achieve DRY stage?

It works the other way around, it gets called every time Activity gets logged, not on a per model basis. If you only want to log the IP for certain models, you'd have to add a conditional to the closure.

Quick example (untested):

use Spatie\Activitylog\Models\Activity;

Activity::saving(function (Activity $activity) {

    $addIpTo = collect([
        App\Models\Foo::class,
        App\Models\Bar::class,
    ]);

    if ($addIpTo->contains(get_class($activity->subject))) {
        $activity->properties->put('ip', '127.0.0.1');
    }
});

Hi, I try $activity->properties->put('ip', '127.0.0.1') but not working.

Example:

$lastActivity = Activity::all()->last();
//now $lastActivity->properties ={"attributes":{"name":"name","email":"[email protected]"}}

$lastActivity->properties->put('ip', '127.0.0.1');
//now $lastActivity->properties = {"attributes": "name":"name","email":"[email protected]"}}

The method "put" doesn't update the collection :-(

PD: with laravel-activitylog v1.3.1 because I use laravel 5.1.

Thanks!

where do i place the code that you suggest ?
`use SpatieActivitylog\ModelsActivity;

Activity::saving(function (Activity $activity) {
$activity->properties->put('ip', '127.0.0.1');
});`

Doesn't really matter, as long as it gets executed once. You could create a service provider and place it in the boot method.

I've made a service provider. The code fires but the method "put" doesn't update the collection :-(

There are only model attributes setted with:
protected static $logAttributes = []

Oh, probably will have to do:

$activity->properties = $activity->properties->put('ip', '...');

put this code in boot method in the AppServiceProvider.php is work for me

Activity::saving(function (Activity $activity) {
    $activity->properties = $activity->properties->put('ip', request()->ip());
});

My apologies, but would the maintainers accept a pull request for this feature? It appears to have been requested several times and I would love to help contribute directly to this project in this regard. I believe doing it in the package itself may be cleaner than putting code in the AppServiceProvider and would also be easier and more friendly for the general user.

Furthermore, IP is not always statically derived. By this I mean, it could be 'HTTP_X_FORWARDED_FOR' or 'REMOTE_ADDR', and it may be helpful for the user to make this configurable and it would seem to be useful, maybe with a flag to disable it so it does not bloat the application. We could also add an option for storing the user agent as well. :)

Please let me know if such a pull request would be acceptable.

Thank you very much for your time and consideration. :)

@ospira I would like to reject this because:

  1. the IP is part of private/personal data - which have to be handled special in europe and I would think also in other countries
  2. there are several ways users may want to save the IP address. I would like to add a relation to my device model which contains all data. Others may want to save it in an extra column or as part of the properties.

After all I also don't think that the effort to add this functionality is too much for the user. But if you want to provide it you can feel free to create your own package wich depends on spatie/laravel-activitylog and adds the logic needed to also log IP and useragent.

hi, I have a question related to this issue , I wanted to add ip , OS , Browser name , browser version
into my logs, i have added
$activity->properties = $activity->properties
->put('ip', request()->ip())
->put('os', Browser::platformName())
->put('browser', Browser::browserName())
->put('BrowserVersion', Browser::browserVersion());
my question is that where is put() method? and do we have any putAll() or similar method to add all IP, OS, Browser , Browser version in an array?

@amiiiiiink unser the hood it's a collection. So you could use the merge() method.
https://laravel.com/docs/5.8/collections#available-methods

I am using it in bootmethod in the AppServiceProvider.php

Activity::saving(function (Activity $activity) {
            $activity->properties = $activity->properties->put('agent', [
                'ip' => Request::ip(),
                'browser' => \Browser::browserName(),
                'os' => \Browser::platformName(),
                'url' => Request::fullUrl(),
            ]);
        });
Was this page helpful?
0 / 5 - 0 ratings