Developed with love by KnpLabs Hire us for your project!
55

EasyAuditBundle

by xiidea

A Symfony2/Symfony3/Symfony4 Bundle To Log Selective Events

Easy Audit

Build Status
HHVM Status
Coverage Status
Scrutinizer Code Quality
Latest Stable Version
Total Downloads
SensioLabsInsight
knpbundles.com

A Symfony2 Bundle To Log Selective Events. It is easy to configure and easy to customize for your need.

Note: If you are using Symfony version older then 2.4 you need to use EasyAuditBundle 1.2.x

Note: If you are using Symfony version older then 2.6 you need to use EasyAuditBundle 1.3.x

Install

  1. Add EasyAuditBundle in your composer.json
  2. Enable the Bundle
  3. Create audit_log entity class
  4. Configure config.yml
  5. Update Database Schema

1. Add EasyAuditBundle in your composer.json

Add EasyAuditBundle in your composer.json:

{
    "require": {
        "xiidea/easy-audit": "^1.4"
    }
}

Now tell composer to download the bundle by running the command:

$ php composer.phar update xiidea/easy-audit

Composer will install the bundle to your project's vendor/xiidea directory.

2. Enable the Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Xiidea\EasyAuditBundle\XiideaEasyAuditBundle(),
    );
}

3. Create audit_log entity class

The XiideaEasyAuditBundle supports Doctrine ORM by default. However, you must provide a concrete AuditLog class. Follow the instructions to set up the class:

4. Configure config.yml

You can find sample config data in Resources/config/config-sample.yml file

# app/config/config.yml
xiidea_easy_audit:
    #resolver: xiidea.easy_audit.default_event_resolver                           #Optional
    #entity_class : MyProject\Bundle\MyBundle\Entity\AuditLog                     #Required
    #entity_event_resolver : xiidea.easy_audit.default_entity_event_resolver      #Optional
    #default_logger : true                                                        #Optional

    #user property to use as actor of an event
    #valid value will be any valid property of your user class
    user_property : ~ # or username                            #Optional

    #List of doctrine entity:event you wish to track or set to false to disable logs for doctrine events
    # valid events are = [created, updated, deleted]
    #doctrine_entities :                                              #Optional
    #     MyProject\Bundle\MyBundle\Entity\MyEntity : [created, updated, deleted]
    #     MyProject\Bundle\MyBundle\Entity\MyEntity2 : []

    #List all events you want to track  (Optional from v1.2.1 you can now use subscriber to define it)
    events :                                                   #Optional
        - security.interactive_login

    #List all custom resolver for event
    #custom_resolvers :
    #       security.interactive_login : user.event_resolver
    #       security.authentication.failure : user.event_resolver

    #logger_channel:
    #    xiidea.easy_audit.logger.service: ["info", "debug"]
    #    file.logger: ["!info", "!debug"]

    #Custom Event Resolver Service
services:
    #user.event_resolver:
    #     class: Xiidea\EasyAuditBundle\Resolver\UserEventResolver
    #     calls:
    #        - [ setContainer,[ @service_container ] ]

5. Update Database Schema

As all setup done, now you need to update your database schema. To do so,run the following command from your project directory
bash
$ php app/console doctrine:schema:update --force

Core Concepts

Logger:

Logger is the core service which are responsible for persist the event info. You can define as many logger as you like.
EasyAudit Bundled with a logger service xiidea.easy_audit.logger.service which is the default logger service. You can easily
disable the service by setting default_logger: false in configuration.

Resolver:

Resolver is like translator for an event. It used to translate an event to AuditLog entity. EasyAudit bundled with two(2)
resolver services xiidea.easy_audit.default_event_resolver, xiidea.easy_audit.default_entity_event_resolver. And a
custom EventResolver class UserEventResolver to illustrate how the transformation works. You can define as many resolver
service as you want and use them to handle different event. Here is the place you can set the severity level for a event. Default
level is Psr\Log\LogLevel::INFO. Custom severity levels are not available. EasyAudit supports the logging levels
described by PSR-3. These values are present for basic filtering purposes. You can
use this value as channel to register different logger to handle different event. If you add any other field to your
AuditLog object, this is the place to add those extra information (tags, metadata, etc..)

