亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home PHP Framework ThinkPHP How to implement logging of addition, deletion, modification and query operations in ThinkPHP 5.1

How to implement logging of addition, deletion, modification and query operations in ThinkPHP 5.1

Apr 11, 2023 am 09:15 AM

ThinkPHP is a very popular PHP development framework that provides a variety of powerful functions and supports rapid development. Among them, CRUD (CRUD) is an indispensable basic operation in web development. In order to better track the operation of the application, it is very important to record operation logs. In this article, we will explore how to implement logging of add, delete, modify and query operations in ThinkPHP 5.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 application to record various events such as error messages, debugging information, user actions, etc. The framework provides four levels of logging: DEBUG, INFO, NOTICE and ERROR.

In addition to the built-in log levels, we can also define our own log levels. For example, we can create a log level "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 query operations

Let’s take a look at how to implement logging of add, delete, modify and query operations in ThinkPHP 5.1. We will follow the following steps to achieve it:

  1. Create a custom configuration file in the config directory
  2. Add log fields to the data table
  3. Rewrite additions, deletions and modifications in the Model Method
  4. Logging
  5. Create a custom configuration file in the config directory

First, we need to create a custom configuration file 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.

  1. 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:

  1. id (auto-increment primary key)
  2. user_id (operation user id, can be empty)
  3. action ( Operation type, such as add, delete, modify)
  4. table_name (data table name of operation)
  5. data (data of operation)
  6. created_at (operation time)
  7. 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());
????????}
????});
}

}

  1. 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.

Conclusion

In this article, we introduced how to record the log of addition, deletion, modification and query operations in ThinkPHP 5.1. By comparing traditional operation methods, we found that using logging can better track the operation of the application, and can also be better combined with other tools for analysis. I hope readers can use the experience of this article to improve the maintainability and scalability of applications.

The above is the detailed content of How to implement logging of addition, deletion, modification and query operations in ThinkPHP 5.1. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72