PHP code example of wadywh / operation-log

1. Go to this page and download the library: Download wadywh/operation-log 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/ */

    

wadywh / operation-log example snippets




return [
    'default' => env('DB_CONNECTION', 'mysql'),
    ...
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            ...
            ...
            // 模型所在的命名空间
            "modelNamespace" => "Operation\Log\Test\model",
            // 日志记录的主键
            "logKey" => "id",
        ],
        ...
    ]
    ...
];



return [
    'default'         => env('database.driver', 'mysql'),
    ...
    'connections'     => [
        'mysql' => [
            // 服务器地址
            'hostname'        => env('database.hostname', '127.0.0.1'),
            // 数据库名
            'database'        => env('database.database', ''),
            // 用户名
            'username'        => env('database.username', 'root'),
            // 密码
            'password'        => env('database.password', ''),
            // 端口
            'hostport'        => env('database.hostport', '3306'),
            ...
            ...
            // 数据库类型
            'type'            => \Operation\Log\orm\think\MySqlConnection::class,
            // 指定查询对象
            "query"           => \Operation\Log\orm\think\Query::class,
            // Builder类
            "builder"         => \think\db\builder\Mysql::class,
            // 模型所在的命名空间
            "modelNamespace"  => "Operation\Log\Test\model",
            // 日志记录的主键
            "logKey"          => "id",
        ],
        // 更多的数据库配置信息
        ...
    ],
    ...
];

\Illuminate\Database\Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
    return new \Operation\Log\orm\illuminate\MySqlConnection($connection, $database, $prefix, $config);
});

\Operation\Log\facades\OperationLog::setTableModelMapping([
    'test_user' => 'App\Models\Test\User',
    'test_role' => 'App\Models\Test\Role',
]);

\Operation\Log\facades\OperationLog::setExecInfoSchema(false);



namespace Operation\Log\Test\model;

class User extends BaseModel
{
    // 日志记录的主键名称
    public string $logKey = 'id';
}



namespace Operation\Log\Test\model;

class User extends BaseModel
{
    // 日志记录的主键名称
    public string $logKey = 'id';
    // 表注释
    public $tableComment = '用户';
    // 字段注释
    public $columnComment = [
        'name' => '姓名',
        'sex' => '性别',
    ];
    // 日志记录忽略的字段
    public $ignoreLogFields = [
        'create_time',
        'update_time',
    ];

    // Laravel ORM 获取器设置方法
    public function getSexTextAttribute($key): string
    {
        return ['女','男'][($key ?? $this->sex)] ?? '未知';
    }

    // ThinkPHP ORM 获取器设置方法
    public function getSexTextAttr($key): string
    {
        return ['女','男'][($key ?? $this->sex)] ?? '未知';
    }
}



namespace Operation\Log\Test\model;

class User extends BaseModel
{
    // 不生成操作日志
    public $notRecordLog = true;
}

\Operation\Log\facades\OperationLog::getLog();

\Operation\Log\facades\OperationLog::clearLog();

\Operation\Log\facades\OperationLog::setTableModelMapping($map);

\Operation\Log\facades\OperationLog::setExecInfoSchema(false);

\Operation\Log\facades\OperationLog::setRecordTypes(['updated', 'deleted']);

\Operation\Log\facades\OperationLog::setShutdownFunction([new $className, $methodName]);