Skip to content

Commit

Permalink
feat: log driver selection feature added - database, or file
Browse files Browse the repository at this point in the history
  • Loading branch information
arif98741 committed May 24, 2024
1 parent 07c740d commit 3014e87
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
37 changes: 10 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@ SMS::via(Ssl::class)->shootWithQueue("01XXXXXXXXX",'test sms');
</pre>

# Log Generate
You can generate log in database for every sms api request and save in database. For doing this. Follow below points
1. Be confirm you have completed **step-2** and **step-3**
2. Run command ``php artisan migrate``. This will create ``lbs_log`` table in your database
3. Go to your project directory and locate ``config/sms.php``
4. Find and make true ``'sms_log' => true,``
You can generate log for every sms api request and save in database or file. For doing this. Follow below points
1. Laravelbdsms stores log in two drivers(`database, file`). `database` is default. You can change it from _config/sms.php_
2. Find and make true `'sms_log' => true,`
3. Be confirm you have completed **step-2** and **step-3**
4. For `database` driver
1. Change log driver to `log_driver =>'database'` from `config/sms.php`
2. Run command `php artisan migrate`. This will create `lbs_log` table in your database
5. For `file` driver
1. Change log driver to `log_driver =>'file'` from `config/sms.php`

Otherwise, if you want more control, you can use the underlying sender object. This will not touch any laravel facade or
service provider.
Expand All @@ -119,7 +123,7 @@ use Xenon\LaravelBDSms\Provider\Ssl;
use Xenon\LaravelBDSms\Sender;

$sender = Sender::getInstance();
$sender->setProvider(Ssl::class);
$sender->setProvider(Ssl::class); //change this provider class according to need
$sender->setMobile('017XXYYZZAA');
//$sender->setMobile(['017XXYYZZAA','018XXYYZZAA']);
$sender->setMessage('helloooooooo boss!');
Expand All @@ -145,27 +149,6 @@ array:6 [▼
--------------------------------------------------
</pre>

## MimSms

<pre>
use Xenon\LaravelBDSms\Provider\MimSms;
use Xenon\LaravelBDSms\Sender;

$sender = Sender::getInstance();
$sender->setProvider(MimSms::class);
$sender->setMobile('017XXYYZZAA');
$sender->setMessage('This is test message');
$sender->setQueue(true); //if you want to sent sms from queue
$sender->setConfig(
[
'api_key' => 'api_key_goes_here',
'type' => 'text',
'senderid' => 'approved_send_id',
]
);

$status = $sender->send();
</pre>

## Sms Send Using Custom Gateway
We have tried to added most of the gateways of Bangladesh in this package as much as possible. But still if you don't find your expected gateway in this list, then use Custom Gateway using following code snippet.
Expand Down
8 changes: 7 additions & 1 deletion src/Job/SendSmsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log as LaravelLog;
use JsonException;
use Xenon\LaravelBDSms\Facades\Logger;

Expand Down Expand Up @@ -140,7 +141,12 @@ private function insertLoggerLog(array $log): void
{
$config = Config::get('sms');
if ($config['sms_log']) {
Logger::createLog($log);

if ($config['log_driver'] === 'database') {
Logger::createLog($log);
} else if ($config['log_driver'] === 'file') {
LaravelLog::info('laravelbdsms',$log);
}
}
}

Expand Down
35 changes: 26 additions & 9 deletions src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log as LaravelLog;
use JsonException;
use Xenon\LaravelBDSms\Facades\Logger;
use Xenon\LaravelBDSms\Handler\ParameterException;
Expand Down Expand Up @@ -81,6 +82,9 @@ class Sender
| On subsequent runs, it returns the client existing object stored in the static field. This implementation
| lets you subclass the Singleton class while keeping just one instance of each subclass around.
*/
private array $headers;
private bool $contentTypeJson;

/**
* @throws RenderException
*/
Expand Down Expand Up @@ -364,15 +368,28 @@ private function logGenerate($config, $response): void

$providerResponse = $object->response;

Logger::createLog([
'provider' => get_class($this->provider),
'request_json' => json_encode([
'config' => $config['providers'][get_class($this->provider)],
'mobile' => $this->getMobile(),
'message' => $this->getMessage()
], JSON_THROW_ON_ERROR),
'response_json' => json_encode($providerResponse, JSON_THROW_ON_ERROR)
]);
$providerClass = get_class($this->provider);
$requestData = [
'config' => $config['providers'][$providerClass],
'mobile' => $this->getMobile(),
'message' => $this->getMessage()
];

if ($config['log_driver'] === 'database') {
$logData = [
'provider' => $providerClass,
'request_json' => json_encode($requestData, JSON_THROW_ON_ERROR),
'response_json' => json_encode($providerResponse, JSON_THROW_ON_ERROR)
];
Logger::createLog($logData);
} elseif ($config['log_driver'] === 'file') {
$logData = [
'provider' => $providerClass,
'request_json' => $requestData,
'response_json' => $providerResponse,
];
LaravelLog::info('laravelbdsms', $logData);
}
}
}

Expand Down

0 comments on commit 3014e87

Please sign in to comment.