Giter Site home page Giter Site logo

Comments (3)

zmorris avatar zmorris commented on September 28, 2024 1

Since this is literally the very first thing that people will try to do the first time they use Laravel Logger during development, I thought it would be useful to post an unopinionated solution that "just works" so that people can get real work done in a pinch.

The CreateLaravelLoggerActivityTable migration creates LONGTEXT fields (that can store up to 4 GB of data) for description, route and referrer. But we usually don't care about the referrer while working locally, so the following code stores the request body in the request referrer field before it's ever processed by the LogActivity class, so that we can examine form submits and other requests. It intentionally avoids the friction of having to create a new middleware file, so be sure not to commit it or push it to production:


Laravel:

Put this class declaration somewhere in app/Http/Kernel.php (or wherever you are using the LogActivity class). It can go at the bottom, so as to preserve the PSR-4 autoloading of class Kernel extends HttpKernel as the first class declaration:

class LogActivityWithBody extends \jeremykenedy\LaravelLogger\App\Http\Middleware\LogActivity {
    public function handle($request, \Closure $next, $description = null)
    {
        $request->headers->set('referer', print_r($request->all(), true));
        //\Request::instance()->headers->set('referer', print_r(\Request::instance()->all(), true));
        //throw new \Exception('LogActivity::handle was called');

        return parent::handle($request, $next, $description);
    }
};

Then replace this line:

protected $middleware = [\jeremykenedy\LaravelLogger\App\Http\Middleware\LogActivity::class, ];

With:

protected $middleware = [LogActivityWithBody::class, ];

The comments just show how to examine the global Request when you don't have a $request instance available, and how to use an exception to verify that your middleware is being called.


Lumen (untested but should work):

Replace this line in bootstrap/app.php:

$app->routeMiddleware(['activity' => \jeremykenedy\LaravelLogger\App\Http\Middleware\LogActivity::class, ]);

With:

$app->routeMiddleware(['activity' => get_class(new class extends \jeremykenedy\LaravelLogger\App\Http\Middleware\LogActivity {
    public function handle($request, \Closure $next, $description = null)
    {
        $request->headers->set('referer', print_r($request->all(), true));

        return parent::handle($request, $next, $description);
    }
}), ]);

If it complains that it can't find the class (because it was declared locally), you may need to store the anonymous class in a global, which can also be done inside a function if you need to:

global $class;

$class = new class extends \jeremykenedy\LaravelLogger\App\Http\Middleware\LogActivity {
    public function handle($request, \Closure $next, $description = null)
    {
        $request->headers->set('referer', print_r($request->all(), true));

        return parent::handle($request, $next, $description);
    }
};

$app->routeMiddleware(['activity' => get_class($class), ]);

You can also just create the class somewhere in the file like in the Laravel example.


More ways of getting the request body can be found at:

https://stackoverflow.com/questions/33005815/laravel-5-retrieve-json-array-from-request

$request->all()
$request->json()
$request->getContent()

...

\Request::instance()->all()
\Request::instance()->json()
\Request::instance()->getContent()

...

file_get_contents('php://input')
$request->input('key')
Input::get('key')

There are eleventeen different ways of getting the request body as a string, object or array for json, form-encoded key-value pairs and multipart form encoded sections, so YMMV.

If you're going to use this for production, you'll need to create a new middleware class file and most likely extend the storeActivity() method to include an additional field for the request body. You'll also need to add a migration to add a body column to the laravel_logger_activity table, which is beyond the scope of this comment. Libraries and frameworks should generally include a userdata or reserved field for exactly this purpose.

Hopefully someone can verify if the one-liner works in Lumen so that I can update this comment at some point.

from laravel-logger.

jeremykenedy avatar jeremykenedy commented on September 28, 2024

It processes specific items and doesn't blindly record the data, there may be privacy and sensitive data issues on some cases if it did that.

from laravel-logger.

jeremykenedy avatar jeremykenedy commented on September 28, 2024

@zmorris Good write up

from laravel-logger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.