WPLogger.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Services\Logger; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Logger\LoggerInterface; |
| 6 | |
| 7 | /** |
| 8 | * Class WPLogger |
| 9 | * |
| 10 | * WordPress-based logger implementation using error_log() |
| 11 | * |
| 12 | * @package AmeliaBooking\Infrastructure\Services\Logger |
| 13 | */ |
| 14 | class WPLogger implements LoggerInterface |
| 15 | { |
| 16 | private string $prefix; |
| 17 | |
| 18 | public function __construct(string $prefix = 'Amelia') |
| 19 | { |
| 20 | $this->prefix = $prefix; |
| 21 | } |
| 22 | |
| 23 | public function info(string $message, array $context = []): void |
| 24 | { |
| 25 | $this->log('INFO', $message, $context); |
| 26 | } |
| 27 | |
| 28 | public function error(string $message, array $context = []): void |
| 29 | { |
| 30 | $this->log('ERROR', $message, $context); |
| 31 | } |
| 32 | |
| 33 | public function warning(string $message, array $context = []): void |
| 34 | { |
| 35 | $this->log('WARNING', $message, $context); |
| 36 | } |
| 37 | |
| 38 | public function debug(string $message, array $context = []): void |
| 39 | { |
| 40 | $this->log('DEBUG', $message, $context); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Internal method to log messages |
| 45 | * |
| 46 | * @param string $level |
| 47 | * @param string $message |
| 48 | * @param array $context |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | private function log(string $level, string $message, array $context = []): void |
| 53 | { |
| 54 | $contextStr = !empty($context) ? ' | ' . json_encode($context) : ''; |
| 55 | error_log("[{$this->prefix}] [{$level}] {$message}{$contextStr}"); |
| 56 | } |
| 57 | } |
| 58 |