douxian9010 2019-07-30 15:59
浏览 32
已采纳

防止Log中的重复条目

I have a spam with some code, I'm trying to prevent the logs from spamming "Cancellation Identified", there are 3 instances when the same code is run and it is spamming. If there is a duplicate I want to prevent from "Cancellation Identified" spamming in my log.

Is there a way to prevent duplicate entries into log for "Cancellation Identified"?

I have some pseudo code for a fix, but am having trouble converting it to php.

   if($searchfor)
    {
        $searchfor = "Cancellation Identified";
        $searchfor = true;
        continue process_imports();
    }
if(!empty($contract)){
    //Determine if contract has been cancelled based on the presence of a cancellation date.
    if ((isset($data['cancelled_date'])) && (substr_count($data['sold_date'], '/') == 2) && ($contract->cancelled_date >= '2015-01-01')) {
        //If cancelled determine if cancellation is new by comparing to previously cancelled contracts table.
        $IsCancelled = ContractCancellation::LocateCancellation($contract->moxy_contract_id);
        if (!$IsCancelled->first()) { //Contract is not in cancellations table, flag contract for later cancellations processing.
            $contract->cancel_pending = 1;
            if($contract->hold == '1'){
                LogAction::add("Data Adjustment", "Hold Removed Due To Contract Being Cancelled.", 0, "", $contract->moxy_contract_id);
            }
            $contract->hold = 0;
            $contract->save();
            LogAction::add("Data Adjustment", "Cancellation Identified.", 0, "", $contract->moxy_contract_id);                                  
        }
    }
    $contract->cancel_miles = !empty($data['cancel_miles']) ? $data['cancel_miles'] : 0;
    $contract->cancel_reason = !empty($data['cancel_reason']) ? $data['cancel_reason'] : NULL;
    $contract->save();
}
  • 写回答

1条回答 默认 最新

  • dtsps00544 2019-08-01 07:16
    关注

    As mentioned in the comments, you can use Session helper to remember whether the log is made already or not, to avoid spamming your log.

    <?php
    
    if(empty(session('Cancellation_identified')) || session('Cancellation_identified') !== $contract->moxy_contract_id){
        session(['Cancellation_identified' => $contract->moxy_contract_id]);
        LogAction::add("Data Adjustment", "Cancellation Identified.", 0, "", $contract->moxy_contract_id);         
    } 
    

    The above would check if Cancellation_identified is present in the session or not. If not, it would make a log entry and add the Cancellation_identified key with it's respective ID to the session. In other 2 instances, you can have the same check.

    Note that, this also useful with multiple HTTP requests as it is quite possible that $contract->moxy_contract_id would be different for each request. Above code would handle that too since we are checking for ID equality as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度