Skip to content

Commit 81c6ee3

Browse files
committed
feat(trait): [LogWithClassPath] added
1 parent 0caa38b commit 81c6ee3

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Concerns/LogWithClassPath.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace Onramplab\LaravelLogEnhancement\Concerns;
3+
4+
use Illuminate\Support\Facades\Log;
5+
6+
trait LogWithClassPath {
7+
public function debug(string $message, array $context = [])
8+
{
9+
$this->log('debug', $message, $context);
10+
}
11+
12+
public function info(string $message, array $context = [])
13+
{
14+
$this->log('info', $message, $context);
15+
}
16+
17+
public function notice(string $message, array $context = [])
18+
{
19+
$this->log('notice', $message, $context);
20+
}
21+
22+
public function warning(string $message, array $context = [])
23+
{
24+
$this->log('warning', $message, $context);
25+
}
26+
27+
public function error(string $message, array $context = [])
28+
{
29+
$this->log('error', $message, $context);
30+
}
31+
32+
public function critical(string $message, array $context = [])
33+
{
34+
$this->log('critical', $message, $context);
35+
}
36+
37+
38+
public function alert(string $message, array $context = [])
39+
{
40+
$this->log('alert', $message, $context);
41+
}
42+
43+
public function emergency(string $message, array $context = [])
44+
{
45+
$this->log('emergency', $message, $context);
46+
}
47+
48+
protected function log(string $logLevel, string $message, array $context = [])
49+
{
50+
$className = get_class($this);
51+
$context = array_merge($context, [
52+
'class_path' => $className,
53+
]);
54+
55+
Log::log($logLevel, $message, $context);
56+
}
57+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns;
4+
5+
use Onramplab\LaravelLogEnhancement\Tests\TestCase;
6+
use Illuminate\Support\Facades\Log;
7+
use Onramplab\LaravelLogEnhancement\Concerns\LogWithClassPath;
8+
9+
class LogWithClassPathTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*
14+
* @return void
15+
*/
16+
public function log_should_include_class_path_in_context()
17+
{
18+
$this->object = new Fake();
19+
Log::spy();
20+
21+
$this->object->run();
22+
23+
Log
24+
::shouldHaveReceived('log', function ($logLevel, $message, $context) {
25+
return $logLevel === 'info'
26+
&& $message === 'Test'
27+
&& $context['class_path'] === 'Onramplab\LaravelLogEnhancement\Tests\Unit\Concerns\Fake';
28+
})
29+
->once();
30+
}
31+
}
32+
33+
class Fake {
34+
use LogWithClassPath;
35+
36+
public function run()
37+
{
38+
$this->info('Test');
39+
}
40+
}

0 commit comments

Comments
 (0)