File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Onramplab \LaravelLogEnhancement ;
4+
5+ use Illuminate \Support \ServiceProvider ;
6+
7+ class DatadogLoggingServiceProvider extends ServiceProvider
8+ {
9+ /**
10+ * Register any application services.
11+ *
12+ * @return void
13+ */
14+ public function register ()
15+ {
16+ // can get function after install php datadog-setup
17+ if (!function_exists ('\DDTrace\current_context ' )) {
18+ return ;
19+ }
20+
21+ // Get the Monolog instance
22+ $ monolog = logger ()->getLogger ();
23+ if (!$ monolog instanceof \Monolog \Logger) {
24+ return ;
25+ }
26+
27+ foreach ($ monolog ->getHandlers () as $ handler ) {
28+ if (method_exists ($ handler , 'setFormatter ' )) {
29+ $ handler ->setFormatter (new \Monolog \Formatter \JsonFormatter ());
30+ }
31+ }
32+
33+ // Inject the trace and span ID to connect the log entry with the APM trace
34+ $ monolog ->pushProcessor (function ($ record ) {
35+ $ context = \DDTrace \current_context ();
36+ $ record ->extra ['dd ' ] = [
37+ 'trace_id ' => $ context ['trace_id ' ],
38+ 'span_id ' => $ context ['span_id ' ],
39+ ];
40+ return $ record ;
41+ });
42+ }
43+
44+ /**
45+ * Bootstrap any application services.
46+ *
47+ * @return void
48+ */
49+ public function boot ()
50+ {
51+ //
52+ }
53+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Onramplab \LaravelLogEnhancement \Tests \Unit ;
4+
5+ use Onramplab \LaravelLogEnhancement \DatadogLoggingServiceProvider ;
6+ use Orchestra \Testbench \TestCase as BaseTestCase ;
7+
8+
9+ class DatadogLoggingServiceProviderTest extends BaseTestCase
10+ {
11+ protected function getPackageProviders ($ app )
12+ {
13+ return [DatadogLoggingServiceProvider::class];
14+ }
15+
16+ /**
17+ * @test
18+ */
19+ public function provider_is_loaded ()
20+ {
21+ $ this ->assertInstanceOf (DatadogLoggingServiceProvider::class, $ this ->app ->getProvider (DatadogLoggingServiceProvider::class));
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments