Skip to content

Commit

Permalink
Merge pull request #31 from JohnRoux/feature/driver-update
Browse files Browse the repository at this point in the history
Logger Update 1) Custom Log Driver Update
  • Loading branch information
aungwinthant authored Mar 23, 2020
2 parents 1b91ae4 + 1e7f483 commit e9a507d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AWT\Providers\ApiLogServiceProvider::class
2. Publish the config file with:

```bash
php artisan vendor:publish --tag=config
php artisan vendor:publish --tag=config --provider="AWT\Providers\ApiLogServiceProvider"
```

The config file is called *apilogs.php*. Currently supported drivers are *db* and *file*
Expand Down Expand Up @@ -61,6 +61,7 @@ php artisan apilog:clear

1. Your driver class ***must*** implement ApiLoggerInterface for saving, retrieving and deleting the logs.
2. Your driver class may extends `AbstractLogger` class which provide helpful methods such as logData and mapArrayToModel.
3. Substitude in your new class name instead of `db` or `file` as the driver. eg: `\App\Apilogs\CustomLogger::class`

## Security

Expand Down
43 changes: 39 additions & 4 deletions config/apilog.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
<?php

return [
//currently supported drivers are 'db' and 'file'
"driver" => "file",
"filename" => "api-{Y-m-d}.log",
"dont_log" => [

/*
|--------------------------------------------------------------------------
| Log Storage Driver
|--------------------------------------------------------------------------
|
| This determines how the logs are stored.
| Currently supported drivers are 'db' and 'file'
| If you want to use your own custom driver, you can parse through your
| Class name here: Eg: `\App\Apilogs\CustomLogger::class`
| Note: your log driver MUST implement ApiLoggerInterface!
|
*/

'driver' => env('API_LOGS_DRIVER', 'file'),


/*
|--------------------------------------------------------------------------
| Filename Format
|--------------------------------------------------------------------------
|
| The name and format of the log files which are created.
| Only applicable if the 'file' driver is being used
|
*/

'filename' => env('API_LOGS_FILENAME_FORMAT', 'api-{Y-m-d}.log'),


/*
|--------------------------------------------------------------------------
| Request Exclusions
|--------------------------------------------------------------------------
|
| This sets which request data is excluded from the logging
|
*/
'dont_log' => [
'password', 'password_confirmation', 'new_password', 'old_password',
]
];
17 changes: 17 additions & 0 deletions src/Exceptions/InvalidApiLogDriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AWT\Http\Exceptions;

use Exception;
use Throwable;

class InvalidApiLogDriverException extends Exception
{
public function __construct(
$message = 'Invalid Api Log Driver',
$code = 0,
Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}
}
15 changes: 14 additions & 1 deletion src/Providers/ApiLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AWT\Providers;

use AWT\Console\Commands\ClearApiLogger;
use AWT\Http\Exceptions\InvalidApiLogDriverException;
use AWT\Http\Middleware\ApiLogger;
use AWT\Contracts\ApiLoggerInterface;
use AWT\DBLogger;
Expand All @@ -16,6 +17,7 @@ class ApiLogServiceProvider extends ServiceProvider
* Register services.
*
* @return void
* @throws \Exception
*/
public function register()
{
Expand Down Expand Up @@ -50,7 +52,16 @@ public function bindServices(){
$instance = DBLogger::class;
break;
default:
throw new Exception("Unsupported Driver");
try {
$instance = $driver;
if(!(resolve($instance) instanceof ApiLoggerInterface))
{
throw new InvalidApiLogDriverException();
}
}
catch(\ReflectionException $exception){
throw new InvalidApiLogDriverException();
}
break;
}
$this->app->singleton(ApiLoggerInterface::class,$instance);
Expand All @@ -59,11 +70,13 @@ public function bindServices(){
return new ApiLogger($app->make($instance));
});
}

public function loadConfig(){
$this->publishes([
__DIR__.'/../../config/apilog.php' => config_path('apilog.php')
], 'config');
}

public function loadRoutes(){
$this->loadRoutesFrom(__DIR__.'/../../routes/web.php');
}
Expand Down

0 comments on commit e9a507d

Please sign in to comment.