PHP code example of ebola / logging

1. Go to this page and download the library: Download ebola/logging library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ebola / logging example snippets


$logging = new LoggingRender();

{!! $logging->renderUserLogging() !!}

{!! $logging->renderDownloadLogging() !!}

// config/app.php
'providers' => [
    ...
    Ebola\Logging\LoggingServiceProvider::class,
];

// config/app.php
'aliases' => [
    ...
    'LoggingRender' => Ebola\Logging\Renders\LoggingRender::class,
    'LoggingFilters' => Ebola\Logging\Helpers\Filters::class,
    'LoggingProperties' => Ebola\Logging\Helpers\Properties::class,
];

return [

    /*
     * Route prefixes for save transitions on the URL
     * 'web' is all routes without prefix
     */
    'logging_routing_prefixes' => [
        'web',
        // 'admin',
    ],

    /*
     * Save transition if route is ajax query
     */
    'logging_routing_save_ajax' => false,

    /*
     * Users events for save
     */
    'logging_users_events' => [
        'registered',
        'attempting',
        // 'authenticated',
        'login',
        'failed',
        'logout',
        'lockout',
        'password_reset',
    ],

    /*
     * Models events for save
     */
    'logging_models_events' => [
        'created',
        'updated',
        'deleted',
    ],

    /*
     * Number rows for display on one page paginate
     */
    'num_rows_on_page' => 15,

    /*
     * Path to folder where logging files are saving
     */
    'download_path' => '/reports/',

    /*
     * List of logging models fields
     */
    'logging_fields' => [
        'id',
        'log_name',
        'subject_id',
        'subject_type',
        'causer_id',
        'causer_type',
        'description',
        // 'properties',
        'created_at',
        // 'updated_at',
    ],

    /*
     * Path to translated logging fields
     * Need path to fields translates in "dot" notation
     */
    'translation_path' => null, // 'admin.logging.fields' => []

    /*
     * Filters for fields
     * Types of filter and fields for it
     */
    'logging_filters' => [
        'selectable' => [
            'log_name',
            'subject_type',
            'causer_id',
        ],

        'according_to_the_text' => [
            'created_at',
        ],
    ],
];

// App\Providers\EventServiceProvider.php
protected $subscribe = [
    'Ebola\Logging\Listeners\RequestEventLogging',
    'Ebola\Logging\Listeners\UserEventLogging',
    'Ebola\Logging\Listeners\MailEventLogging',
];

// App\Providers\AppServiceProvider.php

use Ebola\Logging\LoggingObserver;

...

public function boot()
    {
        //

        Article::observe(LoggingObserver::class);
    }

    $logging = new LoggingRender();

    // or

    $logging = new LoggingRender(\Auth::user(), ['id', 'log_name', 'description']);

    {!! $logging->renderUserLogging() !!}

    {!! $logging->renderDownloadLogging() !!}

    <link rel="stylesheet" href="{{ asset('vendor/logging/css/styles.css') }}">

    <script src="{{ asset('vendor/logging/js/common.js') }}"></script>

    // User in logging object
    $loggingUser = $this->getUser();

    // Path to download directory from config download_path
    $fileLoggingPath = $this->getFileLoggingPath();

    // Path to activity model from config activitylog.activity_model
    $activityModel = $this->getActivityModel();

    // Path to translation path from config translation_path
    $pathToTranslationFields = $this->getTranslationPath();

    // From config num_rows_on_page
    $countRowOnDisplayFromConfig = $this->logging->getRowCount();

    // Fields from object. Default from config logging_fields
    $fieldsForDisplay = $this->logging->getFields();

    // Translates from config translation_path. Default from vendor
    $translatedFieldsForDisplay = $this->logging->getTranslatedFields();

    // Returns the constructed query
    // Fields must exist in the log table and be broken according to the type of filtering in the config logging_filters
    // Default filters are empty array
    $rows = $this->logging->getRows($filters);

    // Creates a file and displays a custom save window
    $rows = $this->logging->getRows()->get()
    $this->logging->getLoggingFile($rows);

    // Work with json properties field from Activity model
    class Properties
    {
        public static function getProperties($activity, $flag='attributes')
        {   
            $properties = $activity->properties->toArray();

            return array_key_exists($flag, $properties) ? $properties[$flag] : null;
        }

        public static function getPropertiesChanges($activity)
        {
            $attributes = self::getProperties($activity, 'attributes');
            $old        = self::getProperties($activity, 'old');

            $result = [];
            foreach ($attributes ?? [] as $key => $value) {
                if (isset($old) && ($attributes[$key] != $old[$key])) {
                    $result[] = $key;
                }
            }

            return !empty($result) ? $result : null;
        }

        public static function getPropertiesArray($model)
        {
            $attributes    = $model->getAttributes();
            $oldAttributes = $model->getOriginal();
        
            $properties['attributes'] = $attributes;

            $checkAttributes = false;
            foreach ($attributes as $key => $value) {
                if (!empty($oldAttributes) && ($oldAttributes[$key] != $value)) {
                    $checkAttributes = true;
                    break;
                }
            }

            if ($checkAttributes) 
                $properties['old']    = $oldAttributes;

            return $properties;
        }
    }

    // Display filter if transmitted field is in array from config logging_filters
    class Filters
    {
        const SELECT_DEFAULT_KEY = 'default';

        public static function getFilter($field, $translate)
        {   
            $fieldsForFilterSelect = config('logging.logging_filters.selectable');
            $fieldsForFilterText   = config('logging.logging_filters.according_to_the_text');

            if (in_array($field, $fieldsForFilterSelect)) {
                $resultHtml = self::getSelectFilter($field, $translate);
            } elseif (in_array($field, $fieldsForFilterText)) {
                $resultHtml = self::getTextFilter($field, $translate);
            } else {
                $resultHtml = null;
            }

            return $resultHtml;
        }

        private static function getSelectFilter($field, $translate)
        {
            $activityModel = config('activitylog.activity_model');

            $values = $activityModel::select([$field])->distinct()->orderBy($field)->get();

            $arrayValues[self::SELECT_DEFAULT_KEY] = $translate;
            foreach ($values as $value) {
                if (!is_null($value->{$field})) {
                    $arrayValues[$value->{$field}] = $value->{$field};
                } else {
                    $arrayValues['null'] = __('logging::logging.user_logging.undefined');
                }
            }

            return view('logging::filters._select_filter', compact('field', 'arrayValues'));
        }
     
        private static function getTextFilter($field, $translate)
        {
            return view('logging::filters._text_filter', compact('field', 'translate'));
        }
    }
bash
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="views"
bash
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="public"
bash
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="translations"
bash
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="config"