friendship ended with social-app. php is my new best friend
1<?php declare(strict_types = 1);
2
3namespace Contributte\Logging;
4
5use DirectoryIterator;
6use Throwable;
7
8/**
9 * AbstractTracyLogger based on official Tracy\Logger (@copyright David Grudl)
10 */
11abstract class AbstractLogger implements ILogger
12{
13
14 /** @var string */
15 protected $directory;
16
17 public function __construct(string $directory)
18 {
19 $this->directory = $directory;
20 }
21
22 public function setDirectory(string $directory): void
23 {
24 $this->directory = $directory;
25 }
26
27 protected function getExceptionFile(Throwable $exception): string
28 {
29 $data = [];
30
31 while ($exception) {
32 $data[] = [
33 $exception->getMessage(),
34 $exception->getCode(),
35 $exception->getFile(),
36 $exception->getLine(),
37 array_map(function ($item): array {
38 unset($item['args']);
39
40 return $item;
41 }, $exception->getTrace()),
42 ];
43 $exception = $exception->getPrevious();
44 }
45
46 $hash = substr(md5(serialize($data)), 0, 10);
47
48 foreach (new DirectoryIterator($this->directory) as $file) {
49 if ($file->isDot()) {
50 continue;
51 }
52
53 if ((bool) strpos($file->getBasename(), $hash)) {
54 return $file->getPathname();
55 }
56 }
57
58 return $this->directory . '/exception--' . @date('Y-m-d--H-i') . '--' . $hash . '.html'; // @ timezone may not be set
59 }
60
61}