Log Laravel application request and response with a unique ID for easy debugging

Overview

Flexible and extendable logging of Laravel application request and responses

bilfeldt/laravel-request-logger

Latest Version on Packagist GitHub Tests Action Status StyleCI Code Style Status Total Downloads

Zero configuration logging of Requests and Responses to database or custom drivers in Laravel applications - no more issues debugging customer support requests.

Installation

You can install the package via composer:

composer require bilfeldt/laravel-request-logger

Publish and run the migrations with:

php artisan vendor:publish --provider="Bilfeldt\RequestLogger\RequestLoggerServiceProvider" --tag="request-logger-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Bilfeldt\RequestLogger\RequestLoggerServiceProvider" --tag="request-logger-config"

Usage

It is possible to enable logging of all or some requests conditionally using one of the approaches below.

Enable log via middleware (Recommended)

This package comes with a convenient requestlog middleware that can be used to enable logging of request by simply registering the middleware on the routes (or route groups) you wish to log:

Route::middleware('requestlog')->get('/', function () {
    return 'Hello World';
});

Enable via config file

The config file includes some convenient settings for enabling logging of all or some requests:

// Enable all requests:
'log_methods' => ['*'],

// or enable all server errors
'log_statuses' => ['5**'],

Enable log via request

This package adds a macro on the Illuminate\Http\Request class making it possible to enable logging directly from the request:

/**
 * Index posts.
 *
 * @param  Request  $request
 * @return Response
 */
public function index(Request $request)
{
    $request->enableLog();

    //
}

Unique Request UUID

This package adds a macro getUniqueId() for the Illuminate\Http\Request class which generates a unique request UUID that will be saved to the logs and that can be included as Global Log Context which will pass it onto any application logging or error reporting. This id will per default also be added as a custom response header.

This is an extremely helpful trick when debugging customer requests as both customer, application logs, reported errors and request logs (this package) now all include a single common UUID!

$request->getUniqueId(); // Example: 94d0e2d6-4cc6-449c-9140-80bca47d29b4

Extending with custom Drivers

This package implements the Laravel Manager Class making it possible to easily register custom drivers either in your application or by third party packages.

An example driver can be specified as middleware parameters:

Route::middleware('log:example')->get('/', function () {
    return 'Hello World';
});

or via request macro:

$request->enableLog('example');

Pruning

The number of logged requests can quickly grow if you are not pruning the logs regularly. In order to keep the logs manageable, you can use the prune command to remove old logs as described in the Laravel Docs:

$schedule->command('requestlog:prune')->daily();

Note that the default RequestLog model setup by this package will not be discovered as a "Prunable" model by Laravel default model:prune command as it does not reside in the app/Models directory. If you change this class in the configuration to a custom class this will be auto registered and the command above will be needless.

Notes and advises

Proxies and load balancers

