Using multiple logs | laravel-activitylog | Spatie

 SPATIE

  Laravel Activity Log
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog](https://spatie.be/docs/laravel-activitylog/v1)  Advanced-usage  Using multiple logs

 Version   v5   v4   v3   v2   v1

 Other versions for crawler [v5](https://spatie.be/docs/laravel-activitylog/v5) [v4](https://spatie.be/docs/laravel-activitylog/v4) [v3](https://spatie.be/docs/laravel-activitylog/v3) [v2](https://spatie.be/docs/laravel-activitylog/v2) [v1](https://spatie.be/docs/laravel-activitylog/v1)

  Using multiple logs
- [ Introduction ](https://spatie.be/docs/laravel-activitylog/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-activitylog/v1/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog/v1/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-activitylog/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-activitylog/v1/about-us)

Basic usage
-----------

- [ Logging activity ](https://spatie.be/docs/laravel-activitylog/v1/basic-usage/logging-activity)
- [ Cleaning up the log ](https://spatie.be/docs/laravel-activitylog/v1/basic-usage/cleaning-up-the-log)

Advanced usage
--------------

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog/v1/advanced-usage/logging-model-events)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog/v1/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog/v1/advanced-usage/using-multiple-logs)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-activitylog                                                                                                                                                                                                                                    `

Using multiple logs
===================

###  On this page

1. [ The default log ](#content-the-default-log)
2. [ Specifying a log ](#content-specifying-a-log)
3. [ Specifying a log for each model ](#content-specifying-a-log-for-each-model)
4. [ Retrieving activity ](#content-retrieving-activity)

The default log
-----------------------------------------------------------------------------------------------------

Without specifying a log name the activities will be logged on the default log.

```
activity()->log('hi');

$lastActivity = Spatie\Activitylog\Models\Activity::all()->last();

$lastActivity->log_name; //returns 'default';
```

You can specify the name of the default log in the `default_log_name` key of the config file.

Specifying a log
--------------------------------------------------------------------------------------------------------

You can specify the log on which an activity must be logged by passing the log name to the `activity` function:

```
activity('other-log')->log("hi");

Activity::all()->last()->log_name; //returns 'other-log';
```

Specifying a log for each model
-----------------------------------------------------------------------------------------------------------------------------------------------------

By default, the `LogsActivity` trait uses `default_log_name` from the config file to write the logs. You can set a different log for each model by overriding the `getLogNameToUse()` function in your model.

```
public function getLogNameToUse(string $eventName = ''): string
{
   return 'custom_log_name_for_this_model';
}
```

Retrieving activity
-----------------------------------------------------------------------------------------------------------------

The `Activity` model is just a regular Eloquent model that you know and love:

```
Activity::where('log_name' , 'other-log')->get(); //returns all activity from the 'other-log'
```

There's also an `inLog` scope you can use:

```
Activity::inLog('other-log')->get();

//you can pass multiple log names to the scope
Activity::inLog('default', 'other-log')->get();

//passing an array is just as good
Activity::inLog(['default', 'other-log'])->get();
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
