Loggr
Provides an interface for storing and retrieving database stored system log messages., (*1)
Installation
Just place require new package for your laravel installation via composer.json, (*2)
"c4studio/loggr": "1.0.*"
Then simply composer update
, (*3)
Registering to use it with laravel
Add following lines to app/config/app.php
, (*4)
ServiceProvider array, (*5)
C4studio\Loggr\LoggrServiceProvider::class,
Alias array, (*6)
'Loggr' => C4studio\Loggr\Facades\Loggr::class,
Publishing migrations (Laravel 5.2 and lower only)
php artisan vendor:publish --provider="C4studio\Loggr\LoggrServiceProvider" --tag=migrations
Running migrations
Loggr uses a database table for storage, so you'll need to run the migrations, (*7)
php artisan migrate
Usage
Add log message using facade
Loggr::add('Message');
You can specify the owner using the second parameter which accepts either a User model or a user ID, (*8)
Loggr::add('Message', Auth::user());
You can also add any additional data as a 3rd parameter, which will be stored in a binary type column, (*9)
Loggr::add('Message', Auth::user(), serialize($myArray));
Add log message using helper function
loggr('Message');
As in the case of the facade, you can specify the owner using the second parameter, (*10)
loggr('Message', Auth::user());
Retrieving log messages
To get all log messages use, (*11)
Loggr::get();
You can also easily get log messages belonging to a user, (*12)
Loggr::owner(Auth::user());
or between two dates, (*13)
Loggr::interval(\Carbon\Carbon::yesterday(), \Carbon\Carbon::today());
If you want to only set starting or end date, just leave off parameter or pass in null, (*14)
Loggr::interval(\Carbon\Carbon::yesterday());
Loggr::interval(null, \Carbon\Carbon::yesterday());
For more complex queries, you can return a Builder object by using query(). Easy, right?, (*15)
Loggr::query()->orderBy('timestamp', 'desc')->take(2)->get();