| 1: | <?php |
| 2: | namespace Worldline\Connect\Sdk\Logging; |
| 3: | |
| 4: | use Exception; |
| 5: | use SplFileObject; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class SplFileObjectLogger implements CommunicatorLogger |
| 13: | { |
| 14: | const DATE_FORMAT_STRING = DATE_ATOM; |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | private SplFileObject $splFileObject; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public function __construct(SplFileObject $splFileObject) |
| 25: | { |
| 26: | $this->splFileObject = $splFileObject; |
| 27: | } |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public function getSplFileObject(): SplFileObject |
| 33: | { |
| 34: | return $this->splFileObject; |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public function log(string $message): void |
| 43: | { |
| 44: | $this->splFileObject->fwrite($this->getDatePrefix() . $message . PHP_EOL); |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function logException(string $message, Exception $exception): void |
| 54: | { |
| 55: | $this->splFileObject->fwrite($this->getDatePrefix() . $message . PHP_EOL . $exception . PHP_EOL); |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | protected function getDatePrefix(): string |
| 62: | { |
| 63: | return date(static::DATE_FORMAT_STRING) . ' '; |
| 64: | } |
| 65: | } |
| 66: | |