# Audit Logging Architecture {#audit-logging-architecture}


How entity changes are tracked for compliance and traceability.


## Overview {#overview}


The audit logging system records:

- Who changed what entity
- What changed (old vs new values)
- When it changed
- IP address and request context


## Activity Log Flow {#activity-log-flow}






```mermaid
sequenceDiagram
    User->>API: PUT /api/task/:id
    API->>Guard: TenantPermissionGuard
    API->>Interceptor: ActivityLogInterceptor
    Interceptor->>Service: Get entity before update
    Service->>Database: Update entity
    Interceptor->>ActivityLog: Save change record
    ActivityLog->>Database: INSERT activity_log
```






## ActivityLog Entity {#activitylog-entity}


| Column         | Type     | Description               |
| -------------- | -------- | ------------------------- |
| `id`           | UUID     | Log entry ID              |
| `entity`       | string   | Entity name               |
| `entityId`     | UUID     | ID of changed entity      |
| `action`       | enum     | CREATED, UPDATED, DELETED |
| `actorType`    | enum     | USER, SYSTEM              |
| `actorId`      | UUID     | User who made the change  |
| `data`         | JSON     | New values                |
| `previousData` | JSON     | Old values                |
| `ipAddress`    | string   | Client IP                 |
| `createdAt`    | datetime | Timestamp                 |


## Using the Decorator {#using-the-decorator}


```typescript
@ActivityLog('TASK_UPDATED')
@Put(':id')
async update(@Param('id') id: string, @Body() dto: UpdateTaskDTO) {
  return this.taskService.update(id, dto);
}
```


## Querying Logs {#querying-logs}


```
GET /api/activity-log?entity=Task&entityId=uuid&action=UPDATED
```


## Related Pages {#related-pages}


- [Activity Log Endpoints](https://sourcey-gauzy.pages.dev/api/activity-log-endpoints/index.md) — activity API
- [Decorator System](https://sourcey-gauzy.pages.dev/architecture/decorator-system/index.md) — custom decorators
