Skip to content

Commit

Permalink
add readme and license
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkcodes committed Apr 25, 2020
1 parent de33ffd commit 8eee9c3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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;}}}
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;}}}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions src/LogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/LogpressServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Sdkcodes\Logpress\LogWriter;

class LogApiRequests
class LogRequests
{
/**
* Handle an incoming request.
Expand Down
2 changes: 1 addition & 1 deletion tests/RequestLogging.php → tests/RequestLoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"}');
Expand Down
27 changes: 14 additions & 13 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){

// }
}

0 comments on commit 8eee9c3

Please sign in to comment.