44 lines
1.7 KiB
PHP
44 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
if (! function_exists('audit_log')) {
|
||
|
|
/**
|
||
|
|
* CRUD 활동 로그 기록
|
||
|
|
*
|
||
|
|
* @param string $action 'create', 'update', 'delete'
|
||
|
|
* @param string $table 대상 테이블명
|
||
|
|
* @param int $recordId 대상 레코드 PK
|
||
|
|
* @param array|null $before 변경 전 데이터 (update/delete 시)
|
||
|
|
* @param array|null $after 변경 후 데이터 (create/update 시)
|
||
|
|
*/
|
||
|
|
function audit_log(string $action, string $table, int $recordId, ?array $before = null, ?array $after = null): void
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$db = \Config\Database::connect();
|
||
|
|
|
||
|
|
// 테이블 존재 여부 확인 (없으면 skip)
|
||
|
|
if ($db->query("SHOW TABLES LIKE 'activity_log'")->getNumRows() === 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$mbIdx = session()->get('mb_idx');
|
||
|
|
$ip = service('request')->getIPAddress();
|
||
|
|
|
||
|
|
model(\App\Models\ActivityLogModel::class)->insert([
|
||
|
|
'al_mb_idx' => $mbIdx ? (int) $mbIdx : null,
|
||
|
|
'al_action' => $action,
|
||
|
|
'al_table' => $table,
|
||
|
|
'al_record_id' => $recordId,
|
||
|
|
'al_data_before' => $before !== null ? json_encode($before, JSON_UNESCAPED_UNICODE) : null,
|
||
|
|
'al_data_after' => $after !== null ? json_encode($after, JSON_UNESCAPED_UNICODE) : null,
|
||
|
|
'al_ip' => $ip,
|
||
|
|
'al_regdate' => date('Y-m-d H:i:s'),
|
||
|
|
]);
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
// 로깅 실패 시 본 로직 방해하지 않음
|
||
|
|
log_message('error', 'audit_log failed: ' . $e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|