<commit_before>entity-logger
=============

*By <NAME>*

This bundle allows you to log every modification made to your entities, just by implementing the ``EntityLoggerInterface`` interface.

## Installation

Using Composer:

    php composer.phar require meyfarth/entity-logger dev-master

Enable the bundle in your kernel :

```php
// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new \Meyfarth\EntityLoggerBundle\MeyfarthEntityLoggerBundle(),
        );
        
        ...
        
        return $bundles;
    }
```

Update your database :

    php app/console doctrine:schema:update --force
    
This will create a ``meyfarth_entity_log`` table. You can dump your database first using `--dump-sql` option if you want to know the table structure.

## Usage

To log an entity, implement the ``EntityLoggerInterface`` interface in the entities you want to log :
```php
<?php

namespace MyApp\MyBundle\Entity;

use Meyfarth\EntityLoggerBundle\Entity\EntityLoggerInterface;

/**
 * MyEntity
 */
class MyEntity implements EntityLoggerInterface
{
  // Your code
}
```

## Configuration
```yml
# app/config/config.yml
meyfarth_entity_logger:
    enable: true # default false. If not enabled, nothing will be logged
    log:
        create: true  # log on new entities
        update: true  # log on updates, new entities will not be considered as updates
        delete: true  # log on deletion
    user_class: false # log the current user (see below for explainations)
```


If you use the security component with your own user entity (managed by Doctrine in your database), you can automatically log the current user by specifying the user_class parameter:

```yml
# app/config/config.yml
meyfarth_entity_logger:
    user_class: MyApp\MyBundle\Entity\MyUser
```

## Interfaces

For now, there is no interface allowing you to list or search among the logs. It's scheduled for a future version.<commit_msg>Adding doc for the log interface<commit_after>entity-logger
=============

*By <NAME>*

This bundle allows you to log every modification made to your entities, just by implementing the ``EntityLoggerInterface`` interface.

## Installation

Using Composer:

    php composer.phar require meyfarth/entity-logger dev-master

Enable the bundle in your kernel :

```php
// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new \Meyfarth\EntityLoggerBundle\MeyfarthEntityLoggerBundle(),
        );
        
        ...
        
        return $bundles;
    }
```

Update your database :

    php app/console doctrine:schema:update --force
    
This will create a ``meyfarth_entity_log`` table. You can dump your database first using `--dump-sql` option if you want to know the table structure.

## Usage

To log an entity, implement the ``EntityLoggerInterface`` interface in the entities you want to log :
```php
<?php

namespace MyApp\MyBundle\Entity;

use Meyfarth\EntityLoggerBundle\Entity\EntityLoggerInterface;

/**
 * MyEntity
 */
class MyEntity implements EntityLoggerInterface
{
  // Your code
}
```

## Configuration
```yml
# app/config/config.yml
meyfarth_entity_logger:
    enable: true # default false. If not enabled, nothing will be logged
    log:
        create: true  # log on new entities
        update: true  # log on updates, new entities will not be considered as updates
        delete: true  # log on deletion
    user_class: false # log the current user (see below for explainations)
    nb_logs_by_page: 50 # number of logs by page on listing page, default is 50
```


If you use the security component with your own user entity (managed by Doctrine in your database), you can automatically log the current user by specifying the user_class parameter:

```yml
# app/config/config.yml
meyfarth_entity_logger:
    user_class: MyApp\MyBundle\Entity\MyUser
```

## Interfaces

Upcoming : interface to compare the entity between 2 logs.

You can now list all the logs. Simply add to your routing :
```yml
# app/config.routing.yml
#...
meyfarth_entity_log:
    resource: "@MeyfarthEntityLoggerBundle/Resources/config/routing.yml"
    prefix:   /entity-log

```

You can now access the log list by going to ``yourserver/entity-log/list/{page}`` and see the ugliest table you'll ever see. Note that the ``{page}`` token is by default 1.

To override the default template, add your own template named ``list.html.twig`` in ``app/Resouces/MeyfarthEntityLoggerBundle/views/Log/list.html.twig``. 

The controller pass those parameters to the view :
```yml
'logs' # the database result of the current page.
'page' # the current page
'nbByPage' # the number of elements by page
'nbPages' # the total number of pages
```

To access the EntityLog data in twig :
```twig
log.id      {# the EntityLog id #}
log.dateLog {# DateTime of the log #}
log.typeLog {# type of log. Possibles values are defined in EntityLoggerService #}
log.data    {# the data : an array of array #}
```

Note : to compare the values in ``log.typeLog``, you can use the following class constants :
* ``Meyfarth\EntityLoggerBundle\Service\EntityLoggerService::TYPE_INSERT`` 
* ``Meyfarth\EntityLoggerBundle\Service\EntityLoggerService::TYPE_UPDATE`` 
* ``Meyfarth\EntityLoggerBundle\Service\EntityLoggerService::TYPE_DELETE`` 

The ``log.data`` is defined as following :
```php

/* Note that only the fields modified will be in the data. 
It means that if you only modify the "string" field of your entity, only the "string" field 
will be present in the log.data
*/
array(
    '[FIELDNAME]' => array(
        0 => previousData,
        1 => currentData,
    ),
    '[FIELDNAME2]' => array(
        0 => previousData,
        1 => currentData,
    ),
);

```
{'fraction_non_alphanumeric': 0.09572649572649573, 'fraction_numerical': 0.0018803418803418803, 'mean_word_length': 3.4596036585365852, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '696323', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1644, 'n_words': 634}