An open API service indexing awesome lists of open source software.

https://github.com/utopia-php/audit

Lite & fast micro PHP audit log library that is **easy to use**.
https://github.com/utopia-php/audit

hacktoberfest php

Last synced: 2 months ago
JSON representation

Lite & fast micro PHP audit log library that is **easy to use**.

Awesome Lists containing this project

README

        

# Utopia Audit

[![Build Status](https://travis-ci.org/utopia-php/audit.svg?branch=master)](https://travis-ci.com/utopia-php/audit)
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/audit.svg)
[![Discord](https://img.shields.io/discord/564160730845151244)](https://appwrite.io/discord)

Utopia framework audit library is simple and lite library for managing application user logs. This library is aiming to be as simple and easy to learn and use. This library is maintained by the [Appwrite team](https://appwrite.io).

Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free, and can be used as standalone with any other PHP project or framework.

## Getting Started

Install using composer:
```bash
composer require utopia-php/audit
```

Init the audit object:

```php
3, // Seconds
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_STRINGIFY_FETCHES => true,
]);

$cache = new Cache(new NoCache());

$database = new Database(new MySQL($pdo),$cache);
$database->setNamespace('namespace');

$audit = new Audit($database);
$audit->setup();
```

**Create Log**

A simple example for logging a user action in the audit DB.

```php
$userId = 'user-unique-id';
$event = 'deleted'; // Log specific action name
$resource = 'database/document-1'; // Resource unique ID (great for filtering specific logs)
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'; // Set user-agent
$ip = '127.0.0.1'; // User IP
$location = 'US'; // Country name or code
$data = ['key1' => 'value1','key2' => 'value2']; // Any key-value pair you need to log

$audit->log($userId, $event, $resource, $userAgent, $ip, $location, $data);
```

**Get Logs By User**

Fetch all logs by given user ID

```php
$logs = $audit->getLogsByUser(
'userId' // User unique ID
); // Returns an array of all logs for specific user
```

**Get Logs By User and Action**

Fetch all logs by given user ID and a specific event name

```php
$logs = $audit->getLogsByUserAndEvents(
'userId', // User unique ID
['update', 'delete'] // List of selected event to fetch
); // Returns an array of all logs for specific user filtered by given actions
```

**Get Logs By Resource**

Fetch all logs by a given resource name

```php
$logs = $audit->getLogsByResource(
'resource-name', // Resource Name
); // Returns an array of all logs for the specific resource
```

## System Requirements

Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

## Copyright and license

The MIT License (MIT) [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)