Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Logger injection #1

Merged
merged 1 commit into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/FCMServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use LaravelFCM\Sender\FCMGroup;
use LaravelFCM\Sender\FCMSender;
use Illuminate\Support\ServiceProvider;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\NullHandler;

class FCMServiceProvider extends ServiceProvider
{
Expand All @@ -32,23 +35,35 @@ public function register()
return (new FCMManager($app))->driver();
});

$this->app->singleton('fcm.logger', function ($app) {
$logger = new Logger('Laravel-FCM');
if ($app[ 'config' ]->get('fcm.log_enabled', false)) {
$logger->pushHandler(new NullHandler());
} else {
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));
}
return $logger;
});

$this->app->bind('fcm.group', function ($app) {
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_group_url');
$logger = $app[ 'fcm.logger' ];

return new FCMGroup($client, $url);
return new FCMGroup($client, $url, $logger);
});

$this->app->bind('fcm.sender', function ($app) {
$client = $app[ 'fcm.client' ];
$url = $app[ 'config' ]->get('fcm.http.server_send_url');
$logger = $app[ 'fcm.logger' ];

return new FCMSender($client, $url);
return new FCMSender($client, $url, $logger);
});
}

public function provides()
{
return ['fcm.client', 'fcm.group', 'fcm.sender'];
return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger'];
}
}
11 changes: 10 additions & 1 deletion src/Response/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LaravelFCM\Response\Exceptions\ServerResponseException;
use LaravelFCM\Response\Exceptions\InvalidRequestException;
use LaravelFCM\Response\Exceptions\UnauthorizedRequestException;
use Monolog\Logger;

/**
* Class BaseResponse.
Expand All @@ -22,13 +23,21 @@ abstract class BaseResponse
*/
protected $logEnabled = false;

/**
* The logger.
*
* @var \Monolog\Logger
*/
protected $logger;

/**
* BaseResponse constructor.
*
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function __construct(ResponseInterface $response)
public function __construct(ResponseInterface $response, Logger $logger)
{
$this->logger = $logger;
$this->isJsonResponse($response);
$this->logEnabled = app('config')->get('fcm.log_enabled', false);
$responseInJson = json_decode($response->getBody(), true);
Expand Down
12 changes: 4 additions & 8 deletions src/Response/DownstreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace LaravelFCM\Response;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Http\Message\ResponseInterface;
use Monolog\Logger;

/**
* Class DownstreamResponse.
Expand Down Expand Up @@ -99,11 +98,11 @@ class DownstreamResponse extends BaseResponse implements DownstreamResponseContr
* @param \Psr\Http\Message\ResponseInterface $response
* @param $tokens
*/
public function __construct(ResponseInterface $response, $tokens)
public function __construct(ResponseInterface $response, $tokens, Logger $logger)
{
$this->tokens = is_string($tokens) ? [$tokens] : $tokens;

parent::__construct($response);
parent::__construct($response, $logger);
}

/**
Expand Down Expand Up @@ -290,15 +289,12 @@ private function needToAddError($index, $result)
*/
protected function logResponse()
{
$logger = new Logger('Laravel-FCM');
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));

$logMessage = 'notification send to '.count($this->tokens).' devices'.PHP_EOL;
$logMessage .= 'success: '.$this->numberTokensSuccess.PHP_EOL;
$logMessage .= 'failures: '.$this->numberTokensFailure.PHP_EOL;
$logMessage .= 'number of modified token : '.$this->numberTokenModify.PHP_EOL;

$logger->info($logMessage);
$this->logger->info($logMessage);
}

/**
Expand Down
12 changes: 4 additions & 8 deletions src/Response/GroupResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace LaravelFCM\Response;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Http\Message\ResponseInterface;
use Monolog\Logger;

/**
* Class GroupResponse.
Expand Down Expand Up @@ -47,10 +46,10 @@ class GroupResponse extends BaseResponse implements GroupResponseContract
* @param \Psr\Http\Message\ResponseInterface $response
* @param $to
*/
public function __construct(ResponseInterface $response, $to)
public function __construct(ResponseInterface $response, $to, Logger $logger)
{
$this->to = $to;
parent::__construct($response);
parent::__construct($response, $logger);
}

