The currently available devel debug functions (dsm, dpm, dprint_r, etc...) allow you to print messages to the screen or using drupal_set_message, and styles them nicely. However, there are some situations where you need to print data to the dblog (using watchdog), for instance for scripts triggered by cron and run as the anonymous user. Currently the only way to do this is something like watchdog('debug', print_r($data, 1));.

It would be great if there was a devel function to do this more easily, and to style the resulting output (instead of getting gross print_r output).

Issue fork devel-989858

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Priority: Normal » Minor

i'm not aware of anything less gross. also note that we have dd() for writing to a log file.

Dane Powell’s picture

Hrm... I can't believe that no one else but me has this problem. How else do you get a pretty-printed array from a function that runs during cron, in a drush script, etc... ? Currently, I have to var_export to the dblog, then copy and paste the output into a PHP script that evals it and then reprints the array using Krumo. This is crazy, there should really be a more streamlined way of accomplishing this.

ergonlogic’s picture

@Dane Powell: You're not alone. I'd love to see a solution for this.

moshe weitzman’s picture

Status: Active » Closed (won't fix)

krumo is too huge to put into a watchdog msg. i think you have to write a function that will write the krumo output to a .html file. not likely to be added by drush core.

xurizaemon’s picture

Moshe is right that watchdog may get angry if you do this to it; even a relatively small form definition array will end up at around 100K when you've kprint_r()'d it.

I don't expect this to get into devel module, but I have been using this a little so I figured I'd sandbox it for others to try adding dpw() (Drupal Print Watchdog) to your toolkit. So here you go.

* http://drupal.org/sandbox/grobot/1124300

Dane Powell’s picture

Thanks for the info grobot- I totally understand why this would be a bad idea. But I will only ever be using this on a development site that will just get deleted afterwards anyway, so this would be very useful. Thanks!

Les Lim’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

It may be a bad idea to store large structures in {watchdog}, but I don't see any reason why we can't have a dedicated table for such things. I just committed a module with that in mind:

http://drupal.org/project/object_log

I've been using it to help debug a web service project for a couple weeks and it's been a great time-saver.

fureigh’s picture

Thanks for posting about that module, Les Lim.

pyry_p’s picture

As a workaround I stored the data in variable_set() and print at nice place.

xurizaemon’s picture

pyry_p variables table is a horrible place to be pooping random data, consider cache_set instead perhaps?

not that spamming the watchdog table is a good idea either, but variables table, no no no

pyry_p’s picture

Grobot: That is really alot better idea. I was doing variable_set in qa- and temp enviroments which i overwrite often. But with cache based variable I think possible ajax debugging at trunk and production will be lot safer. I will keep the variable table still "in the toolbox" tho if i need to build features that clear caches.

nicholas.alipaz’s picture

Issue summary: View changes

I really like the object_log module and wish it were part of devel, as it only makes sense to be here IMO. Thanks Les for writing the module.

A new dpl() function would be a wonderful way handle it. Devel Print Log

drupalfan79’s picture

show variables in devel way with watchdog:

watchdog('debug',kprint_r($var, TRUE, NULL));
Marko B’s picture

Very cool drupalfan79, thanx

kmajzlik’s picture

#13 fails on PHP 5.4 (can not add function call as parameter)

Improved code taken from http://cgit.drupalcode.org/sandbox-grobot-1124300/commit/?id=f6fe936 :

  $vars = array('my', 'variables');
  $name = 'vars';
  $export = kprint_r($vars, TRUE, $name);
  watchdog('debug', '!name: !export', array('!export' => $export, '!name' => isset($name) ? $name : 'Debug'), WATCHDOG_DEBUG);
rajiv.singh’s picture

Here is my handy solution to print array/object in watchdog item
in the style of devel debug functions (dsm, dpm, dprint_r, etc...)

function MYMODULE_preprocess(&$vars, $hook) {
  global $user;
  if(in_array('administrator', $user->roles) && $hook == 'page' && in_array('page__admin__reports__event__%', $vars['theme_hook_suggestions'])) {
    $message = $vars['page']['content']['system_main']['dblog_table']['#rows'][5][1];
    $message_array = json_decode(htmlspecialchars_decode ($message, ENT_QUOTES), true);
    if(!empty($message_array) && (is_array($message_array) || is_object($message_array)) && module_exists('devel')) {
      dsm($message_array);
    }
  }
}

How to use it ?

$var['debug_title'] = 'debugging user in module xyz at line No 1';
$var['data'] = user_load(1);
//Log the Array / Object directly into the watchdog
watchdog('devel debug','@message', array('@message' => json_encode($var)));

Now go to admin/reports/dblog and click the message of "devel debug" item

Output

rajiv.singh’s picture

gopisathya’s picture

#15 is working.

gravisrs’s picture

For smaller values you can print values to watchdog using pretty print json within

 tags:

json_encode($value, JSON_PRETTY_PRINT)

Which makes it easier if you read logs from database. The only disadvantage is lack of loop/same object detection.

metalbote made their first commit to this issue’s fork.