watchdog.drush.inc

  1. 8.0.x commands/core/watchdog.drush.inc
  2. 6.x commands/core/watchdog.drush.inc
  3. 7.x commands/core/watchdog.drush.inc
  4. 3.x commands/core/watchdog.drush.inc
  5. 4.x commands/core/watchdog.drush.inc
  6. 5.x commands/core/watchdog.drush.inc
  7. master commands/core/watchdog.drush.inc

Functions

Namesort descending Description
core_watchdog_format_result Format a watchdog database row.
core_watchdog_message_types Helper function to obtain the message types based on drupal version.
core_watchdog_query Build a WHERE snippet based on given parameters.
drush_core_watchdog_delete Command callback.
drush_core_watchdog_list Command callback.
drush_core_watchdog_show Command callback.
drush_core_watchdog_show_many Print a table of watchdog messages.
drush_core_watchdog_show_one Print a watchdog message.
watchdog_drush_command Implementation of hook_drush_command().
watchdog_drush_help Implementation of hook_drush_help().

File

commands/core/watchdog.drush.inc
View source
  1. <?php
  2. /**
  3. * Implementation of hook_drush_help().
  4. */
  5. function watchdog_drush_help($section) {
  6. switch ($section) {
  7. case 'drush:watchdog-list':
  8. return dt('Show available message types and severity levels. A prompt will ask for a choice to show watchdog messages.');
  9. case 'drush:watchdog-show':
  10. return dt('Show watchdog messages. Arguments and options can be combined to configure which messages to show.');
  11. case 'drush:watchdog-delete':
  12. return dt('Delete watchdog messages. Arguments or options must be provided to specify which messages to delete.');
  13. }
  14. }
  15. /**
  16. * Implementation of hook_drush_command().
  17. */
  18. function watchdog_drush_command() {
  19. $items['watchdog-list'] = array(
  20. 'description' => 'Show available message types and severity levels. A prompt will ask for a choice to show watchdog messages.',
  21. 'drupal dependencies' => array('dblog'),
  22. 'aliases' => array('wd-list'),
  23. );
  24. $items['watchdog-show'] = array(
  25. 'description' => 'Show watchdog messages.',
  26. 'drupal dependencies' => array('dblog'),
  27. 'arguments' => array(
  28. 'wid' => 'Optional id of a watchdog message to show in detail. If not provided, a listing of most recent 10 messages will be displayed. Alternatively if a string is provided, watchdog messages will be filtered by it.',
  29. ),
  30. 'options' => array(
  31. 'count' => 'The number of messages to show. Defaults to 10.',
  32. 'severity' => 'Restrict to messages of a given severity level.',
  33. 'type' => 'Restrict to messages of a given type.',
  34. 'tail' => 'Continuously show new watchdog messages until interrupted.',
  35. 'sleep-delay' => 'To be used in conjunction with --tail. This is the number of seconds to wait between each poll to the database. Delay is 1 second by default.',
  36. 'full' => 'Return extended information about each message',
  37. ),
  38. 'examples' => array(
  39. 'drush watchdog-show' => 'Show a listing of most recent 10 messages.',
  40. 'drush watchdog-show 64' => 'Show in detail message with id 64.',
  41. 'drush watchdog-show "cron run succesful"' => 'Show a listing of most recent 10 messages containing the string "cron run succesful".',
  42. 'drush watchdog-show --count=46' => 'Show a listing of most recent 46 messages.',
  43. 'drush watchdog-show --severity=notice' => 'Show a listing of most recent 10 messages with a severity of notice.',
  44. 'drush watchdog-show --type=php' => 'Show a listing of most recent 10 messages of type php.',
  45. 'drush watchdog-show --tail --full' => 'Show a listing of most recent 10 messages with extended information about each one and continue showing messages as they are registered in the watchdog.',
  46. 'drush watchdog-show --tail --sleep-delay=2' => 'Do a tail of the watchdog with a delay of two seconds between each poll to the database.',
  47. ),
  48. 'aliases' => array('wd-show', 'ws'),
  49. );
  50. $items['watchdog-delete'] = array(
  51. 'description' => 'Delete watchdog messages.',
  52. 'drupal dependencies' => array('dblog'),
  53. 'options' => array(
  54. 'severity' => 'Delete messages of a given severity level.',
  55. 'type' => 'Delete messages of a given type.',
  56. ),
  57. 'examples' => array(
  58. 'drush watchdog-delete all' => 'Delete all messages.',
  59. 'drush watchdog-delete 64' => 'Delete messages with id 64.',
  60. 'drush watchdog-delete "cron run succesful"' => 'Delete messages containing the string "cron run succesful".',
  61. 'drush watchdog-delete --severity=notice' => 'Delete all messages with a severity of notice.',
  62. 'drush watchdog-delete --type=cron' => 'Delete all messages of type cron.',
  63. ),
  64. 'aliases' => array('wd-del', 'wd-delete'),
  65. );
  66. return $items;
  67. }
  68. /**
  69. * Command callback.
  70. */
  71. function drush_core_watchdog_list() {
  72. $options['-- types --'] = dt('== message types ==');
  73. $types = core_watchdog_message_types();
  74. foreach ($types as $type) {
  75. $options[] = $type;
  76. }
  77. $options['-- levels --'] = dt('== severity levels ==');
  78. drush_include_engine('drupal', 'environment');
  79. $severities = core_watchdog_severity_levels();
  80. foreach ($severities as $key => $value) {
  81. $options[] = "$value($key)";
  82. }
  83. $option = drush_choice($options, dt('Select a message type or severity level.'));
  84. if ($option === FALSE) {
  85. return drush_user_abort();
  86. }
  87. $ntypes = count($types);
  88. if ($option < $ntypes) {
  89. drush_set_option('type', $types[$option]);
  90. }
  91. else {
  92. drush_set_option('severity', $option - $ntypes);
  93. }
  94. drush_core_watchdog_show_many();
  95. }
  96. /**
  97. * Command callback.
  98. */
  99. function drush_core_watchdog_show($arg = NULL) {
  100. if (is_numeric($arg)) {
  101. drush_core_watchdog_show_one($arg);
  102. }
  103. else {
  104. drush_core_watchdog_show_many($arg);
  105. }
  106. }
  107. /**
  108. * Print a watchdog message.
  109. *
  110. * @param $wid
  111. * The id of the message to show.
  112. */
  113. function drush_core_watchdog_show_one($wid) {
  114. $rsc = drush_db_select('watchdog', '*', 'wid = :wid', array(':wid' => $wid), 0, 1);
  115. $result = drush_db_fetch_object($rsc);
  116. if (!$result) {
  117. return drush_set_error(dt('Watchdog message #!wid not found.', array('!wid' => $wid)));
  118. }
  119. $result = core_watchdog_format_result($result, TRUE);
  120. foreach ($result as $key => $value) {
  121. $uc = ucfirst($key);
  122. $upper->$uc = $value;
  123. }
  124. drush_print_table(drush_key_value_to_array_table($upper));
  125. print "\n";
  126. }
  127. /**
  128. * Print a table of watchdog messages.
  129. *
  130. * @param $filter
  131. * String to filter the message's text by.
  132. */
  133. function drush_core_watchdog_show_many($filter = NULL) {
  134. $count = drush_get_option('count', 10);
  135. $type = drush_get_option('type');
  136. $severity = drush_get_option('severity');
  137. $tail = drush_get_option('tail', FALSE);
  138. $full = drush_get_option('full', FALSE);
  139. $where = core_watchdog_query($type, $severity, $filter);
  140. if ($where === FALSE) {
  141. return drush_log(dt('Aborting.'));
  142. }
  143. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], 0, $count, 'wid', 'DESC');
  144. if ($rsc === FALSE) {
  145. return drush_log(dt('Aborting.'));
  146. }
  147. $table = array();
  148. $header = array(dt('Id'), dt('Date'), dt('Severity'), dt('Type'), dt('Message'));
  149. while ($result = drush_db_fetch_object($rsc)) {
  150. $row = core_watchdog_format_result($result, $full);
  151. $table[] = array($row->wid, $row->date, $row->severity, $row->type, $row->message);
  152. }
  153. if (empty($table) && !$tail) {
  154. return drush_log(dt('No log messages available.'), 'ok');
  155. }
  156. else {
  157. drush_log(dt('Most recent !count watchdog log messages:', array('!count' => $count)));
  158. if ($tail) {
  159. $table = array_reverse($table);
  160. }
  161. array_unshift($table, $header);
  162. $tbl = drush_print_table($table, TRUE);
  163. }
  164. if ($tail) {
  165. // Reuse the table object to display each line generated while in tail mode.
  166. // To make it possible some hacking is done on the object:
  167. // remove the header and reset the rows on each iteration.
  168. $tbl->_headers = NULL;
  169. // Obtain the last wid. If the table has no rows, start at 0.
  170. if (count($table) > 1) {
  171. $last = array_pop($table);
  172. $last_wid = $last[0];
  173. }
  174. else {
  175. $last_wid = 0;
  176. }
  177. // Adapt the where snippet.
  178. if ($where['where'] != '') {
  179. $where['where'] .= ' AND ';
  180. }
  181. $where['where'] .= 'wid > :wid';
  182. // sleep-delay
  183. $sleep_delay = drush_get_option('sleep-delay', 1);
  184. while (TRUE) {
  185. $where['args'][':wid'] = $last_wid;
  186. $table = array();
  187. // Reset table rows.
  188. $tbl->_data = array();
  189. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], NULL, NULL, 'wid', 'ASC');
  190. while ($result = drush_db_fetch_object($rsc)) {
  191. $row = core_watchdog_format_result($result, $full);
  192. $table[] = array($row->wid, $row->date, $row->severity, $row->type, $row->message);
  193. #$tbl->addRow(array($row->wid, $row->date, $row->severity, $row->type, $row->message));
  194. $last_wid = $row->wid;
  195. }
  196. $tbl->addData($table);
  197. print $tbl->_buildTable();
  198. sleep($sleep_delay);
  199. }
  200. }
  201. else {
  202. print "\n";
  203. }
  204. }
  205. /**
  206. * Format a watchdog database row.
  207. *
  208. * @param $result
  209. * Array. A database result object.
  210. * @param $full
  211. * Boolean. Return extended message details.
  212. * @return
  213. * Array. The result object with some attributes themed.
  214. */
  215. function core_watchdog_format_result($result, $full = FALSE) {
  216. // Severity.
  217. drush_include_engine('drupal', 'environment');
  218. $severities = core_watchdog_severity_levels();
  219. $result->severity = $severities[$result->severity];
  220. // Date.
  221. $result->date = format_date($result->timestamp, 'custom', 'd/M H:i');
  222. unset($result->timestamp);
  223. // Message.
  224. $variables = $result->variables;
  225. if (is_string($variables)) {
  226. $variables = unserialize($variables);
  227. }
  228. if (is_array($variables)) {
  229. $result->message = strtr($result->message, $variables);
  230. }
  231. unset($result->variables);
  232. $message_length = 188;
  233. // Print all the data available
  234. if ($full) {
  235. // Possible empty values.
  236. if (empty($result->link)) {
  237. unset($result->link);
  238. }
  239. if (empty($result->referer)) {
  240. unset($result->referer);
  241. }
  242. // Username.
  243. if ($account = user_load($result->uid)) {
  244. $result->username = $account->name;
  245. }
  246. else {
  247. $result->username = dt('Anonymous');
  248. }
  249. unset($result->uid);
  250. $message_length = PHP_INT_MAX;
  251. }
  252. $result->message = truncate_utf8(strip_tags(decode_entities($result->message)), $message_length, FALSE, FALSE);
  253. return $result;
  254. }
  255. /**
  256. * Command callback.
  257. *
  258. * @param $arg
  259. * The id of the message to delete or 'all'.
  260. */
  261. function drush_core_watchdog_delete($arg = NULL) {
  262. if ($arg == 'all') {
  263. drush_print(dt('All watchdog messages will be deleted.'));
  264. if (!drush_confirm(dt('Do you really want to continue?'))) {
  265. return drush_user_abort();
  266. }
  267. drush_db_delete('watchdog');
  268. drush_log(dt('All watchdog messages have been deleted.'), 'ok');
  269. }
  270. else if (is_numeric($arg)) {
  271. drush_print(dt('Watchdog message #!wid will be deleted.', array('!wid' => $arg)));
  272. if(!drush_confirm(dt('Do you really want to continue?'))) {
  273. return drush_user_abort();
  274. }
  275. $affected_rows = drush_db_delete('watchdog', 'wid=:wid', array(':wid' => $arg));
  276. if ($affected_rows == 1) {
  277. drush_log(dt('Watchdog message #!wid has been deleted.', array('!wid' => $arg)), 'ok');
  278. }
  279. else {
  280. return drush_set_error(dt('Watchdog message #!wid does not exist.', array('!wid' => $arg)));
  281. }
  282. }
  283. else {
  284. $type = drush_get_option('type');
  285. $severity = drush_get_option('severity');
  286. if ((is_null($arg))&&(is_null($type))&&(is_null($severity))) {
  287. return drush_set_error(dt('No options provided.'));
  288. }
  289. $where = core_watchdog_query($type, $severity, $arg, 'OR');
  290. if ($where === FALSE) {
  291. // Drush set error was already called by core_watchdog_query
  292. return FALSE;
  293. }
  294. drush_print(dt('All messages with !where will be deleted.', array('!where' => preg_replace("/message LIKE %$arg%/", "message body containing '$arg'" , strtr($where['where'], $where['args'])))));
  295. if(!drush_confirm(dt('Do you really want to continue?'))) {
  296. return drush_user_abort();
  297. }
  298. $affected_rows = drush_db_delete('watchdog', $where['where'], $where['args']);
  299. drush_log(dt('!affected_rows watchdog messages have been deleted.', array('!affected_rows' => $affected_rows)), 'ok');
  300. }
  301. }
  302. /**
  303. * Build a WHERE snippet based on given parameters.
  304. *
  305. * @param $type
  306. * String. Valid watchdog type.
  307. * @param $severity
  308. * Int or String for a valid watchdog severity message.
  309. * @param $filter
  310. * String. Value to filter watchdog messages by.
  311. * @param $criteria
  312. * ('AND', 'OR'). Criteria for the WHERE snippet.
  313. * @return
  314. * False or array with structure ('where' => string, 'args' => array())
  315. */
  316. function core_watchdog_query($type = NULL, $severity = NULL, $filter = NULL, $criteria = 'AND') {
  317. $args = array();
  318. $conditions = array();
  319. if ($type) {
  320. $types = core_watchdog_message_types();
  321. if (array_search($type, $types) === FALSE) {
  322. $msg = "Unrecognized message type: !type.\nRecognized types are: !types.";
  323. return drush_set_error('WATCHDOG_UNRECOGNIZED_TYPE', dt($msg, array('!type' => $type, '!types' => implode(', ', $types))));
  324. }
  325. $conditions[] = "type = :type";
  326. $args[':type'] = $type;
  327. }
  328. if (!is_null($severity)) {
  329. drush_include_engine('drupal', 'environment');
  330. $severities = core_watchdog_severity_levels();
  331. if (isset($severities[$severity])) {
  332. $level = $severity;
  333. }
  334. elseif (($key = array_search($severity, $severities)) !== FALSE) {
  335. $level = $key;
  336. }
  337. else {
  338. $level = FALSE;
  339. }
  340. if ($level === FALSE) {
  341. foreach ($severities as $key => $value) {
  342. $levels[] = "$value($key)";
  343. }
  344. $msg = "Unknown severity level: !severity.\nValid severity levels are: !levels.";
  345. return drush_set_error(dt($msg, array('!severity' => $severity, '!levels' => implode(', ', $levels))));
  346. }
  347. $conditions[] = 'severity = :severity';
  348. $args[':severity'] = $level;
  349. }
  350. if ($filter) {
  351. $conditions[] = "message LIKE :filter";
  352. $args[':filter'] = '%'.$filter.'%';
  353. }
  354. $where = implode(" $criteria ", $conditions);
  355. return array('where' => $where, 'args' => $args);
  356. }
  357. /**
  358. * Helper function to obtain the message types based on drupal version.
  359. *
  360. * @return
  361. * Array of watchdog message types.
  362. */
  363. function core_watchdog_message_types() {
  364. return _dblog_get_message_types();
  365. }