php 把抛出错误记录到日志中

  /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            //执行代码
            (new LazadaTransactionsExportQueueJobLogic())->run($this->batchId);
        } catch (\Exception $exception) {
            $this->failed($exception);
        };
    }

    /**
     * 任务失败的处理过程
     *
     * @param Exception $exception
     * @return void
     */
    public function failed(\Exception $exception)
    {
        $logFile = fopen(
            storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_lazada_transactions_export_queue.log'),
            'a+'
        );
        fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $exception->getMessage() . PHP_EOL);
        fclose($logFile);
    }

 

posted on 2021-08-03 14:51  kevin_yang123  阅读(61)  评论(0编辑  收藏  举报