Skip to content

Commit eb4675b

Browse files
committed
feat: [Logger] extend Laravel's Logger and log extra info when writing log
1 parent f44eba9 commit eb4675b

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/Logger.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,61 @@
22

33
namespace Onramplab\LaravelLogEnhancement;
44

5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Log\Logger as IlluminateLogger;
57
use Psr\Log\LoggerInterface;
68
use Ramsey\Uuid\Uuid;
79

8-
class Logger
10+
class Logger extends IlluminateLogger
911
{
12+
/**
13+
* @var string
14+
*/
1015
protected $debugId;
16+
17+
/**
18+
* LoggerInterface
19+
*/
1120
protected $logger;
1221

1322
/**
14-
* Create an instance of the QueryLogger class
23+
* @var string
24+
*/
25+
protected $channelName;
26+
27+
/**
28+
* Create a new log writer instance.
1529
*
16-
* @param \Psr\Log\LoggerInterface $logger
30+
* @param \Psr\Log\LoggerInterface $logger
31+
* @param \Illuminate\Contracts\Events\Dispatcher|null $dispatcher
1732
* @return void
18-
*
19-
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException
20-
* @throws \InvalidArgumentException
21-
* @throws \Exception
2233
*/
23-
public function __construct(LoggerInterface $logger)
34+
public function __construct(LoggerInterface $logger, Dispatcher $dispatcher = null)
2435
{
2536
$this->logger = $logger;
37+
$this->dispatcher = $dispatcher;
2638
$this->debugId = Uuid::uuid4()->toString();
27-
//
2839
}
2940

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)
3150
{
32-
$message = $arguments[0];
33-
$context = $arguments[1] ?? [];
34-
35-
// attach caller class
51+
// attach class_path
52+
// NOTE: it's hardcoded, should find a better way to get caller class
3653
$caller = debug_backtrace();
37-
$caller = $caller[2];
54+
$caller = $caller[4];
3855
$context['class_path'] = $caller['class'];
3956

4057
// attach tracking_id
4158
$context['tracking_id'] = $this->debugId;
4259

43-
$this->logger->log($name, $message, $context);
60+
parent::writeLog($level, $message, $context);
4461
}
4562
}

tests/Unit/LoggerTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns;
44

5+
use Illuminate\Events\Dispatcher;
6+
use Illuminate\Foundation\Application;
57
use Illuminate\Support\Facades\Log;
8+
use Mockery;
69
use Onramplab\LaravelLogEnhancement\Tests\TestCase;
710
use Onramplab\LaravelLogEnhancement\Logger;
11+
use Psr\Log\LoggerInterface;
812

913
class LoggerTest extends TestCase
1014
{
@@ -15,18 +19,20 @@ class LoggerTest extends TestCase
1519
*/
1620
public function log_should_include_class_path_and_uuid_in_context()
1721
{
18-
Log::spy();
19-
$logger = app()->make(Logger::class);
22+
$this->app->instance(Logger::class, Mockery::mock(Logger::class));
2023

21-
$logger->info('123');
24+
$logger = app()
25+
->make(Logger::class)
26+
->makePartial();
2227

23-
Log
24-
::shouldHaveReceived('log', function ($logLevel, $message, $context) {
28+
$logger->shouldReceive('log', function ($logLevel, $message, $context) {
2529
return $logLevel === 'info'
2630
&& $message === '123'
2731
&& isSet($context['class_path'])
2832
&& isSet($context['tracking_id']);
2933
})
3034
->once();
35+
36+
$logger->info('123');
3137
}
3238
}

0 commit comments

Comments
 (0)