diff --git a/.phpunit.result.cache b/.phpunit.result.cache index f84992c..8cd24d1 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":206:{a:2:{s:7:"defects";a:1:{s:67:"Sdkcodes\Logpress\Tests\RequestLoggingTest::testRequestWrittenToLog";i:3;}s:5:"times";a:1:{s:67:"Sdkcodes\Logpress\Tests\RequestLoggingTest::testRequestWrittenToLog";d:0.169;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":246:{a:2:{s:7:"defects";a:2:{s:67:"Sdkcodes\Logpress\Tests\RequestLoggingTest::testRequestWrittenToLog";i:3;s:7:"Warning";i:6;}s:5:"times";a:2:{s:67:"Sdkcodes\Logpress\Tests\RequestLoggingTest::testRequestWrittenToLog";d:0.165;s:7:"Warning";d:0.001;}}} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b4233f3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + +MIT License + +Copyright (c) 2020 Pine + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..44c6118 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +> ### Log requests coming into your laravel app + +Helps you know exactly what comes in and when. The package will log all query and body parameters attached to a request. + +---------- + +# Getting started + +## Installation + +Install this package via composer + +```$ composer require sdkcodes/logpress``` + + +## Middleware Usage + +** The package comes with a middleware that you can apply either to only the routes you want to log, or all routes. + +The middleware lives in the namespace: + +`use Sdkcodes\Logpress\Middleware\LogRequests;` + +Open your `App\Http\Kernel.php` + +To log request to all routes, add the LogRequests api to the `$middleware` array: +``` +protected $middleware = [ + \App\Http\Middleware\TrustProxies::class, + \Fruitcake\Cors\HandleCors::class, + \App\Http\Middleware\CheckForMaintenanceMode::class, + \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, + \App\Http\Middleware\TrimStrings::class, + \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, + LogRequests::class +]; +``` + +Or you can add to specific middleware groups `$middlewareGroups` array e.g 'web' or 'api'. + +Or add to the `$routeMiddleware` array to apply on a route by route basis. + + +## Configuration + +By default, the package 'avoids' logging fields with the name 'password' or 'pin', however, you can overwrite these values by publishing the config file and updating the 'avoids' + +`λ php artisan vendor:publish --provider="Sdkcodes\Logpress\LogpressServiceProvider" --tag=config` + +This command publishes a new config file 'logpress.php' to your config directory and you can modify to include all the fields you want to avoid logging. + +## Output + +The output generated looks like: + +`[2020-04-25 16:42:17] local.INFO: GET / - Body: {"q":"artisan","person":"goodluck"} Query: {"q":"artisan","person":"goodluck"} Full path: http://laravelpackagedev.sdk Scheme: http` + +## LICENSE +The MIT License (MIT). Please see [License File](LICENSE) for more information. \ No newline at end of file diff --git a/src/LogWriter.php b/src/LogWriter.php index ecc7f4f..e62581a 100644 --- a/src/LogWriter.php +++ b/src/LogWriter.php @@ -8,12 +8,14 @@ class LogWriter{ public function logRequest(Request $request){ + $method = strtoupper($request->getMethod()); $body = json_encode($request->except($this->avoids())); $queryParameters = json_encode(collect($request->query())->except($this->avoids())); $fullPath = $request->url(); $uri = $request->getPathInfo(); $scheme = $request->getScheme(); + $message = "{$method} {$uri} - Body: {$body} Query: {$queryParameters} Full path: {$fullPath} Scheme: {$scheme}"; Log::info($message); diff --git a/src/LogpressServiceProvider.php b/src/LogpressServiceProvider.php index 7e99580..f4b7510 100644 --- a/src/LogpressServiceProvider.php +++ b/src/LogpressServiceProvider.php @@ -24,7 +24,7 @@ public function register() public function boot() { $this->publishes([ - __DIR__."/config/logpress.php" => config_path('logpress') + __DIR__."/config/logpress.php" => config_path('logpress.php') ], 'config'); } diff --git a/src/Middleware/LogApiRequests.php b/src/Middleware/LogRequests.php similarity index 95% rename from src/Middleware/LogApiRequests.php rename to src/Middleware/LogRequests.php index 29f535c..1060c23 100644 --- a/src/Middleware/LogApiRequests.php +++ b/src/Middleware/LogRequests.php @@ -5,7 +5,7 @@ use Closure; use Sdkcodes\Logpress\LogWriter; -class LogApiRequests +class LogRequests { /** * Handle an incoming request. diff --git a/tests/RequestLogging.php b/tests/RequestLoggingTest.php similarity index 92% rename from tests/RequestLogging.php rename to tests/RequestLoggingTest.php index d7086c4..91f5e73 100644 --- a/tests/RequestLogging.php +++ b/tests/RequestLoggingTest.php @@ -17,7 +17,7 @@ class RequestLoggingTest extends TestCase */ public function testRequestWrittenToLog() { - $response = $this->get('/')->dump(); + $response = $this->get('/'); Log::shouldReceive('info') ->with('Body: {"q" : "body"}'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 9c88a2a..174665f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,22 +2,23 @@ namespace Sdkcodes\Logpress\Tests; -use Orchestra\Testbench\Concerns\CreatesApplication; -use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use Orchestra\Testbench\TestCase as TestbenchTestCase; +use Sdkcodes\Logpress\LogpressServiceProvider; -abstract class TestCase extends BaseTestCase +class TestCase extends TestbenchTestCase { - use CreatesApplication; - public function setUp(): void{ - parent::setUp(); - } + // public function setUp(): void{ + // parent::setUp(); + // } - public function getEnvironmentSetUp($app){ - - } - - public function getPackageProviders($app){ + // public function getEnvironmentSetUp($app){ + // return [ + // LogpressServiceProvider::class, + // ]; + // } - } + // public function getPackageProviders($app){ + + // } }