47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
trait LogTrait
|
|
{
|
|
/**
|
|
* PHP 로그에 메시지 저장
|
|
*
|
|
* @param string $type 로그 타입 (info, warn, error, success, debug 등)
|
|
* @param mixed $message 문자열, 배열, 객체 등
|
|
*/
|
|
public function set(mixed $message, string $type = 'debug'): void
|
|
{
|
|
// 배열/객체를 문자열로 변환
|
|
if (is_array($message) || is_object($message)) {
|
|
$message = print_r($message, true);
|
|
}
|
|
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$class = static::class;
|
|
$type = strtoupper($type);
|
|
|
|
// 로그 형식
|
|
$formatted = "[{$timestamp}] [{$class}] [{$type}] {$message}";
|
|
|
|
// PHP의 error_log()에 저장 (표준 로그파일 또는 php.ini의 error_log 경로)
|
|
error_log($formatted);
|
|
}
|
|
public function log_error(string $message): void
|
|
{
|
|
$this->set($message, 'error');
|
|
}
|
|
public function log_info(string $message): void
|
|
{
|
|
$this->set($message, 'info');
|
|
}
|
|
public function log_debug(string $message): void
|
|
{
|
|
$this->set($message, 'debug');
|
|
}
|
|
public function log_success(string $message): void
|
|
{
|
|
$this->set($message, 'success');
|
|
}
|
|
}
|