11<?php namespace Rollbar \Laravel ;
22
3+ use Illuminate \Contracts \Container \BindingResolutionException ;
4+ use Illuminate \Contracts \Events \Dispatcher ;
35use Rollbar \Rollbar ;
46use Rollbar \RollbarLogger ;
57use Illuminate \Support \Arr ;
1012
1113class RollbarServiceProvider extends ServiceProvider
1214{
15+ /**
16+ * The telemetry event listener.
17+ */
18+ protected TelemetryListener $ telemetryListener ;
19+
1320 /**
1421 * Register the service provider.
1522 */
@@ -21,35 +28,11 @@ public function register(): void
2128 }
2229
2330 $ this ->app ->singleton (RollbarLogger::class, function (Application $ app ) {
31+ $ config = $ this ->getConfigs ($ app );
2432
25- $ defaults = [
26- 'environment ' => $ app ->environment (),
27- 'root ' => base_path (),
28- 'handle_exception ' => true ,
29- 'handle_error ' => true ,
30- 'handle_fatal ' => true ,
31- ];
32-
33- $ config = array_merge ($ defaults , $ app ['config ' ]->get ('logging.channels.rollbar ' , []));
34-
35- $ config ['access_token ' ] = static ::config ('access_token ' );
36-
37- if (empty ($ config ['access_token ' ])) {
38- throw new InvalidArgumentException ('Rollbar access token not configured ' );
39- }
40-
41- $ handleException = (bool ) Arr::pull ($ config , 'handle_exception ' );
42- $ handleError = (bool ) Arr::pull ($ config , 'handle_error ' );
43- $ handleFatal = (bool ) Arr::pull ($ config , 'handle_fatal ' );
44-
45- // Convert a request for the Rollbar agent to handle the logs to
46- // the format expected by `Rollbar::init`.
47- // @see https://github.com/rollbar/rollbar-php-laravel/issues/85
48- $ handler = Arr::get ($ config , 'handler ' );
49- if ($ handler === AgentHandler::class) {
50- $ config ['handler ' ] = 'agent ' ;
51- }
52- $ config ['framework ' ] = 'laravel ' . app ()->version ();
33+ $ handleException = (bool )Arr::pull ($ config , 'handle_exception ' );
34+ $ handleError = (bool )Arr::pull ($ config , 'handle_error ' );
35+ $ handleFatal = (bool )Arr::pull ($ config , 'handle_fatal ' );
5336 Rollbar::init ($ config , $ handleException , $ handleError , $ handleFatal );
5437
5538 return Rollbar::logger ();
@@ -58,20 +41,39 @@ public function register(): void
5841 $ this ->app ->singleton (MonologHandler::class, function (Application $ app ) {
5942
6043 $ level = static ::config ('level ' , 'debug ' );
61-
44+
6245 $ handler = new MonologHandler ($ app [RollbarLogger::class], $ level );
6346 $ handler ->setApp ($ app );
6447
6548 return $ handler ;
6649 });
6750 }
6851
52+ /**
53+ * Boot is called after all services are registered.
54+ *
55+ * This is where we can start listening for events.
56+ *
57+ * @param RollbarLogger $logger This parameter is injected by the service container, and is required to ensure that
58+ * the Rollbar logger is initialized.
59+ * @return void
60+ *
61+ * @since 8.1.0
62+ */
63+ public function boot (RollbarLogger $ logger ): void
64+ {
65+ // Set up telemetry if it is enabled.
66+ if (null !== Rollbar::getTelemeter ()) {
67+ $ this ->setupTelemetry ($ this ->getConfigs ($ this ->app ));
68+ }
69+ }
70+
6971 /**
7072 * Check if we should prevent the service from registering.
7173 *
7274 * @return boolean
7375 */
74- public function stop () : bool
76+ public function stop (): bool
7577 {
7678 $ level = static ::config ('level ' );
7779
@@ -85,8 +87,8 @@ public function stop() : bool
8587 /**
8688 * Return a rollbar logging config.
8789 *
88- * @param string $key The config key to lookup.
89- * @param mixed $default The default value to return if the config is not found.
90+ * @param string $key The config key to lookup.
91+ * @param mixed $default The default value to return if the config is not found.
9092 *
9193 * @return mixed
9294 */
@@ -98,8 +100,66 @@ protected static function config(string $key = '', mixed $default = null): mixed
98100 $ envKey = 'ROLLBAR_TOKEN ' ;
99101 }
100102
101- $ logKey = empty ($ key ) ? 'logging.channels.rollbar ' : " logging.channels.rollbar. $ key" ;
103+ $ logKey = empty ($ key ) ? 'logging.channels.rollbar ' : ' logging.channels.rollbar. ' . $ key ;
102104
103105 return getenv ($ envKey ) ?: Config::get ($ logKey , $ default );
104106 }
107+
108+ /**
109+ * Returns the Rollbar configuration.
110+ *
111+ * @param Application $app The Laravel application.
112+ * @return array
113+ *
114+ * @since 8.1.0
115+ *
116+ * @throw InvalidArgumentException If the Rollbar access token is not configured.
117+ */
118+ public function getConfigs (Application $ app ): array
119+ {
120+ $ defaults = [
121+ 'environment ' => $ app ->environment (),
122+ 'root ' => base_path (),
123+ 'handle_exception ' => true ,
124+ 'handle_error ' => true ,
125+ 'handle_fatal ' => true ,
126+ ];
127+
128+ $ config = array_merge ($ defaults , $ app ['config ' ]->get ('logging.channels.rollbar ' , []));
129+ $ config ['access_token ' ] = static ::config ('access_token ' );
130+
131+ if (empty ($ config ['access_token ' ])) {
132+ throw new InvalidArgumentException ('Rollbar access token not configured ' );
133+ }
134+ // Convert a request for the Rollbar agent to handle the logs to
135+ // the format expected by `Rollbar::init`.
136+ // @see https://github.com/rollbar/rollbar-php-laravel/issues/85
137+ $ handler = Arr::get ($ config , 'handler ' , MonologHandler::class);
138+ if ($ handler === AgentHandler::class) {
139+ $ config ['handler ' ] = 'agent ' ;
140+ }
141+ $ config ['framework ' ] = 'laravel ' . $ app ->version ();
142+ return $ config ;
143+ }
144+
145+ /**
146+ * Sets up the telemetry event listeners.
147+ *
148+ * @param array $config
149+ * @return void
150+ *
151+ * @since 8.1.0
152+ */
153+ protected function setupTelemetry (array $ config ): void
154+ {
155+ $ this ->telemetryListener = new TelemetryListener ($ this ->app , $ config );
156+
157+ try {
158+ $ dispatcher = $ this ->app ->make (Dispatcher::class);
159+ } catch (BindingResolutionException $ e ) {
160+ return ;
161+ }
162+
163+ $ this ->telemetryListener ->listen ($ dispatcher );
164+ }
105165}
0 commit comments