


How to implement addition, deletion, modification and check operation log recording in ThinkPHP5.1
1. Introduction to the logging function of ThinkPHP 5.1
In ThinkPHP 5.1, the logging function has been built-in. We can use it in our applications to record different types of events, such as error messages, debugging information, and user actions. Four levels of logging are provided by the framework: DEBUG, INFO, NOTICE and ERROR.
In addition to the built-in log levels, we can also define our own log levels. We can give an example by creating a log level named "CRUD" to record addition, deletion, modification and query operations.
By default, logs will be recorded to the log directory under the application root directory. If you need to change the log storage location, you can do so by modifying the configuration file. Configuration files are generally located in the application's config directory.
2. Implementation of log recording of add, delete, modify and check operations
Let’s take a look at how to implement log recording of add, delete, modify and check operations in ThinkPHP 5.1. We will follow the following steps to achieve it:
Create a custom configuration file in the config directory
Add log fields to the data table
Rewrite the addition, deletion and modification methods in the Model
Record logs
Create a custom configuration in the config directory File
We need to create a custom configuration file first and place it in the config directory of the application. We name this file common_extra.php.
$config = [
'crud_log' => true, // 记录增删改查日志 'crud_ignore_fields' => ['create_time', 'update_time'] // 忽略日志记录的字段
];
In the configuration, we set two options. The first is crud_log, which is used to turn logging on or off. The second is crud_ignore_fields, which is used to specify fields that are not logged.
Add log fields to the data table
Next, we need to add some fields to the data table to record addition, deletion, modification and query operations. We can add the following fields to each data table:
id (auto-increment primary key)
user_id (operation user id, can be empty )
action (operation type, such as add, delete, modify)
table_name (data table name of the operation)
data (operated data)
created_at (operation time)
Rewrite the addition, deletion and modification methods in the Model
Now we need to rewrite the addition, deletion and modification methods in the Model to implement logging. We will use global query scope to achieve this. We will override the create, update and delete methods.
In each method, we will record the corresponding operation type and data. We will then write the logs to the log file by using the log class.
Here are some sample codes:
namespace app\common\model;
use think\Model;
class User extends Model
{
protected $table = 'users'; // 添加全局查询范围 protected static function init() { // 添加操作记录 static::beforeInsert(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'add'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); // 修改操作记录 static::beforeUpdate(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'update'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); // 删除操作记录 static::beforeDelete(function ($item) { if (config('common_extra.crud_log')) { $item->user_id = session('user_id'); $item->action = 'delete'; $item->table_name = $this->table; $item->data = json_encode($item->toArray(), JSON_UNESCAPED_UNICODE); $item->created_at = date('Y-m-d H:i:s', time()); Db::table('log')->insert($item->toArray()); } }); }
}
Logging
Finally, we will log. In the previous example, we wrote the log to a data table named "log". However, you can write the logs to a file, send them to a log server, or send them elsewhere if desired.
Through the above steps, we successfully implemented the function of logging addition, deletion, modification and query operations in ThinkPHP 5.1.
The above is the detailed content of How to implement addition, deletion, modification and check operation log recording in ThinkPHP5.1. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.
