【问题标题】:Store cron job logs in a file Php laravel将 cron 作业日志存储在文件中 Php laravel
【发布时间】:2020-03-18 11:40:00
【问题描述】:

我正在尝试将我的 cron 作业记录到日志文件中。不幸的是,我无法做到这一点,也找不到任何关于此的教程/博客。

我正在使用最新的 laravel 版本,我写了 1 个 cron 作业

这是我的命令文件句柄

public function handle()
    {
        Employee::where('id', 2)->update(['salary' => 0]);

        $this->info('Command:Update Run successfully!');
    }

这是我的日程安排

$schedule->command('command:update')
        ->everyMinute();

【问题讨论】:

    标签: php laravel cron


    【解决方案1】:

    在 cron 作业命令中执行此操作。这将在 queue.log 中创建 cron 日志。

    /usr/local/bin/<ea-php71> /home/<domain>/public_html/artisan queue:listen 1>> public_html/queue.log 2>&1
    

    这里 ea-php71 适用于 php 版本 7.1。您可以找到其他 php 版本。

    【讨论】:

      【解决方案2】:

      试一试

      添加到顶部

      use Illuminate\Support\Facades\Log;
      

      并在您的代码中使用

      public function handle()
      {
          Log::info('cron has just started...');
          Employee::where('id', 2)->update(['salary' => 0]);
          $this->info('Command:Update Run successfully!');
          Log::info('... and now cron has ended ?');
      }
      

      您将根据您在.env LOG_CHANNEL 中配置的内容以及您在config/logging.php 中配置的内容找到日志?


      添加用于记录数据库内容


      当然,我这样做的方式如下:在我的AppServiceProvider.php 中,我在boot 函数中有这个:

              if (config('database.log_querys') === true) {
                  DB::listen(static function ($query) {
                      Log::debug($query->sql, [$query->bindings, $query->time]);
                  });
              }
      

      在我的 config 和 env 中,我有触发器来记录所有数据库查询或不记录 ?

      除此之外,我使用https://logcrawler.de 集中查看所有日志?

      【讨论】:

      • 几乎这就是我要找的......你知道如何在日志文件中显示查询结果吗?
      • 好吧,让我试试这个,我希望它能完美运行
      • 先生,您能告诉我如何使用本地项目配置 logccrawler... 我正在尝试但无法成功连接谢谢
      • 创建一个项目,然后查看您的项目概览并点击“编辑项目”,您将在其中找到详细说明。
      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2010-11-21
      相关资源
      最近更新 更多