Customizing actions | laravel-activitylog-go | Spatie       

 SPATIE  

laravel-activitylog-go
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog-go](https://spatie.be/docs/laravel-activitylog-go/v1)  Advanced-usage  Customizing actions

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-activitylog-go/v1) 

  Customizing actions    
- [ Introduction ](https://spatie.be/docs/laravel-activitylog-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog-go/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-activitylog-go/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog-go/v1/questions-and-issues)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog-go/v1/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-activitylog-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-activitylog-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-activitylog-go/v1/support-us)

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

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

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

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/logging-model-events)
- [ Define causer for runtime ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/disabling-logging)
- [ Customizing actions ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/customizing-actions)
- [ Before logging hook ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/before-logging-hook)
- [ Buffering activities ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/buffering)
- [ Log options ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/log-options)
- [ Causer resolver ](https://spatie.be/docs/laravel-activitylog-go/v1/advanced-usage/causer-resolver)

 Customizing actions
===================

###  On this page 

1. [ Available actions ](#content-available-actions)
2. [ Overriding an action ](#content-overriding-an-action)
3. [ Manipulating changes ](#content-manipulating-changes)
4. [ Custom activity models ](#content-custom-activity-models)
5. [ Overridable behavior ](#content-overridable-behavior)

The package exposes function hooks and a storage contract for its core operations. These replace PHP action inheritance and configured model class names.

Available actions
-----------------------------------------------------------------------------------------------------------

`Config.LogAction` wraps persistence after description replacement and hooks. It receives the activity and a `SaveFunc` that uses the configured store and buffer policy. `Config.CleanAction` receives the selected store, cutoff and optional log name. Both apply to manual and framework entry points.

Overriding an action
--------------------------------------------------------------------------------------------------------------------

```
cfg.LogAction = func(ctx context.Context, a *activitylog.Activity, save activitylog.SaveFunc) (activitylog.Record, error) {
    a.Description = "audit: " + a.Description
    return save(ctx, a)
}
```

Use `ResolveDescription` to replace placeholder formatting, `TransformChanges` to redact changes, and `BeforeLogging` to enrich records. To dispatch a custom queue job, implement the action in your application, register that job with `app.Jobs` and serialize only activity data. The package does not ship a job class or assume your queue policy.

Manipulating changes
--------------------------------------------------------------------------------------------------------------------

```
cfg.TransformChanges = func(ctx context.Context, a *activitylog.Activity) error {
    for _, key := range []string{"attributes", "old"} {
        if values, ok := a.AttributeChanges[key].(activitylog.Attributes); ok {
            delete(values, "password")
        }
    }
    return nil
}
```

Observer-generated change maps are `Attributes` before saving. If you supplied decoded JSON manually, its nested maps are `map[string]any`, so handle that type too. Selecting safe attributes is preferable to redacting after capture.

Custom activity models
--------------------------------------------------------------------------------------------------------------------------

Set `Config.Store` to a type implementing `Store`. Its `Save` can construct your native model and return it as a `Record`. Embed `Activity` in that model to inherit `ActivityData`. `Query` returns the same concrete record type. `Clean` implements retention for that model's destination.

```
type Audit struct {
    activitylog.Activity
    Tenant string `json:"tenant"`
}

func (Audit) TableName() string { return "audits" }

func (AuditStore) Save(ctx context.Context, a *activitylog.Activity) (activitylog.Record, error) {
    record := &Audit{Activity: *a, Tenant: "shop"}
    return record, model.Create(ctx, record)
}
```

Implement `Query(context.Context, Filter) ([]Record, error)` and `Clean(context.Context, time.Time, *string) (int64, error)` on `AuditStore` too. `Filter.Apply(db.Table("audits"))` applies the standard filters to a native query. For example, call `.Get[Audit](ctx)` and convert the resulting slice to `[]Record` containing pointers to those models. Cleanup can delegate to `SQLStore{Table: "audits"}.Clean` if the standard retention columns apply.

Register your schema with application migrations. Set `SkipMigrations` if you do not want the default table. The selected store is used across immediate saves, queries, buffered flushes and the cleanup command. Writes must honor the supplied transaction context. Do not embed SQLStore in a custom store that overrides `Save` unless its inherited `SaveBatch` is also correct for your custom model. Prefer composition or implement `BatchStore` explicitly.

Overridable behavior
--------------------------------------------------------------------------------------------------------------------

PHP extension pointGo replacementactivity model`Store` and `Record``resolveDescription`, `replacePlaceholders``Config.ResolveDescription``transformChanges``Config.TransformChanges`subject `beforeActivityLogged``Observer.BeforeLogging`global `beforeLogging``Config.BeforeLogging``save``Config.LogAction`, `SaveFunc`, `Store.Save`cleanup cutoff and deletion`Config.CleanAction`, `Store.Clean`causer resolver`Config.CauserResolver`, `WithCauser` 

 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)
