Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 609 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to log error message in drupal

#1
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**.
Reply

#2
You can use the [`watchdog` function][1] :

watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL)

Quoting the manual, the parameters are :

- `$type` The category to which this message belongs.
- `$message` The message to store in the log.
- `$variables` Array of variables to replace in the message on display or NULL if message is already translated or not possible to translate.
- `$severity` The severity of the message, as per RFC 3164
- `$link` A link to associate with the message.

And the error levels can be found on the page of [`watchdog_severity_levels`][2]. For an error, you'll most probably use `WATCHDOG_ERROR`, or maybe even something more "critical", depending on the kind of error.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
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).

[To see links please register here]

Reply

#4
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][1] module, [Firebug][2] extension to FireFox and possibly [Firephp][3].

You can use the dfb() function to write log messages directly to the general Firebug console.

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 Firebug][4] log with the firep() function:

firep($item, $optional_title)


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

[padding]: padding_for_edit
Reply

#5
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.



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.

**$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

[To see links please register here]


**$link**:
A link to associate with the message.

**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:**

// 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.

\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(),
));

Reply

#6
Both `watchdog` for D7 & `\Drupal::logger` for D8 will write log in `watchdog` 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][1]).


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`)

[tag:drupal-8][tag:drupal][tag:php][tag:log]

[1]:

[To see links please register here]

Reply

#7
// Get logger factory.
$logger = \Drupal::service('logger.factory');

// Log a message with dynamic variables.
$nodeType = 'Article';
$userName = 'Admin';
$logger->get($moduleName)->notice('A new "@nodeType" created by %userName.', [
'@nodeType' => $nodeType,
'%userName' => $userName,
]);

[Source][1]


[1]:

[To see links please register here]

Reply

#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][1].


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through