Comments (3)
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.
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.
@zmorris Good write up
from laravel-logger.
Related Issues (20)
- Laravel 9.x support - illuminate/view HOT 4
- Log activity url not working
- Could be a translator error? HOT 2
- URL is not working Properly
- Loggin IP Adresses HOT 1
- Call to undefined function jeremykenedy\LaravelLogger\config_path() HOT 1
- URL is not working Properly
- Class "Request" not found
- Attempt to read property \"id\" on null src\\App\\Http\\Traits\\ActivityLogger.php:29
- Non-static method Request::is() cannot be called statically HOT 1
- Laravel 9 default lang directory has moved HOT 1
- My activity log dashboard spammed by Crawler HOT 3
- View [vendor.pagination.bootstrap-4] not found HOT 2
- laravelcollective/html is abandoned, laravellux/html looks like a replacement candidate HOT 7
- [FR] Add support for indexed resource IDs and models (diff included) HOT 2
- Help to Disable GeoPlugin HOT 8
- Multi Models of User Support HOT 1
- Non-static method jeremykenedy\LaravelLogger\App\Http\Traits\ActivityLogger::activity() cannot be called statically HOT 1
- Non-static method ActivityLogger::activity() cannot be called statically after PR #166 HOT 2
- Undefined variable $slot On new laravel installation. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-logger.