How to log our own error messages(for ex: error due to invalid user date entry) which is generated in php program to drupal error log.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Both
watchdog
for D7 &\Drupal::logger
for D8 will write log inwatchdog
table (in your database), and with HUGE data logged, you can imagine performance impact.You can use
error_log
php function to do it (see PHP manual).drupal-8drupalphplog
1) Indeed, watchdog is a standard way to record own PHP errors.
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 - Devel module, Firebug extension to FireFox and possibly Firephp.
You can use the dfb() function to write log messages directly to the general Firebug console.
If you want to keep your Drupal-related log messages out of the normal Firebug console, you can write messages to the Drupal for Firebug log with the firep() function:
Drupal 8
See more examples at How to Log Messages in Drupal 8.
In drupal 7 we can log message by following method:
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.
$type: The category to which this message belongs , Example: PHP,cron.., we can filter message by type.
$message : The message to store in the log,Example: 'The following module is missing from the file system: 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.
Example: watchdog('cg_volunteer', 'cg in form_alter %formly', array('%formly' => $form['#id']), WATCHDOG_NOTICE, $link = NULL);
$severity The 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
$link: A link to associate with the message.
Example
// for logs notices
// for Loging Error
In drupal 8 we used following method:
// For Logs a notice.
// For Logs an error.
// For Alert, action must be taken immediately.
// For Critical message.
// For Debug-level messages.
//For Emergency, system is unusable.
//For Warning
//For Informational messages.
Also for translate we should not use t() function.
this will be translated on run time.
Example :
Watchdog is the way to go for a production system no doubt but during debugging I find the
drupal_set_message
function useful.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).
http://api.drupal.org/api/function/drupal_set_message/6
Source