File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Onramplab \LaravelLogEnhancement ;
4+
5+ use Psr \Log \LoggerInterface ;
6+ use Ramsey \Uuid \Uuid ;
7+
8+ class Logger
9+ {
10+ protected $ debugId ;
11+ protected $ logger ;
12+
13+ /**
14+ * Create an instance of the QueryLogger class
15+ *
16+ * @param \Psr\Log\LoggerInterface $logger
17+ * @return void
18+ *
19+ * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException
20+ * @throws \InvalidArgumentException
21+ * @throws \Exception
22+ */
23+ public function __construct (LoggerInterface $ logger )
24+ {
25+ $ this ->logger = $ logger ;
26+ $ this ->debugId = Uuid::uuid4 ()->toString ();
27+ //
28+ }
29+
30+ public function __call ($ name , $ arguments )
31+ {
32+ $ message = $ arguments [0 ];
33+ $ context = $ arguments [1 ] ?? [];
34+
35+ // attach caller class
36+ $ caller = debug_backtrace ();
37+ $ caller = $ caller [2 ];
38+ $ context ['class_path ' ] = $ caller ['class ' ];
39+
40+ // attach tracking_id
41+ $ context ['tracking_id ' ] = $ this ->debugId ;
42+
43+ $ this ->logger ->log ($ name , $ message , $ context );
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Onramplab \LaravelLogEnhancement \Tests \Unit \Concerns ;
4+
5+ use Illuminate \Support \Facades \Log ;
6+ use Onramplab \LaravelLogEnhancement \Tests \TestCase ;
7+ use Onramplab \LaravelLogEnhancement \Logger ;
8+
9+ class LoggerTest extends TestCase
10+ {
11+ /**
12+ * @test
13+ *
14+ * @return void
15+ */
16+ public function log_should_include_class_path_and_uuid_in_context ()
17+ {
18+ Log::spy ();
19+ $ logger = app ()->make (Logger::class);
20+
21+ $ logger ->info ('123 ' );
22+
23+ Log
24+ ::shouldHaveReceived ('log ' , function ($ logLevel , $ message , $ context ) {
25+ return $ logLevel === 'info '
26+ && $ message === '123 '
27+ && isSet ($ context ['class_path ' ])
28+ && isSet ($ context ['tracking_id ' ]);
29+ })
30+ ->once ();
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments