【问题标题】:How can I write a custom function that will write error to my log file in Php?如何编写自定义函数,将错误写入 PHP 中的日志文件?
【发布时间】:2017-06-09 11:08:44
【问题描述】:

我正在做 Moodle 项目。我想有一种方法可以将我的错误写入 PHP 中的 Log.txt 文件。

下面是我的代码

drill_auto_enrolluser 类扩展 \core\task\scheduled_task{

public function execute() {

    global $DB;
    $name="";
    $description="";
    $descriptionformat ="";
    $userid="";
    $templateid="";
    $timecreated="";
    $timemodified=0;
    $origtemplateid=NULL;
    $status=0;
    $duedate=0;
    $reviewerid=NULL;     

         $DATA = $DB->get_recordset_sql ("Select name,description,descriptionformat,userid,templateid from vw_new_user_lp");

            foreach ($DATA as $id => $rec) {

                 $record = new \stdClass();                                          
                             $record ->name = $rec->name;
                             $record ->description= $rec->description;
                             $record ->descriptionformat=$rec->descriptionformat;
                             $record ->userid= $rec->userid;    
                             $record->origtemplateid=$origtemplateid;
                             $record ->templateid=$rec->templateid;
                             $record->status= $status;
                             $record->duedate= $duedate;
                             $record->reviewerid= $reviewerid;
                             $record->timecreated= time();
                             $record->timemodified = $timemodified;
                             $record->usermodified = $userid;

                $DB->insert_record('competency', $record);

      }

}

}

【问题讨论】:

  • 你试过什么?代码在哪里?
  • 嗨,我已经添加了我的 Moodle Php 代码。但我已经编写了任何错误记录代码。

标签: php logging moodle


【解决方案1】:

在这种情况下,您必须知道错误是如何报告的。这通常通过异常返回值来完成。在第一种情况下,如果没有捕获到异常,应用程序就会中止(这是理所当然的)。

例如,假设插入抛出了MoodleDatabaseException类的异常(当然,你需要检查究竟抛出了什么异常):

try {
    $DB->insert_record('competency', $record);
} catch (\MoodleDatabaseException $err) {
    myErrorLog($err->getMessage());
}

否则,如果函数返回例如真或假,你会去:

if (true !== $DB->insert_record('competency', $record)) {
    myErrorLog($DB->functionThatReturnsLastErrorMessage());
}

在这两种情况下,您都需要一个 myErrorLog 函数。我认为 Moodle 有一个日志记录工具和you may want to check it out,并且可能会问一个更详细的后续问题。您可能还决定使用the Apache error_log facility,具体取决于您的需要。在紧要关头,您可以使用文本日志文件:

function myErrorLog($message = 'no message') {
    $date = date('Y-m-d H:i:s');
    $fp   = fopen('/var/log/moodle-errors.log', 'a');
    if (!$fp) {
         throw new \Exception("Could not open log file! Permission error?");
    }
    fwrite($fp, $date . ' ' . $message . "\n");
    fclose($fp);
}

上面的代码不担心多用户锁定,这可能是有问题的。这有利于快速而肮脏的调试;否则你需要使用提供的任何功能(或者考虑集成 Log4Php 或 Monolog,也许;但这看起来是一个要求很高的项目)。

更新

由于我还不太清楚(英语不是我的母语),所以留下了 where 定义myErrorLog 函数以使其随处可用的问题,但永远不要定义两次以避免错误。

为了快速调试,您可以将error_log 替换为error_log,这将输出到Apache 错误日志文件,从而也避免了并发问题和权限问题。

【讨论】:

  • 感谢您的回复,我不想使用Modle错误记录功能。我会尝试您的自定义功能。如果有任何疑问可以在这里评论。
  • 谢谢,LSemi..你的英文单词很容易理解。我遇到权限问题。警告:fopen(/moodle_error.txt):打开流失败:/home/onlin200/ 中的权限被拒绝第 13 行的 public_html/mod/certificate/classes/task/errorlogging_function.php 计划任务失败:在能力课程中自动注册用户 (mod_certificate\task\drill_auto_enrolluser ),无法打开日志文件!权限错误?
  • 您正在写入/,Web服务器没有权限。尝试 /tmp/moodle-errors.txt 或在路径前添加一个点: ./moodle_error.txt .
  • 我已为所有必要的 PHP 文件设置了 777 权限。我会尝试您建议的解决方案。谢谢。
  • 我已经尝试了上述解决方案,但对我不起作用。没有运气。现在该怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 2019-04-21
  • 2013-07-19
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多