ebola / logging
为PHP 7.1和包spatie/laravel-activitylog提供日志记录功能
v0.6.4
2018-07-17 07:54 UTC
Requires
- php: >=7.0
- spatie/laravel-activitylog: 2.*
README
这是一个 Laravel >=5.5 的包。它提供了一个简单的API来处理和版本控制 Laravel Activitylog。要了解有关 Laravel Activitylog 的更多信息,请访问详细的文档。
以下是一些简短的示例,说明你可以做什么
$logging = new LoggingRender();
可以在前端显示所有用户活动
{!! $logging->renderUserLogging() !!}
或显示下载用户活动的表单
{!! $logging->renderDownloadLogging() !!}
安装
您可以通过以下命令使用composer安装此包:
composer require ebola/logging
接下来,您必须安装服务提供者
// 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, ];
您必须发布activitylog迁移
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
您必须迁移
php artisan migrate
您可以选择发布activitylog配置文件
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
您可以使用以下命令发布视图:
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="views"
您可以使用以下命令发布资源:
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="public"
您可以使用以下命令发布翻译:
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="translations"
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Ebola\Logging\LoggingServiceProvider" --tag="config"
这是发布配置文件的内容
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); }
显示日志
您必须创建LoggingRender类的对象。您可以将用户对象作为参数传递以仅显示其日志,以及要在屏幕上显示或保存的字段列表。如果没有传递值,您将输出所有日志和配置文件中指定的字段。
$logging = new LoggingRender(); // or $logging = new LoggingRender(\Auth::user(), ['id', 'log_name', 'description']);
您有两个受保护的方法用于显示日志
{!! $logging->renderUserLogging() !!}
{!! $logging->renderDownloadLogging() !!}
如果您使用渲染方法,请记住调用css和js以正确显示
<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')); } }
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。