如何在 CodeIgniter (PHP) 中进行错误记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3209807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:00:38  来源:igfitidea点击:

How to do error logging in CodeIgniter (PHP)

phpcodeigniterloggingerror-handling

提问by udaya

I want error logging in PHP CodeIgniter. How do I enable error logging?

我想在 PHP CodeIgniter 中记录错误。如何启用错误日志记录?

I have some questions:

我有一些问题:

  1. What are all the steps to log an error?
  2. How is an error log file created?
  3. How to push the error message into log file (whenever an error occurs)?
  4. How do you e-mail that error to an email address?
  1. 记录错误的所有步骤是什么?
  2. 错误日志文件是如何创建的?
  3. 如何将错误消息推送到日志文件中(每当发生错误时)?
  4. 您如何将该错误通过电子邮件发送到电子邮件地址?

回答by Keyo

CodeIgniter has some error logging functions built in.

CodeIgniter 内置了一些错误记录功能。

  • Make your /application/logsfolder writable
  • In /application/config/config.phpset
    $config['log_threshold'] = 1;
    or use a higher number, depending on how much detail you want in your logs
  • Use log_message('error', 'Some variable did not contain a value.');
  • To send an email you need to extend the core CI_Exceptions class method log_exceptions(). You can do this yourself or use this. More info on extending the core here
  • 使您的/application/logs文件夹可写
  • /application/config/config.php 中设置
    $config['log_threshold'] = 1;
    或使用更高的数字,具体取决于您希望在日志中包含多少详细信息
  • log_message('error', 'Some variable did not contain a value.');
  • 要发送电子邮件,您需要扩展核心 CI_Exceptions 类方法log_exceptions()。您可以自己执行此操作或使用this。有关在此处扩展核心的更多信息

See http://www.codeigniter.com/user_guide/general/errors.html

http://www.codeigniter.com/user_guide/general/errors.html

回答by Mark Eirich

To simply put a line in the server's error log, use PHP's error_log() function. However, that method will not send an e-mail.

要简单地在服务器的错误日志中添加一行,请使用 PHP 的 error_log() 函数。但是,该方法不会发送电子邮件。

First, to trigger an error:

首先,触发一个错误:

trigger_error("Error message here", E_USER_ERROR);

By default, this will go in the server's error log file. See the ErrorLog directivefor Apache. To set your own log file:

默认情况下,这将进入服务器的错误日志文件。请参阅Apache的ErrorLog 指令。要设置您自己的日志文件:

ini_set('error_log', 'path/to/log/file');

Note that the log file you choose must already exist and be writable by the server process. The simplest way to make the file writable is to make the server user the owner of the file. (The server user may be nobody, _www, apache, or something else, depending on your OS distribution.)

请注意,您选择的日志文件必须已经存在并且可由服务器进程写入。使文件可写的最简单方法是使服务器用户成为文件的所有者。(服务器用户可能是 nobody、_www、apache 或其他东西,具体取决于您的操作系统发行版。)

To e-mail the error, you need to set up a custom error handler:

要通过电子邮件发送错误,您需要设置自定义错误处理程序:

function mail_error($errno, $errstr, $errfile, $errline) {
  $message = "[Error $errno] $errstr - Error on line $errline in file $errfile";
  error_log($message); // writes the error to the log file
  mail('[email protected]', 'I have an error', $message);
}
set_error_handler('mail_error', E_ALL^E_NOTICE);

Please see the relevant PHP documentationfor more info.

有关更多信息,请参阅相关 PHP 文档

回答by Gpak

Also make sure that you have allowed codeigniter to log the type of messages you want in a config file.

还要确保您已允许 codeigniter 在配置文件中记录您想要的消息类型。

i.e $config['log_threshold'] = [log_level ranges 0-4];

IE $config['log_threshold'] = [log_level ranges 0-4];

回答by Master James

More oin regards to question part 4 How do you e-mail that error to an email address? The error_log function has email destination too. http://php.net/manual/en/function.error-log.php

有关问题第 4 部分的更多信息,您如何将该错误通过电子邮件发送到电子邮件地址?error_log 函数也有电子邮件目的地。 http://php.net/manual/en/function.error-log.php

Agha, here I found an example that shows a usage. Send errors message via email using error_log()

啊哈,在这里我找到了一个显示用法的示例。 使用 error_log() 通过电子邮件发送错误消息

error_log($this->_errorMsg, 1, ADMIN_MAIL, "Content-Type: text/html; charset=utf8\r\nFrom: ".MAIL_ERR_FROM."\r\nTo: ".ADMIN_MAIL);