php 如何在drupal中记录错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1705840/
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
How to log error message in drupal
提问by ArK
How to log our own error messages(for ex: error due to invalid user date entry) which is generated in phpprogram to drupal error log.
如何将在php程序中生成的我们自己的错误消息(例如:由于无效的用户日期输入而导致的错误)记录到drupal 错误日志中。
回答by Pascal MARTIN
You can use the watchdogfunction:
您可以使用该watchdog功能:
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
Quoting the manual, the parameters are :
引用手册,参数是:
$typeThe category to which this message belongs.$messageThe message to store in the log.$variablesArray of variables to replace in the message on display or NULL if message is already translated or not possible to translate.$severityThe severity of the message, as per RFC 3164$linkA link to associate with the message.
$type此消息所属的类别。$message要存储在日志中的消息。$variables要在显示的消息中替换的变量数组,如果消息已翻译或无法翻译,则为 NULL。$severity消息的严重性,根据 RFC 3164$link与消息关联的链接。
And the error levels can be found on the page of watchdog_severity_levels. For an error, you'll most probably use WATCHDOG_ERROR, or maybe even something more "critical", depending on the kind of error.
错误级别可以在 的页面上找到watchdog_severity_levels。对于错误,您很可能会使用WATCHDOG_ERROR,甚至可能使用更“关键”的东西,具体取决于错误的类型。
回答by milkovsky
Drupal 8
Drupal 8
// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);
See more examples at How to Log Messages in Drupal 8.
请参阅如何在 Drupal 8 中记录消息中的更多示例。
回答by AlexA
1) Indeed, watchdog is a standard way to record own PHP errors.
1)确实,看门狗是记录自己的PHP错误的标准方式。
2) Alternatively, if you need to immediately see error messages while debugging your Drupal pages, you may want to see them logged/printed right at the related page - in FireBug console. Sometimes is this very convenient when you can see page-related just-in-time logs. This requires - Develmodule, Firebugextension to FireFox and possibly Firephp.
2) 或者,如果您需要在调试 Drupal 页面时立即查看错误消息,您可能希望在相关页面上查看它们的记录/打印 - 在 FireBug 控制台中。有时,当您可以查看与页面相关的即时日志时,这会非常方便。这就要求-杰韦利模块,Firebug的扩展,Firefox和可能Firephp。
You can use the dfb() function to write log messages directly to the general Firebug console.
您可以使用 dfb() 函数将日志消息直接写入通用 Firebug 控制台。
dfb($input, $label = NULL)
If you want to keep your Drupal-related log messages out of the normal Firebug console, you can write messages to the Drupal for Firebuglog with the firep() function:
如果您想将与 Drupal 相关的日志消息保留在普通 Firebug 控制台之外,您可以使用 firep() 函数将消息写入Drupal for Firebug日志:
firep($item, $optional_title)
回答by DilbertDave
Watchdog is the way to go for a production system no doubt but during debugging I find the drupal_set_messagefunction useful.
毫无疑问,看门狗是生产系统的必经之路,但在调试过程中,我发现该drupal_set_message功能很有用。
It outputs the message to the screen where the 'Operation Successful'-type messages are normally displayed (so make sure you remove them before making the site Live).
它将消息输出到通常显示“操作成功”类型消息的屏幕(因此请确保在使站点上线之前将其删除)。
回答by SynapseIndia
In drupal 7 we can log message by following method:
在 drupal 7 中,我们可以通过以下方法记录消息:
drupal watchdog function we can use to log message in database , make sure we have enabled optional core module for Database Logging at /admin/build/modules.
我们可以使用 drupal 看门狗功能在数据库中记录消息,确保我们在 /admin/build/modules 中启用了可选的数据库日志记录核心模块。
watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)
$type:The category to which this message belongs , Example: PHP,cron.., we can filter message by type.
$type:此消息所属的类别,例如:PHP,cron..,我们可以按类型过滤消息。
$message :The message to store in the log,Example: 'The following module is missing from the file system: security_review'
$message :要存储在日志中的消息,例如:'文件系统中缺少以下模块:security_review'
$variables :Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate. to make message translated , do not pass dynamic value pass variables in the message should be added by using placeholder strings.
$variables :要在显示的消息中替换的变量数组,如果消息已经翻译或无法翻译,则为 NULL。要使消息翻译,不要在消息中传递动态值传递变量,应使用占位符字符串添加。
Example:watchdog('cg_volunteer', 'cg in form_alter %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
示例:watchdog('cg_volunteer', 'cg in form_alter %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
$severityThe severity of the message,logs can be filter by severity as per RFC 3164. Possible values are WATCHDOG_ERROR, WATCHDOG_WARNING, etc. For more example see https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/watchdog/7.x
$severity消息的严重性,日志可以根据 RFC 3164 的严重性进行过滤。可能的值为 WATCHDOG_ERROR、WATCHDOG_WARNING 等。更多示例请参见https://api.drupal.org/api/drupal/includes!bootstrap。 inc/function/watchdog/7.x
$link: A link to associate with the message.
$link:与消息关联的链接。
Example
例子
// for logs notices
// 用于日志通知
watchdog('my_module', $message, array());
// for Loging Error
// 用于记录错误
watchdog('my_module', $message, array(), WATCHDOG_ERROR);
In drupal 8 we used following method:
在 drupal 8 中,我们使用了以下方法:
// For Logs a notice.
// 对于日志通知。
\Drupal::logger('my_module')->notice($message);
// For Logs an error.
// 对于日志错误。
\Drupal::logger('my_module')->error($message);
// For Alert, action must be taken immediately.
// 对于警报,必须立即采取行动。
\Drupal::logger('my_module')->alert($message);
// For Critical message.
// 对于关键消息。
\Drupal::logger('my_module')->critical($message);
// For Debug-level messages.
// 对于调试级别的消息。
\Drupal::logger('my_module')->debug($message);
//For Emergency, system is unusable.
//对于紧急情况,系统不可用。
\Drupal::logger('my_module')->emergency($message);
//For Warning
//用于警告
\Drupal::logger('my_module')->warning($message);
//For Informational messages.
//对于信息性消息。
\Drupal::logger('my_module')->info($message);
Also for translate we should not use t() function.
同样对于翻译,我们不应该使用 t() 函数。
\Drupal::logger('my_module')->alert('Message from @module: @message.', [
'@module' => $module,
'@message' => $message,
]);
this will be translated on run time.
这将在运行时进行翻译。
Example :
例子 :
\Drupal::logger('content_entity_example')->notice('@type: deleted %title.',
array(
'@type' => $this->entity->bundle(),
'%title' => $this->entity->label(),
));
回答by kaay
Both watchdogfor D7 & \Drupal::loggerfor D8 will write log in watchdogtable (in your database), and with HUGE data logged, you can imagine performance impact.
无论watchdog对于D7及\Drupal::logger为D8会写日志watchdog表(数据库),并与庞大的数据记录,你能想象的性能影响。
You can use error_logphp function to do it (see PHP manual).
您可以使用error_logphp 函数来做到这一点(请参阅PHP 手册)。
error_log("Your message", 3, "/path/to/your/log/file.log");
You need to have permission to write in your log file (
/path/to/your/log/file.log)
您需要具有写入日志文件的权限 (
/path/to/your/log/file.log)

