Laravel Activity Log 安装和基础用法【传送门】
高级使用
占位符
当记录一个活动日志时,你可以使用以:subject,:causer或:properties开头的占位符。这些占位符将被给定主题、causer或属性的属性替换。
activity()
->performedOn($article)
->causedBy($user)
->withProperties(['laravel' => 'awesome'])
->log('The subject name is :subject.name, the causer name is :causer.name and Laravel is :properties.laravel');
自定义log name
默认情况下,LogsActivity trait使用配置文件中的default_log_name来写日志。
1、通过activity($logname)指定日志名称
2、通过设置模型上的$logName属性,可以为每个模型设置不同的日志。
3、通过重新getLogNameToUse方法
//1、主动指定
activity('other-log')->log("hi");
//2、在操作模型中指定
protected static $logName = 'custom_log_name_for_this_model';
//3、通过重新getLogNameToUse方法
/**
* 在此自定义日志的log_name字段
*
*
* @param string $eventName
* @return string
*/
public function getLogNameToUse(string $eventName = ''): string
{
if (self::$logName) {
return self::$logName . '.' . $eventName;
}
if ($this->getConnectionName()) {
return $this->getConnectionName() . '.' . $this->getTable() . '.' . $eventName;
}
return $this->getTable() . '.' . $eventName;
}
日志记录查询
//returns all activity from the 'other-log'
Activity::where('log_name' , 'other-log')->get();
Activity::inLog('other-log')->get();
//you can pass multiple log names to the scope
Activity::inLog('default', 'other-log')->get();
//passing an array is just as good
Activity::inLog(['default', 'other-log'])->get();
通过log日志模型Event记录
可以让Model使用LogsActivity tirat实现自动记录事件,比如模型何时被创建、更新和删除。
1、在模型上设置$logAttributes属性时,包还会记录所有这些事件的更改属性。
2、需要记录的属性可以通过名称定义,也可以使用通配符“*”来记录任何已更改的属性。
3、如果你想记录模型中所有$fillable属性的变化,你可以指定protected static $logFillable = true;在模型上
4、如果你有很多属性,使用$ protected而不是$fillable,你也可以设置protected static $logunprotected = true;
对于这两个布尔标志,它将遵循可能的通配符并添加all (logFillable)或no ( logunprotected)属性。
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
class NewsItem extends Model
{
use LogsActivity;
protected $fillable = ['name', 'text'];
//记录字段信息变更
protected static $logAttributes = ['name', 'text'];
//记录所以fillabel变更
protected static $logFillable = true;
//记录排除的字段变更
protected static $logunprotected = true;
//忽略变更记录
protected static $ignoreChangedAttributes = ['text'];
//只记录变更的字段信息
protected static $logOnlyDirty = true;
//可以通过点.的方式记录关联模型的数据变更
//protected static $logAttributes = ['name', 'text', 'user.name'];
public function user()
{
return $this->belongsTo(User::class);
}
}
在model里面控制记录日志的事件
//created,updated,deleted
protected static $recordEvents = ['deleted'];
通过重写getDescriptionForEvent设置日志描述:这里可以根据event类型进行操作描述定制化
public function getDescriptionForEvent(string $eventName): string
{
return "This model has been {$eventName}";
}
添加自定义字段
public function tapActivity(Activity $activity, string $eventName)
{
$activity->ip = request()->ip();
}
运行时配置
您还可以在运行时禁用特定模型的日志记录。为此,您可以使用disableLogging()方法禁用记录,
$newsItem = NewsItem::create([
'name' => 'original name',
'text' => 'Lorum'
]);
// Updating with logging disabled
$newsItem->disableLogging();
//该次变更将不会记录日志
$newsItem->update(['name' => 'The new name is not logged']);
//如果需要记录,重新启动记录
$newsItem->enableLogging();
$newsItem->update(['name' => 'The new name is logged']);