Channel

It is now possible to register logger for specific channel. channel is refers to log level. you can configure EasyAudit logger
services to handle only specific level of event.

Warning - BC Breaking Changes

  • Since v1.2.2 pre_persist_listener option has been removed. You can use this cookbook to achieve the same functionality
  • Since v1.2.2 EventResolverInterface been split into EmbeddedEventResolverInterface and EventResolverInterface
  • Since v1.3.x The new Event object has been adapted. And the signature of EmbeddedEventResolverInterface and EventResolverInterface also changed. Now it expects extra $eventName parameter

Cookbook

Look the cookbook for another interesting things.

The MIT License (MIT)

Copyright (c) 2013 Xiidea <http://www.xiidea.net/>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
xiidea_easy_audit:
user_property: ~ # Required
entity_class: ~ # Required
resolver: xiidea.easy_audit.default_event_resolver
entity_event_resolver: xiidea.easy_audit.default_entity_event_resolver
default_logger: true
doctrine_entities: Array
events: Array
custom_resolvers: Array
logger_channel:

# Prototype
name:
type: ~
elements: []
  • Fix #17 Symfony 4 incompatibility
    By ronisaha, 1 year ago
  • Merge pull request #18 from DamienHarper/master [skip ci]
    By web-flow, 1 year ago
  • Doc fixes relatives to how to track all events for an entity. (#16)
    By DamienHarper, 1 year ago
  • Make Symfony4 Compatible
    By ronisaha, 1 year ago
  • Note added for prePersist listener [skip ci]
    By web-flow, 1 year ago
  • Fix Typo
    By ronisaha, 1 year ago
  • Add more code coverage
    By ronisaha, 1 year ago
  • Fix Unit test
    By ronisaha, 1 year ago
  • Implement MonnologLogger
    By ronisaha, 1 year ago
  • Make service public, as symfony autowire removing if service not public and never accessed
    By ronisaha, 1 year ago
  • Add Symfony4 to composer dependencies
    By ronisaha, 1 year ago
  • Make BaseAuditLog class concrete rather abstract, that will allow use it without doctrine entity
    By ronisaha, 1 year ago
  • Refactor fromArray method to use property accessor
    By ronisaha, 1 year ago
  • Composer update dependencies [ci skip]
    By ronisaha, 1 year ago
  • Dropping Support for Symfony 3.1 PHP 5.5 As as these versions is not maintained anymore
    By ronisaha, 1 year ago
  • update deprecated code style
    By ronisaha, 1 year ago
  • Dropping Support for Symfony 3.0 As as these versions is not maintained anymore
    By ronisaha, 2 years ago
  • Fix test failure for FOS UserBundle Update
    By ronisaha, 2 years ago
  • Merge pull request #13 from afurculita/patch-1 [ci skip]
    By web-flow, 2 years ago
  • Rename logger-chanel.md to logger-channel.md
    By web-flow, 2 years ago
  • fix #12 Add missing argument in doctrine service configuration
    By ronisaha, 2 years ago
  • Remove assets option
    By ronisaha, 2 years ago
  • Add impersonatingUser field in sample audit log entity
    By ronisaha, 2 years ago
  • Add functional test for Impersonating user
    By ronisaha, 2 years ago
  • Add impersonating user info
    By ronisaha, 2 years ago
  • Refactor to getting property name and increase code quality check
    By ronisaha, 2 years ago
  • fix #11 exclude Symfony version 3.0.4 in dependency
    By ronisaha, 2 years ago
  • Add a simple line feed as the last character
    By ronisaha, 2 years ago
  • Add PHP7 for ci
    By ronisaha, 3 years ago
  • Fix abstract declaration
    By ronisaha, 3 years ago