Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdheiden committed Oct 28, 2024
0 parents commit 65e7614
Show file tree
Hide file tree
Showing 40 changed files with 1,967 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Bug report
about: Create a report to help us improve the package
title: ""
labels: bug
assignees: dvdheiden
---

## Describe the bug

A clear and concise description of what the bug is.

## Reproduction

Steps to reproduce the behavior.

## Expected behavior

A clear and concise description of what you expected to happen.

## Actual behavior

Describe the behavior as it is right now.

## Additional information

Anything else you want to provide.
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
name: Feature request
about: Suggest an idea for this package
title: ""
labels: feature
assignees: dvdheiden
---

## Goal

A clear and concise description of which "problem" you want to get solved. Ex. I think this could be easier when...

## Additional information

Add any other context or screenshots about the feature request here.
3 changes: 3 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changes

Provide a summary of your changes.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: RequestResponseLog

on: [ push ]

jobs:
tests:
runs-on: ubuntu-latest

name: Tests

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, intl

- name: Install composer dependencies
run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress

- name: Execute static analysis
run: composer analyse

- name: Execute tests via PHPUnit
run: composer test:no-coverage

- name: Check code style
run: composer code-style:check
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/vendor
composer.lock
/build
.phpunit.result.cache
.idea
.phpunit.cache
.DS_Store
.phpactor.json
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dick van der Heiden

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.
196 changes: 196 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# RequestResponseLog

This package for Laravel applications provides a middleware for logging HTTP requests and responses, both incoming to,
or outgoing from your Laravel application. It offers:
- Middleware to be used with the Laravel HTTP client, Guzzle and Saloon.
- Middleware to log requests made to your application and its response.
- Functionality to manual logs requests and responses.

It's originally created to log API requests to other services and to log incoming webhooks, but can be used for any
request and response logging.

## Requirements

This package requires Laravel 10+ and PHP 8.3+.

## Installation

You can install the package via composer:

```bash
composer require goedemiddag/request-response-log
```

The package will automatically register itself in Laravel, but you need to run the migrations:

```bash
php artisan migrate
```

You are now ready to use the package.

## Usage

This package provides two middleware solutions:
- Logging HTTP requests and responses, to be used with the Laravel HTTP client, Guzzle and Saloon.
- Logging Requests and Responses from your application.

### HTTP Logger

#### Laravel HTTP client

When using the Laravel HTTP client:

```php
use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use Illuminate\Support\Facades\Http;

Http::withMiddleware(RequestResponseLogger::middleware('vendor'))
```

#### Guzzle

When initializing the client:

```php
use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(RequestResponseLogger::middleware('vendor'));

$client = new Client(['handler' => $stack]);
```

#### Saloon

In the constructor of the connector:

```php
use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use Saloon\Http\Connector;

class YourConnector extends Connector
{
public function __construct()
{
$this
->sender()
->addMiddleware(RequestResponseLogger::middleware('vendor'));
}
}
```

### Application logger

The package provides a middleware for your Laravel application called `ApplicationRequestResponseLogger`. This will log
the incoming request and the response your application generates. This is originally use for logging incoming webhooks,
but feel free to use it for anything you like. Just register the middleware to a group or apply it to a route
individually:

```php
Route::post('/webhook')
->uses([WebhookController::class, 'handle'])
->middleware([ApplicationRequestResponseLogger::class]);
```

### Manual logger

The package provides a logger for manual logging of requests and responses. This can be useful when you want to log
requests and responses that do not support the middleware or any other use case you can come up with.

```php
use Goedemiddag\RequestResponseLog\Enums\RequestFlow;
use Goedemiddag\RequestResponseLog\ManualRequestResponseLogger;

$requestLog = ManualRequestResponseLogger::fromRequest(
vendor: 'vendor',
request: $request,
flow: RequestFlow::Incoming,
);

// TODO your code here

ManualRequestResponseLogger::fromResponse(
requestLog: $requestLog,
response: $response,
);
```

## Configuration

The package provides a configuration file that allows you to configure the package to your needs. You can change the
table names to your likings, change the database connection (so you can store the logs somewhere else than your default
database), determine which fields should be masked (even per vendor), etc. The configuration file can be published by
running:

```bash
php artisan vendor:publish --provider="Goedemiddag\RequestResponseLog\RequestResponseLogServiceProvider" --tag="config"
```

The configuration file will be published to `config/request-response-log.php`. The configuration file contains comments
to explain more about the options.

The migrations are loaded automatically and can't be published, as everything you can configure about it, is in the
configuration.

## Cleaning up

This package uses the default model prune functionality from Laravel to clean up old logs. You can configure the amount
of days to keep logs in the configuration file. To clean up the logs automatically, schedule the model prune command
and pass the model you want to prune:

### Laravel 11

Add the following to your `routes/console.php` file:

```php
use Goedemiddag\RequestResponseLog\Models\RequestLog;
use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', ['--model' => [RequestLog::class]])->daily();
```

## Laravel 10

Add the following to your `app/Console/Kernel.php` file:

```php
use Goedemiddag\RequestResponseLog\Models\RequestLog;

$schedule
->command('model:prune', ['--model' => [RequestLog::class]])
->daily();
```

## Contributing

Found a bug or want to add a new feature? Great! There are also many other ways to make meaningful contributions such
as reviewing outstanding pull requests and writing documentation. Even opening an issue for a bug you found is
appreciated.

When you create a pull request, make sure it is tested, following the code standard (run `composer code-style:fix` to
take care of that for you) and please create one pull request per feature. In exchange, you will be credited as
contributor.

### Testing

To run the tests, you can use the following command:

```bash
composer test
```

### Security

If you discover any security related issues in this or other packages of Goedemiddag, please email [email protected]
instead of using the issue tracker.

# About Goedemiddag

[Goedemiddag!](https://www.goedemiddag.nl) is a digital web-agency based in Delft, the Netherlands. We are a team of
professionals who are passionate about the craft of building digital solutions that make a difference for its users.
See our [GitHub organisation](https://github.com/goedemiddag) for more package.
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "goedemiddag/request-response-log",
"description": "Package for logging (external) requests and the responses",
"homepage": "https://github.com/goedemiddag/request-response-log",
"license": "MIT",
"authors": [
{
"name": "Dick van der Heiden",
"email": "[email protected]"
}
],
"require": {
"php": "^8.3",
"laravel/framework": "^10.0|^11.0"
},
"require-dev": {
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.5",
"phpunit/phpunit": "^11.4",
"roave/security-advisories": "dev-latest",
"symplify/easy-coding-standard": "^12.3"
},
"autoload": {
"psr-4": {
"Goedemiddag\\RequestResponseLog\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Goedemiddag\\RequestResponseLog\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"code-style:check": "vendor/bin/ecs",
"code-style:fix": "vendor/bin/ecs --fix",
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/phpunit",
"test:no-coverage": "vendor/bin/phpunit --no-coverage"
},
"extra": {
"laravel": {
"providers": [
"Goedemiddag\\RequestResponseLog\\RequestResponseLogServiceProvider"
]
}
}
}
Loading

0 comments on commit 65e7614

Please sign in to comment.