1. Currently, there is a requirement to send the generated operation logs to the middle platform’s log system during a single API call. The middle platform’s log system API supports receiving multiple log records simultaneously. Therefore, I decided to first store the N operation logs generated during an API call in a global variable, and then, when the API call ends, retrieve the log records from the global variable and send them to the middle platform’s log system together in a single HTTP request. As shown in Figure 1

2. Reference URL: https://www.yiiframework.com/doc/guide/2.0/zh-cn/structure-applications#params . Yii::$app->params. As shown in Figure 2

3. Edit the log template messages and configure the corresponding templates. Edit the corresponding language pack file: common/messages/zh-CN/application.php
<?php
return [
302200 => '新建选题【{plan_title}】',
302201 => '启用选题【{plan_title}】',
302202 => '禁用选题【{plan_title}】',
302203 => '提交选题【{plan_title}】进行审核',
302204 => '编辑选题【{plan_title}】',
302205 => '删除选题【{plan_title}】',
302206 => '通过选题【{plan_title}】',
302207 => '拒绝选题【{plan_title}】',
302208 => '指派【{plan_task_title}】{plan_task_type}给【{user_nick}】',
302209 => '启用【{plan_task_title}】{plan_task_type}',
302210 => '禁用【{plan_task_title}】{plan_task_type}',
302211 => '删除【{plan_task_title}】{plan_task_type}',
302212 => '新建【{plan_task_title}】{plan_task_type}指派给【{user_nick}】',
302213 => '编辑【{plan_task_title}】{plan_task_type}',
302214 => '转派【{plan_task_title}】{plan_task_type}给【{user_nick}】',
302215 => '认领【{plan_task_title}】{plan_task_type}',
];4. Categorize the log messages accordingly, edit: common/config/params.php
<?php
return [
'cmcApiLogOperates' => [ //框架服务接口日志操作类型
[
'operateKey' => 'plan_management', //操作类型标识
'operateTitle' => '选题管理', //操作类型名称
'codes' => [302200, 302201, 302202, 302203, 302204, 302205], //日志详情的返回码
],
[
'operateKey' => 'plan_review', //操作类型标识
'operateTitle' => '选题审核', //操作类型名称
'codes' => [302206, 302207], //日志详情的返回码
],
[
'operateKey' => 'plan_task_management', //操作类型标识
'operateTitle' => '任务管理', //操作类型名称
'codes' => [302208, 302209, 302210, 302211, 302212, 302213, 302214, 302215], //日志详情的返回码
]
],
];5. Write a method to automatically generate the global variable for logs
/**
* 自动创建上报日志信息,放入(Yii::$app->params['cmcApiLogs']),为后续 HTTP 请求准备数据
* @param int $code 返回码
* @param array $data 数据,需要被替换的变量
* 格式如下:
* [
* 'plan_title' => '选题名称',
* ]
*
* @param object $identity 当前用户的身份实例
*/
public static function automaticCreate($code, $data, $identity)
{
foreach (Yii::$app->params['cmcApiLogOperates'] as $operateValue) {
foreach ($operateValue['codes'] as $codeValue) {
if ($codeValue == $code) {
$operateKey = $operateValue['operateKey'];
$operateTitle = $operateValue['operateTitle'];
}
}
}
if (isset($operateKey) && isset($operateTitle)) {
Yii::$app->params['cmcApiLogs'][] = [
'user_token' => $identity->user_token,
'service_key' => Yii::$app->params['cmcApi']['serviceKey'],
'log_type' => 'operate',
'operate_key' => $operateKey,
'operate_title' => $operateTitle,
'log_detail' => Yii::t('application', Yii::t('application', Yii::t('application', $code), $data)),
'log_time' => date('Y-m-d H:i:s'),
'log_ip' => '0.0.0.0',
];
}
}
6. Check whether it exists when the EVENT_AFTER_REQUEST event is triggered; if it exists, print Yii::$app->params[‘cmcApiLogs’]. There are 2 log records, so you can retrieve the log records from the global variable when the EVENT_AFTER_REQUEST event is triggered, and send them to the middle platform’s log system together in a single HTTP request. As shown in Figure 3
![Check whether it exists when the EVENT_AFTER_REQUEST event is triggered; if it exists, print Yii::$app->params['cmcApiLogs']. There are 2 log records, so you can retrieve the log records from the global variable when the EVENT_AFTER_REQUEST event is triggered, and send them to the middle platform's log system together in a single HTTP request.](https://media.shuijingwanwq.com/2021/10/3-4.png)
Array
(
[0] => Array
(
[user_token] => 3cf1c3d1db4951ad3179e1452580c0c3
[service_key] => pcs
[log_type] => operate
[operate_key] => plan_management
[operate_title] => Topic Management
[log_detail] => Create new topic [Topic 20211015 0]
[log_time] => 2021-10-15 14:02:00
[log_ip] => 0.0.0.0
)
[1] => Array
(
[user_token] => 3cf1c3d1db4951ad3179e1452580c0c3
[service_key] => pcs
[log_type] => operate
[operate_key] => plan_management
[operate_title] => Topic Management
[log_detail] => Submit topic [Topic 20211015 0] for review
[log_time] => 2021-10-15 14:02:00
[log_ip] => 0.0.0.0
)
)
