iamjohndev/audit-logger

Audit Logger 是一个简单的 PHP 类,允许您在应用程序中记录用户交互。

v1.1 2023-04-21 17:31 UTC

This package is auto-updated.

Last update: 2024-09-30 01:54:34 UTC


README

Audit Logger 是一个简单的 PHP 类,允许您在应用程序中记录用户交互。您可以根据需要配置日志驱动程序(文件或数据库)、数据库连接和日志格式化程序。

安装

您可以使用 Composer 安装 Audit Logger,只需运行以下命令

composer require iamjohndev/audit-logger

将您的-vendor-name 替换为您想要的供应商名称,将您的-package-name 替换为您想要的包名称。

使用

在您想使用 AuditLogger 类的 PHP 文件中包含 Composer 自动加载器

require_once 'vendor/autoload.php';

在使用 AuditLogger 之前设置日志存储驱动程序和数据库连接(如果使用数据库驱动程序)

use iamjohndev\AuditLogger;

// Set the log storage driver
AuditLogger::setLogDriver('database');

// Set the database connection
$dbConnection = new mysqli('localhost', 'username', 'password', 'database_name');
AuditLogger::setDbConnection($dbConnection);

如果您想自定义日志条目格式,可以设置日志格式化程序(可选)

AuditLogger::setLogFormatter(function($userId, $action) {
    // Custom log formatting logic
    return "User ID: $userId | Action: $action";
});

通过调用 log 方法开始记录用户交互

// Log user interaction
$userId = 123;
$action = 'edit';
AuditLogger::log($userId, $action);

使用 getAllLogs 方法检索所有日志

// Get all logs
$logs = AuditLogger::getAllLogs();
print_r($logs);

使用 clearLogs 方法清除所有日志

// Clear all logs
AuditLogger::clearLogs();

数据库配置

如果您使用的是数据库日志存储驱动程序,请确保通过在数据库连接代码中设置主机、用户名、密码和数据库名称的适当值来配置数据库连接

$dbConnection = new mysqli('localhost', 'username', 'password', 'database_name');

将 'localhost', 'username', 'password' 和 'database_name' 替换为您数据库配置的实际值。

自定义日志表(可选)

如果您想使用自定义表名在数据库中存储日志,可以使用 setLogsTableName 方法设置它

AuditLogger::setLogsTableName('custom_logs_table');
CREATE TABLE custom_logs_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    timestamp DATETIME,
    user_id INT,
    action VARCHAR(255),
    module VARCHAR(255),
    log_entry TEXT
);

将 'custom_logs_table' 替换为您自定义日志表的首选名称。

许可证

此项目受 MIT 许可证许可。有关详细信息,请参阅 LICENSE 文件。