When using proxies and/or load balancers then the IP address of the proxy/load balancer must be listed as a Trusted Proxy for the users IP address to be correctly logged as the IP of the proxy itself will be logged otherwise.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • add ability to have more headers

    add ability to have more headers

    Header values can be closures. implement an App-Version sample header

    Description

    This comes from an idea to add a hostname into the header to monitor the responding server in case of load balancing. I restructured the configuration array a bit, so now it is possible to have as many headers as you want and you can define the header value as a closure if you want to.

    Type of change

    Please delete options that are not relevant.

    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [ ] New feature (non-breaking change which adds functionality)
    • [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
    • [x] This change requires a documentation update

    Checklist

    • [ ] I have made corresponding changes to the documentation
    • [x] My changes generate no new warnings
    • [x] I have added tests that prove my fix is effective or that my feature works
    • [x] New and existing unit tests pass locally with my changes

    greetings, J

    opened by comes 4
  • Server Error bug when path long

    Server Error bug when path long

    This fixes the SQL problem SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'path' when user attempt to access very long URL path or accessed by some vulnerability scanner (e.g. Qualys PCI DSS scanner).

    Steps to reproduce:

    1. Go to your app URL.
    2. Enter very long path that exceeds 255 characters.
    3. An SQL error will appear on logs and page will return 5xx error instead of 404.

    Example error log (executed by Qualys VAPT) SQL 1406 Error

    opened by woenel 1
  • Fix: Call to a member function getName() on null

    Fix: Call to a member function getName() on null

    This fix the Call to a member function getName() on null error when trying to access the supposedlyNotFoundHttpException route

    Screen Shot 2021-11-15 at 6 08 24 PM

    Steps to reproduce:

    1. tested in api.php route file.
    2. try the endpoint http://{endpoint}/api/supposedly-404-page
    opened by woenel 1
  • Implement proper typecasting for response

    Implement proper typecasting for response

    The response in the RequestHandled event can be of different types, so the DocBlock here is incorrect. Types are

    • Symfony\Component\HttpFoundation\Response
      • \Illuminate\Http\Response
      • \Illuminate\Http\JsonResponse -> Symfony\Component\HttpFoundation\JsonResponse
      • \Illuminate\Http\RedirectResponse -> \Symfony\Component\HttpFoundation\RedirectResponse
      • \Symfony\Component\HttpFoundation\BinaryFileResponse
    opened by bilfeldt 1
  • Add middleware information

    Add middleware information

    Add information about middlewares: https://github.com/laravel/telescope/blob/2a33a309432fc063c65d4f6d8ec9dd75954fdd0a/src/Watchers/RequestWatcher.php#L50

    opened by bilfeldt 0
Releases(v1.1.0)
  • v1.1.0(Jan 30, 2022)

    What's Changed

    • Add Laravel 9 support by @bilfeldt in https://github.com/bilfeldt/laravel-request-logger/pull/19
    • Server Error bug when path long by @woenel in https://github.com/bilfeldt/laravel-request-logger/pull/18

    Full Changelog: https://github.com/bilfeldt/laravel-request-logger/compare/v1.0.2...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Jan 11, 2022)

    What's Changed

    • Exclude 'api_token' from logs per default by @bilfeldt in https://github.com/bilfeldt/laravel-request-logger/pull/17

    Full Changelog: https://github.com/bilfeldt/laravel-request-logger/compare/v1.0.1...v1.0.2

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Nov 15, 2021)

    • Fix issue when route is undefined: By @woenel in https://github.com/bilfeldt/laravel-request-logger/pull/15

    New Contributors

    • @woenel made their first contribution in https://github.com/bilfeldt/laravel-request-logger/pull/15

    Full Changelog: https://github.com/bilfeldt/laravel-request-logger/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Nov 12, 2021)

  • v0.3.0(Nov 9, 2021)

    What's Changed

    • Add logging of middleware by @bilfeldt in https://github.com/bilfeldt/laravel-request-logger/pull/13
    • Rename field 'time' to 'duration' by @bilfeldt in https://github.com/bilfeldt/laravel-request-logger/pull/14

    Full Changelog: https://github.com/bilfeldt/laravel-request-logger/compare/v0.2.0...v0.3.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Nov 8, 2021)

  • v0.1.0(Nov 7, 2021)

Owner
Bilfeldt
Bilfeldt
Small laravel package for viewing api logs which can be used in debugging.

This is a small package that can helps in debugging api logs. It can log request method, url, duration, request payload, which models are retrieved, controller and method.

awt 334 Jan 6, 2023
A simple and beautiful laravel log reader

Laravel Log Reader A simple and beautiful laravel log reader Documentation Get full documentation of Laravel Log Reader Other Packages Laravel H - A h

Md.Harun-Ur-Rashid 356 Dec 30, 2022
Flexible and rock solid audit log tracking for CakePHP 3

AuditStash Plugin For CakePHP This plugin implements an "audit trail" for any of your Table classes in your application, that is, the ability of recor

José Lorenzo Rodríguez 68 Dec 15, 2022
DatabaseLog CakePHP plugin to log into DB instead of files. Better to filter and search.

