merkeleon/laravel-log 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

merkeleon/laravel-log

最新稳定版本:1.4.6

Composer 安装命令:

composer require merkeleon/laravel-log

包简介

Laravel Audit Log

README 文档

README

Laravel module for logs generating

Installation

First, require the package using Composer:

composer require merkeleon/laravel-log

  1. Add new class Log that should extend Merkeleon\Log\Model\Log

  2. Custom your log class.

    • Add protected static attribute $table - the name of table or elastic search index
    • Add custom parameters $customAttributes. By default only parameters Log::$attributes will be save. The keys of Log::$customAttributes are parameters identificators. The values of Log::$customAttributes are casts. Following casts are supported:
      • int
      • float
      • string
      • bool
      • array
    • You can add validation rules.
    • If you want to duplicate your logs to files you should override function toLogFileArray
  3. Add the merkeleon_log.php config php artisan vendor:publish --provider="Merkeleon\Log\Providers\MerkeleonLogProvider"

    • Point your log class
    • Point the driver (mysql or elastic)
    • If you want to duplicate your logs to files you should point the path to log file.
  4. If you want save your logs to buffer and then bulk write all logs from buffer to storage

    • Add buffer directory path to the merkeleon_log.php config. Buffer directory should be writable
    • You should empty your buffer by calling command php artisan merkeleon:log:bulk-insert-to-storage {logName}

###Example:

<?php

namespace App\Models;

use Merkeleon\Log\Model\Log;

class AuditLog extends Log
{
    protected static $table = 'audit_logs';

    protected static $customAttributes = [
        'user_id' => 'int',
        'user_id_related' => 'int',
        'data' => 'array',
    ];

    protected static $rules = [
        'event_type'      => 'required',
        'ip'              => 'required',
        'user_agent'      => 'required',
        'user_id'         => 'integer',
        'user_id_related' => 'integer',
    ];

    public function toLogFileArray()
    {
        return [
            "created_at"         => $this->created_at->format('Y-m-d H:i:s'),
            "ip"                 => $this->ip,
            "event_type"         => $this->event_type,
            "user_id"            => $this->user_id,
            "user_id_related"    => empty($this->user_id_related) ? '-' : $this->user_id_related,
            "user_agent"         => '"' . $this->user_agent . '"',
            "data"               => json_encode($this->data)
        ];
    }

}

    <?php
    
    return [
        'audit_log' => [
            'class' => \App\Models\AuditLog::class,
            'driver' => 'elastic',
            'log_file' => '/var/www/logs/audit_log.log'
        ],
    ];

Usage

##Examples

$auditLogRepository = LogRepository::make('audit_log');

$auditLogRepository->write([
    'user_id'         => 1,
    'event_type'      => 'user_banned',
    'user_id_related' => 2,
    'data'            => [
        'user'         => [
            'id'   => '1',
            'name' => 'Admin User',
        ],
        'user_related' => [
            'id'   => '2',
            'name' => 'Test user'
        ]
    ]
]);

$auditLogRepository->where('user_id', 1)->orderBy('event_type', 'asc')->paginate(10);

$auditLogRepository->where('user_id', 1)->get();

##Examples

    $auditLogRepository = LogRepository::make('audit_log');

    $auditLogRepository->write([
        'user_id'         => 1,
        'event_type'      => 'user_banned',
        'user_id_related' => 2,
        'data'            => [
            'user'         => [
                'id'   => '1',
                'name' => 'Admin User',
            ],
            'user_related' => [
                'id'   => '2',
                'name' => 'Test user'
            ]
        ]
    ], true);
    
    $auditLogRepository->write([
        'user_id'         => 1,
        'event_type'      => 'user_login',
        'data'            => [
            'user'         => [
                'id'   => '1',
                'name' => 'Admin User',
            ]
        ]
    ], true);

    // call command `php artisan merkeleon:log:bulk-insert-to-storage audit_log`
    
    $auditLogRepository->where('user_id', 1)->orderBy('event_type', 'asc')->paginate(10);
   
    $auditLogRepository->where('user_id', 1)->get();

统计信息

  • 总下载量: 29.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2018-12-11