使用 OctoberCMS 的任务调度日志
Task Scheduling Logs with OctoberCMS
我正在使用 OctoberCMS based on Laravel。
我在数据库中有记录 table vendor_log_
.
每天我都需要 Laravel 将新创建的记录列表保存到一个带日期的 .log 文件中。
在我的自定义组件的 Plugin.php
文件中,我创建了一个 registerSchedule
函数。我有 get()
当前日期 table 中的记录,然后使用 file_put_contents
将它们附加到 .log 文件。
但是,没有日志文件被保存到我的日志服务器路径。我无法判断时间表是否未触发或它可能是什么,没有错误。
public function registerSchedule($schedule)
{
# Generate Daily Log File
$schedule->call(function () {
# Get Today's Date
$date = date("Y-m-d");
# Get Records by Today's Date
# Match Record created_at date, exclude time
$records = \Db::table('vendor_log_')
->whereDay('created_at', date('d'))
->whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
->get();
# Write records to log file
foreach ($records as $line) {
file_put_contents("/var/www/mysite/logs/$date.log", $line . PHP_EOL, FILE_APPEND);
}
})->everyMinute();
}
$line
的输出:
stdClass Object ( [user_id] => 1 [id] => 28 [username] => [name] => test [created_at] => 2017-03-22 02:39:13 )
注意:我使用 ->everyMinute()
代替 ->daily()
来更快地生成日志以进行测试。
文档:
https://octobercms.com/docs/plugin/scheduling#defining-schedules
https://laravel.com/docs/5.4/scheduling#schedule-frequency-options
问题总结及评论:
问题由两部分组成;
- 保存日志的位置用户不可写
- DB返回的对象class不能写成字符串
第一个问题已通过将日志文件写入所用主机的 public 文件夹解决。
将DB对象写入字符串可以通过以下代码完成
foreach($records as $record) {
$array = (array) $record;
$string = '';
foreach($record as $key => $value) {
$string .= $key .' '. $value . ',';
}
// strip the trailing comma of the end of the string
$line = rtrim($string, ',');
file_put_contents("/link/to/public/$date.log", $line . PHP_EOL, FILE_APPEND);
}
我正在使用 OctoberCMS based on Laravel。
我在数据库中有记录 table vendor_log_
.
每天我都需要 Laravel 将新创建的记录列表保存到一个带日期的 .log 文件中。
在我的自定义组件的 Plugin.php
文件中,我创建了一个 registerSchedule
函数。我有 get()
当前日期 table 中的记录,然后使用 file_put_contents
将它们附加到 .log 文件。
但是,没有日志文件被保存到我的日志服务器路径。我无法判断时间表是否未触发或它可能是什么,没有错误。
public function registerSchedule($schedule)
{
# Generate Daily Log File
$schedule->call(function () {
# Get Today's Date
$date = date("Y-m-d");
# Get Records by Today's Date
# Match Record created_at date, exclude time
$records = \Db::table('vendor_log_')
->whereDay('created_at', date('d'))
->whereMonth('created_at', date('m'))
->whereYear('created_at', date('Y'))
->get();
# Write records to log file
foreach ($records as $line) {
file_put_contents("/var/www/mysite/logs/$date.log", $line . PHP_EOL, FILE_APPEND);
}
})->everyMinute();
}
$line
的输出:
stdClass Object ( [user_id] => 1 [id] => 28 [username] => [name] => test [created_at] => 2017-03-22 02:39:13 )
注意:我使用 ->everyMinute()
代替 ->daily()
来更快地生成日志以进行测试。
文档:
https://octobercms.com/docs/plugin/scheduling#defining-schedules
https://laravel.com/docs/5.4/scheduling#schedule-frequency-options
问题总结及评论:
问题由两部分组成;
- 保存日志的位置用户不可写
- DB返回的对象class不能写成字符串
第一个问题已通过将日志文件写入所用主机的 public 文件夹解决。
将DB对象写入字符串可以通过以下代码完成
foreach($records as $record) {
$array = (array) $record;
$string = '';
foreach($record as $key => $value) {
$string .= $key .' '. $value . ',';
}
// strip the trailing comma of the end of the string
$line = rtrim($string, ',');
file_put_contents("/link/to/public/$date.log", $line . PHP_EOL, FILE_APPEND);
}