dzaki236 / logging-services
简化Laravel日志库,用于管理日志txt文件,简单易用,基于数据
Requires
- php: >=7.2.0
- illuminate/support: >=5.7
README
日志活动包,简单易用!..
安装步骤
使用以下命令安装包:
$ composer require dzaki236/logging-services
自动加载转储
使用以下命令自动加载包:
$ composer dump-autoload
添加提供者 (在某些情况下此步骤是可选的)
如果使用的是Laravel的早期版本,请将提供者添加到您的config/app.php
文件中的provider
部分
'providers' => [ ... /* * Package Service Providers... */ Dzaki236\LoggingServices\LoggingServicesServiceProvider::class, ... ]
添加外观 (这是可选部分)
将外观添加到您的config/app.php
文件中的aliases
部分的最末尾
'aliases'=>[ ... 'LoggingServices'=>Dzaki236\LoggingServices\LoggingServices::class, ]
发布资产
运行以下命令发布配置文件:
$ php artisan logging-services:publish
运行迁移
如果您想使用日志表中的表,只需运行
$ php artisan migrate
或者,如果您想重置所有迁移 (包括您的数据种子)
,只需运行
$ php artisan migrate:fresh --seed
关系 (可选部分)
如果您想在日志表中使用与用户的关系,请在模型LogActivity.php
中添加以下代码
版本 7+-
public function user() { # code... return $this->belongsTo('App\User','user_id'); }
版本 8.x+
public function user() { # code... return $this->belongsTo(User::class,'user_id'); }
别忘了在关系的第一行(在类之前)添加这些内容(命名空间)
use App\Models\User;
然后,如果您想使用(预加载)
,例如在代码中
LogActivity::with('user')->all();
更改刷新限制?
有时您可能想自定义刷新日志的限制值,在Laravel项目的config
文件夹中的logservices.php
文件中,按照以下代码进行更改(config/logservices.php)
return [ .... 'flush' => TRUE, // change this for activate / disactivated flush (default = true) 'limit' => 1000, // change this for limit you want to flush ];
如何使用?
首先,在您的控制器(例如:UserController
或任何其他控制器)中,在__construct()
之前添加以下行:
// use App\Services\LogActivitiesServices\MainLogActivitiesServices; // You can use namespacing like this at first line before class on contoller /** * Display a listing of the resource. * * @return void */ public function __construct(MainLogActivitiesServices $logs) { ... $this->loging = $logs; // variable name (optional) } /* Or, just write the source like this by following code (without namespacing) : */ /** * Display a listing of the resource. * * @return void */ public function __construct(\App\Services\LogActivitiesServices\MainLogActivitiesServices $logs) { ... $this->loging = $logs; // variable name (optional) }
在以下示例中添加代码
// Example at store function on controller public function store(Request $request) { //if $data = new Student($request->all()); $con = $data->save(); if ($con) { # code... $this->loging->activitylog(true,'add student data'); // if success set to true return redirect()->route('students.index')->with(['success'=>'Success!']); } else { # code... $this->loging->activitylog(false,'add student data'); // if had failure on proccess set to false return redirect()->route('students.index')->with(['error'=>'Failure!']); } } // Example at update function on controller public function update($id,Request $request) { //if $data = Student::find($id) $data->update($request->all()); $con = $data->save(); if ($con) { # code... $this->loging->activitylog(true,'success update student data'); // if success set to true return redirect()->route('students.index')->with(['success'=>'Success!']); } else { # code... $this->loging->activitylog(false,'failure update student data'); // if had failure on proccess set to false return redirect()->route('students.index')->with(['error'=>'Failure!']); } }
函数参数
所有函数参数如下,并按此顺序填写此不能随机填写!!,必须按顺序填写
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息,版本2.0.2
如果您在旧版本中遇到问题或问题,请使用最新版本
更多功能在此!
自定义文件配置(日志)
在2.0.2版本的新更新中,作者添加了新的txt读取器,您可以使用此功能在控制器上进行自定义日志,首先步骤您必须创建配置/日志配置txt文件,如果不这样做,您将收到错误:
以下代码为示例
public function store(Request $request) { //if $data = new Student($request->all()); $con = $data->save(); if ($con) { # code... $this->loging->activitylog(true,'add student data'); // loging services this is optional, if you want to record a some log activities on your project but somehow you need it or not. /* try this */ $config = new \Dzaki236\LoggingServices\FileServicesConfig('config.txt'); // the models allowed a constructor to open a new config file (by default : at public path),jsust following the code. $config->fieldInsert($field,$fill); /*$field = Field To Scaffolding at txt file!*/ /*$fill = fill this with value (default: request value)*/ // For example anyway field id you can use at some case!.. $config->fieldInsert(array('id','name','class','email'),array($data->id,$request->name,$request->class,$request->email)); return redirect()->route('students.index')->with(['success'=>'Success!']); } else { # code... $this->loging->activitylog(false,'add student data'); // if had failure on proccess set to false return redirect()->route('students.index')->with(['error'=>'Failure!']); } }
默认情况下,在2.0.2版本以下的版本中,您不能保存日志的txt文件,但今天您可以!默认情况下,在2.0.2中,您可以保存一些txt文件,例如作者在Laravel项目的public文件中的logactivities.txt
输入txt文件,但您可以通过使用样板类构造函数自定义文件或创建新的路径自定义配置,所有函数/参数都在样板类中,只需按照以下顺序填写,并查看此不能随机填写!!,必须按顺序填写
在模型FileServicesConfig
的constructor
中为YES
!
在config.txt
文件(示例)中
默认情况下,可能您允许随机字段,但不推荐,首先您必须修复,您想填充哪些字段,默认情况下是您的版本。
name|class|email jhondoe|12Ab|jhondoe@gmail.com
错误警告
库有自己的稳定测试,如果出错,可能需要再次检查代码,现在这是错误代码和正确代码的示例
# Wrong code /* * Do not use return something else before library loaded!. * just see some an example's here */ # wrong example ... action ... $data = Model::create($request->all()); return $data;// return some from action $this->logvariables->activitilog(true,'message logs here'); $files = new \Dzaki236\LoggingServices\FileServicesConfig('vendorlogs.txt'); $files->fieldInsert(['field1','field2','field3'],[$request->field1,$request->field2,$request->field3]); } # right example ... action ... $data = Model::create($request->all()); $this->logvariables->activitilog(true,'message logs here'); $files = new \Dzaki236\LoggingServices\FileServicesConfig('vendorlogs.txt'); $files->fieldInsert(['field1','field2','field3'],[$request->field1,$request->field2,$request->field3]); return $data;// return some from action here }
闭包:您必须完成某些操作,并且您可以返回您想要的结果
。
许可证
MIT许可(MIT)。请参阅许可文件获取更多信息,版本为2.0.2