GithubHelp home page GithubHelp logo

vendz / audit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from utopia-php/audit

0.0 0.0 0.0 156 KB

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

License: MIT License

PHP 100.00%

audit's Introduction

Utopia Audit

Build Status Total Downloads 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.

Although this library is part of the Utopia Framework project it is dependency free, and can be used as standalone with any other PHP project or framework.

Getting Started

Install using composer:

composer require utopia-php/audit

Init the audit object:

<?php

require_once __DIR__ . '/../../vendor/autoload.php';

use PDO;
use PDO;
use Utopia\Audit\Audit;
use Utopia\Cache\Cache;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;


$dbHost = '127.0.0.1';
$dbUser = 'travis';
$dbPass = '';

$pdo = new PDO("mysql:host={$dbHost}", $dbUser, $dbPass, array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    PDO::ATTR_TIMEOUT => 5 // Seconds
));

// Connection settings
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);   // Return arrays
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);        // Handle all errors with exceptions 

$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.

$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

$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 action name

$logs = $audit->getLogsByUserAndActions(
    'userId', // User unique ID
    ['update', 'delete'] // List of selected action 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

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

System Requirements

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

Authors

Eldad Fux

Copyright and license

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

audit's People

Contributors

lohanidamodar avatar eldadfux avatar christyjacob4 avatar torstendittmann avatar vendz avatar kodumbeats avatar

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.