- PHP 8.1
- Composer is required for installation
Run the command below to install via Composer
composer require atimrots/audit-logAfter installing the library, you need to register the Provider class in your Laravel application. Open the config/app.php file and add the following line to the providers array
Atimrots\AuditLog\Providers\AuditLogProvider::classYou need to provide some environment variables for the libraryto be able to connect with API. You can add these variables to your .env file.
AUDIT_LOG_API_URL=
AUDIT_LOG_API_USERNAME=
AUDIT_LOG_API_PASSWORD=
AUDIT_LOG_API_SESSION_TIME=3600
*AUDIT_LOG_API_SESSION_TIME, This value means how long (time in seconds) the access token will be kept in the Laravel cache. By default, the token will be valid as long as the configured MongoLogsApi. A new token will be automatically requested when the beam time is up.
Here ir some examples how you can use this library.
You can post log from any place in code using class Atimrots\AuditLog\AuditLog. You need to call the method post, providing two parameters string $collection and array $data. Collection is one of predefined logs collections and data is body of your specific log.
Note that each collection has a predefined schema and the submitted data will be validated against it.
use Atimrots\AuditLog\AuditLog;
AuditLog::post('price_changes', [
'project' => 'ExampleProject',
'product_uuid' => '709cfcfb-3553-43ed-81d1-e014955dda10',
'old' => 10.45,
'new' => 83.12,
'price_type' => 'cost_price',
'time' => Carbon::now()->format('Y-m-d\TH:i:sO')
]);To retrive your posted logs You need to call method get on the same class.
Provide fallowing parameters:
string $collectionsame as posting;array $query, where you can setpage&limitattributes to limit selected result. If you leave it empty, by default, value for page will be 1 & limit 2000.array $bodyis request body and contains query parametrs to execute find operation. If you leave it in mongo query it is equal todb.collection.find({})and will return list of latest stored logs.
This example will return first 50 results from "price_changes" collection where product_uuid is '435089ef-297a-45ee-8327-81ef319116d1' and log creation time is greater than 2023-04-10T15:53:00+04:00.
$q = [
'product_uuid' => '435089ef-297a-45ee-8327-81ef319116d1',
'time' => ['$gt' => '2023-04-10T15:53:00+04:00'],
];
$response = AuditLog::get('price_changes', ['page' => 1, 'limit' => 50], $q);Returned result of executed example request:
[
'exception' => null,
'response' => [
'result' => [
[
'_id' => [
'$oid':'643effc4387f8bf196a1f6d1'
],
'project' => 'ExampleProject',
'product_uuid' => '435089ef-297a-45ee-8327-81ef319116d1',
'price_type' => 'cost_price',
'old' => 68.4,
'new' => 100.45,
'time' => '2023-04-11T15:53:00+05:00'
],
...
],
'page' => 1,
'limit' => 50,
'total' => 4
],
'original_exception' => null
]