Audit 모듈
📝 초안 (Draft)
이 문서는 검토 중입니다. 내용이 변경될 수 있습니다.
모델 변경 이력을 자동으로 기록하는 감사 로그 Core 모듈입니다.
개요
Audit 모듈은 모델의 CRUD 작업을 자동으로 추적하여 감사 로그를 생성합니다. Auditable Trait을 적용한 모델은 생성, 수정, 삭제 시 자동으로 변경 이력이 기록됩니다.
┌─────────────────────────────────────────────────────────────┐
│ Audit 모듈 │
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Model (Auditable Trait) │ │
│ │ │ │
│ │ create() ──┐ │ │
│ │ update() ──┼──► AuditObserver ──► AuditLog │ │
│ │ delete() ──┘ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ 기록 정보: │
│ • 이벤트 (created, updated, deleted) │
│ • 변경 전/후 값 │
│ • 실행 사용자, 테넌트, IP, URL │
└─────────────────────────────────────────────────────────────┘
핵심 컴포넌트
| 컴포넌트 | 역할 |
|---|---|
Auditable | 모델에 적용하는 Trait |
AuditableInterface | Auditable 모델 계약 |
AuditObserver | 모델 이벤트 감지 및 로그 생성 |
AuditLog | 감사 로그 저장 모델 |
Auditable Trait 적용
기본 사용법
<?php
namespace App\Models;
use App\Core\Base\Audit\Contracts\AuditableInterface;
use App\Core\Base\Audit\Traits\Auditable;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements AuditableInterface
{
use Auditable;
protected $fillable = [
'name',
'price',
'description',
];
}
적용 즉시 동작
// 생성 → created 이벤트 기록
$product = Product::create([
'name' => 'New Product',
'price' => 10000,
]);
// 수정 → updated 이벤트 기록 (변경된 필드만)
$product->update(['price' => 15000]);
// 삭제 → deleted 이벤트 기록
$product->delete();
기록되는 정보
| 필드 | 설명 | 예시 |
|---|---|---|
auditable_type | 모델 클래스명 | App\Models\Product |
auditable_id | 모델 ID | 123 |
user_id | 실행한 사용자 ID | 1 |
tenant_id | 테넌트 ID | 5 |
event | 이벤트 유형 | created, updated, deleted |
old_values | 변경 전 값 (JSON) | {"price": 10000} |
new_values | 변경 후 값 (JSON) | {"price": 15000} |
ip_address | 클라이언트 IP | 192.168.1.1 |
user_agent | 브라우저 정보 | Mozilla/5.0... |
url | 요청 URL | /admin/products/123 |
metadata | 추가 메타데이터 | {"order_number": "ORD-001"} |
created_at | 기록 시간 | 2024-01-15 10:30:00 |
커스터마이징
특정 필드만 기록
class Order extends Model implements AuditableInterface
{
use Auditable;
/**
* 이 필드들만 감사 로그에 기록
*/
protected array $auditInclude = [
'status',
'total_amount',
'payment_status',
];
}
민감 정보 제외
class User extends Model implements AuditableInterface
{
use Auditable;
/**
* 이 필드들은 감사 로그에서 제외
*/
protected array $auditExclude = [
'password',
'remember_token',
'two_factor_secret',
'api_key',
];
}
특정 이벤트만 기록
class Document extends Model implements AuditableInterface
{
use Auditable;
/**
* 이 이벤트들만 기록 (삭제는 기록 안 함)
*/
protected array $auditEvents = [
'created',
'updated',
];
}
추가 메타데이터 포함
class Order extends Model implements AuditableInterface
{
use Auditable;
/**
* 감사 로그에 추가할 메타데이터
*/
public function getAuditMetadata(): array
{
return [
'order_number' => $this->order_number,
'customer_id' => $this->customer_id,
'customer_name' => $this->customer?->name,
];
}
}