|
2 | 2 |
|
3 | 3 | namespace Onramplab\LaravelLogEnhancement; |
4 | 4 |
|
| 5 | +use Illuminate\Contracts\Events\Dispatcher; |
| 6 | +use Illuminate\Log\Logger as IlluminateLogger; |
5 | 7 | use Psr\Log\LoggerInterface; |
6 | 8 | use Ramsey\Uuid\Uuid; |
7 | 9 |
|
8 | | -class Logger |
| 10 | +class Logger extends IlluminateLogger |
9 | 11 | { |
| 12 | + /** |
| 13 | + * @var string |
| 14 | + */ |
10 | 15 | protected $debugId; |
| 16 | + |
| 17 | + /** |
| 18 | + * LoggerInterface |
| 19 | + */ |
11 | 20 | protected $logger; |
12 | 21 |
|
13 | 22 | /** |
14 | | - * Create an instance of the QueryLogger class |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $channelName; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new log writer instance. |
15 | 29 | * |
16 | | - * @param \Psr\Log\LoggerInterface $logger |
| 30 | + * @param \Psr\Log\LoggerInterface $logger |
| 31 | + * @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher |
17 | 32 | * @return void |
18 | | - * |
19 | | - * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException |
20 | | - * @throws \InvalidArgumentException |
21 | | - * @throws \Exception |
22 | 33 | */ |
23 | | - public function __construct(LoggerInterface $logger) |
| 34 | + public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null) |
24 | 35 | { |
25 | 36 | $this->logger = $logger; |
| 37 | + $this->dispatcher = $dispatcher; |
26 | 38 | $this->debugId = Uuid::uuid4()->toString(); |
27 | | - // |
28 | 39 | } |
29 | 40 |
|
30 | | - public function __call($name, $arguments) |
| 41 | + /** |
| 42 | + * Write a message to the log. |
| 43 | + * |
| 44 | + * @param string $level |
| 45 | + * @param string $message |
| 46 | + * @param array $context |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + protected function writeLog($level, $message, $context) |
31 | 50 | { |
32 | | - $message = $arguments[0]; |
33 | | - $context = $arguments[1] ?? []; |
| 51 | + $info = $this->generateExtraContextInfo(); |
| 52 | + $context = array_merge($context, $info); |
| 53 | + |
| 54 | + parent::writeLog($level, $message, $context); |
| 55 | + } |
| 56 | + |
| 57 | + protected function generateExtraContextInfo() |
| 58 | + { |
| 59 | + $info = []; |
| 60 | + |
| 61 | + // attach class_path |
| 62 | + // NOTE: it's hardcoded, should find a better way to get caller class |
| 63 | + $stack = debug_backtrace(); |
| 64 | + $caller = $stack[2]; |
| 65 | + |
| 66 | + if ($caller['class'] === 'Illuminate\Log\LogManager') { |
| 67 | + // It means log from channel |
| 68 | + $caller = $stack[4]; |
| 69 | + } |
34 | 70 |
|
35 | | - // attach caller class |
36 | | - $caller = debug_backtrace(); |
37 | | - $caller = $caller[2]; |
38 | | - $context['class_path'] = $caller['class']; |
| 71 | + $info['class_path'] = $caller['class']; |
39 | 72 |
|
40 | 73 | // attach tracking_id |
41 | | - $context['tracking_id'] = $this->debugId; |
| 74 | + $info['tracking_id'] = $this->debugId; |
42 | 75 |
|
43 | | - $this->logger->log($name, $message, $context); |
| 76 | + return $info; |
44 | 77 | } |
45 | 78 | } |
0 commit comments