CakePHP DatabaseLog Plugin DatabaseLog engine for CakePHP applications. This branch is for CakePHP 4.0+. See version map for details. Features Easy se

Mark Sch. 41 Jul 29, 2022
Remove messages from the error log that are from a certain folder

Error-Focus A package to stay focused on relevant entries in your error-log Scope This package allows you to declare folders that are not under your d

Andreas Heigl 7 Jul 25, 2022
An effective,fast,stable log extension for PHP

SeasLog An effective,fast,stable log extension for PHP @author Chitao.Gao [[email protected]] Documentation On php.net 中文文档 Synopsis Why use seaslog What

Neeke Gao 76 Sep 28, 2022
AcLog is a simple, zero-dependency PHP package to log activity to files

AcLog is a simple, zero-dependency PHP package to log activity to files. This is not meant for logging errors, this is can be used for logging c

null 2 Sep 9, 2022
Laravel logger is an activity event logger for your Laravel or Lumen application

Laravel logger is an activity event logger for your Laravel or Lumen application. It comes out the box with ready to use with dashboard to view your activity. Laravel logger can be added as a middleware or called through a trait. Easily have an Activity Log. This package is easily configurable and customizable. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6, and 7+

Jeremy Kenedy 475 Dec 22, 2022
This package is for centralized logging of multiple Laravel application into one connection.

Laralogs This package is for centralized logging of multiple Laravel application into one connection. Installation You can install the package via com

Remon 4 Apr 26, 2022
Capture and monitor detailed error logs with nice dashboard and UI

Capture and monitor detailed error logs with nice dashboard and UI Requirements Check Laravel 6 requirements Check Laravel 7 requirements Installation

Bugphix 107 Dec 12, 2022
Keep your laravel logs small and tidy.

Logs can get quite out of hand. This package helps save server space and keep your Laravel log files small.

Accent Interactive 73 Nov 14, 2022
Sends your logs to files, sockets, inboxes, databases and various web services

Monolog - Logging for PHP Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers be

Jordi Boggiano 20.1k Jan 8, 2023
PHP logging library that is highly extendable and simple to use.

Analog - Minimal PHP logging library Copyright: (c) 2012-Present Johnny Broadway License: MIT A minimal PHP logging package based on the idea of using

Aband*nthecar 331 Dec 21, 2022
Paste, share and analyse Minecraft server logs

mclo.gs Paste, share & analyse your Minecraft server logs About The project mclo.gs was created in 2017 by the Aternos team after more than 4 years of

Aternos 99 Jan 3, 2023
Sends your logs to files, sockets, inboxes, databases and various web services

Monolog - Logging for PHP ⚠ This is the documentation for Monolog 3.x, if you are using older releases see the documentation for Monolog 2.x or Monolo

Jordi Boggiano 20.1k Jan 7, 2023
Robust, composite logger with filtering, formatting, and PSR-3 support

laminas-log ???? Русским гражданам Мы, участники Laminas, родились и живем в разных странах. У многих из нас есть друзья, родственники и коллеги как в

Laminas Project 27 Nov 24, 2022
A simple database logger for all outgoing emails sent by Laravel website.

Laravel Email Database Log A simple database logger for all outgoing emails sent by Laravel website.

Shvets Group 136 Jan 4, 2023
📑 Laravel HTTP Logger

Logging incoming HTTP requests

The Dragon Code 28 Nov 29, 2022
This Package helps you in laravel application to log all desired activity for each request from request entry point to generate response at a single snapshot.

Laravel Scenario Logger This Package helps you in laravel application to log all desired activity for each request from request entry point to generat

Mehrdad Mahdian 6 Sep 27, 2021
A Laravel package to output a specific sql to your favourite debugging tool. The supported log output is Laravel Telescope, Laravel Log, Ray, Clockwork, Laravel Debugbar and your browser.

Laravel showsql A Laravel package to output a specific sql to your favourite debugging tool, your browser or your log file. Use case You often want to

Dieter Coopman 196 Dec 28, 2022