/**
Expand All @@ -74,13 +73,10 @@ protected function parseResponse($responseInJson)
*/
protected function logResponse()
{
$logger = new Logger('Laravel-FCM');
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));

$logMessage = "notification send to group: $this->to";
$logMessage .= "with $this->numberTokensSuccess success and $this->numberTokensFailure";

$logger->info($logMessage);
$this->logger->info($logMessage);
}

/**
Expand Down
12 changes: 4 additions & 8 deletions src/Response/TopicResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace LaravelFCM\Response;

use Monolog\Logger;
use LaravelFCM\Message\Topics;
use Monolog\Handler\StreamHandler;
use Psr\Http\Message\ResponseInterface;
use Monolog\Logger;

/**
* Class TopicResponse.
Expand Down Expand Up @@ -48,10 +47,10 @@ class TopicResponse extends BaseResponse implements TopicResponseContract
* @param \Psr\Http\Message\ResponseInterface $response
* @param Topics $topic
*/
public function __construct(ResponseInterface $response, Topics $topic)
public function __construct(ResponseInterface $response, Topics $topic, Logger $logger)
{
$this->topic = $topic;
parent::__construct($response);
parent::__construct($response, $logger);
}

/**
Expand Down Expand Up @@ -103,9 +102,6 @@ private function parseError($responseInJson)
*/
protected function logResponse()
{
$logger = new Logger('Laravel-FCM');
$logger->pushHandler(new StreamHandler(storage_path('logs/laravel-fcm.log')));

$topic = $this->topic->build();

$logMessage = "notification send to topic: ".json_encode($topic);
Expand All @@ -115,7 +111,7 @@ protected function logResponse()
$logMessage .= "with error (error : $this->error)";
}

$logger->info($logMessage);
$this->logger->info($logMessage);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Sender/FCMSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi

$responseGuzzle = $this->post($request);

$responsePartial = new DownstreamResponse($responseGuzzle, $tokens);
$responsePartial = new DownstreamResponse($responseGuzzle, $tokens, $this->logger);
if (!$response) {
$response = $responsePartial;
} else {
Expand All @@ -54,7 +54,7 @@ public function sendTo($to, Options $options = null, PayloadNotification $notifi
$request = new Request($to, $options, $notification, $data);
$responseGuzzle = $this->post($request);

$response = new DownstreamResponse($responseGuzzle, $to);
$response = new DownstreamResponse($responseGuzzle, $to, $this->logger);
}

return $response;
Expand All @@ -76,7 +76,7 @@ public function sendToGroup($notificationKey, Options $options = null, PayloadNo

$responseGuzzle = $this->post($request);

return new GroupResponse($responseGuzzle, $notificationKey);
return new GroupResponse($responseGuzzle, $notificationKey, $this->logger);
}

/**
Expand All @@ -95,7 +95,7 @@ public function sendToTopic(Topics $topics, Options $options = null, PayloadNoti

$responseGuzzle = $this->post($request);

return new TopicResponse($responseGuzzle, $topics);
return new TopicResponse($responseGuzzle, $topics, $this->logger);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Sender/HTTPSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaravelFCM\Sender;

use GuzzleHttp\ClientInterface;
use Monolog\Logger;

/**
* Class BaseSender.
Expand All @@ -23,15 +24,23 @@ abstract class HTTPSender
*/
protected $url;

/**
* The logger.
*
* @var \Monolog\Logger
*/
protected $logger;

/**
* Initializes a new sender object.
*
* @param \GuzzleHttp\ClientInterface $client
* @param string $url
*/
public function __construct(ClientInterface $client, $url)
public function __construct(ClientInterface $client, $url, Logger $logger)
{
$this->client = $client;
$this->url = $url;
$this->logger = $logger;
}
}
Loading