Buffering activities | 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  Buffering activities

 Version   v1      

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

  Buffering activities    
- [ 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)

 Buffering activities
====================

###  On this page 

1. [ Enabling the buffer ](#content-enabling-the-buffer)
2. [ How it works ](#content-how-it-works)
3. [ No ID until flush ](#content-no-id-until-flush)
4. [ Jobs and commands ](#content-jobs-and-commands)

By default, each activity is saved to the database immediately with its own insert query. If your application logs many activities during a single request, this can result in a significant number of queries.

When buffering is enabled, activities are collected in memory and inserted in bulk within one transaction. Only enable buffering for high-volume logging.

Enabling the buffer
-----------------------------------------------------------------------------------------------------------------

```
ACTIVITYLOG_BUFFER_ENABLED=true
```

Or set `Config.Buffer = true`. The package installs middleware that gives each HTTP request its own logger. Existing manual logging and model observers share that request's buffer.

How it works
--------------------------------------------------------------------------------------------

Unlike PHP's terminating callback, Go HTTP buffers flush after the handler returns but before its response is sent. A flush failure becomes a failed response rather than being hidden after success. Failed handler responses still flush activity that was not rolled back.

SQLStore groups consecutive records with matching columns, preserving log order. Large groups are split into bounded queries. Custom stores use `Save` per record unless they also implement `BatchStore`. Both paths run inside one transaction.

Records join the buffer only after their source transaction commits. A rollback discards them. Call `Flush` inside a transaction to schedule its flush after commit. Failed inserts roll back the whole flush and retain pending activity for retry. If a connection or cancellation error leaves commit outcome uncertain, inspect persisted data before retrying.

No ID until flush
-----------------------------------------------------------------------------------------------------------

Buffered `Log` returns a detached standard draft with ID zero. Later mutations of the input maps or returned draft do not change the pending insert. Query the selected store after a successful flush to retrieve persisted records. Custom models are constructed by their store at flush time.

Jobs and commands
-----------------------------------------------------------------------------------------------------------

Jobs and commands explicitly retain their logger and flush it before returning. There is no shutdown hook and no process-wide shared buffer:

```
logger := activitylog.From(ctx).Buffered()
scoped := activitylog.WithLogger(ctx, logger)
if err := model.Update(scoped, &article, db.Values{"name": "New name"}); err != nil {
    return err
}
return logger.Flush(scoped)
```

`WithLogger` makes automatic observers use the same buffer. Do not create a new logger for every operation in a buffered job. Keep it until flush succeeds or you deliberately abandon that job's pending audit data.

 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)
