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' => drush_drupal_major_version() >= 6 ? array('dblog') : array('watchdog'),
  22. 'aliases' => array('wd-list'),
  23. );
  24. $items['watchdog-show'] = array(
  25. 'description' => 'Show watchdog messages.',
  26. 'drupal dependencies' => drush_drupal_major_version() >= 6 ? array('dblog') : array('watchdog'),
  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' => drush_drupal_major_version() >= 6 ? array('dblog') : array('watchdog'),
  53. 'arguments' => 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. 'deprecated-aliases' => array('wd'),
  66. );
  67. return $items;
  68. }
  69. /**
  70. * Command callback.
  71. */
  72. function drush_core_watchdog_list() {
  73. $options['-- types --'] = dt('== message types ==');
  74. $types = core_watchdog_message_types();
  75. foreach ($types as $type) {
  76. $options[] = $type;
  77. }
  78. $options['-- levels --'] = dt('== severity levels ==');
  79. drush_include_engine('drupal', 'environment');
  80. $severities = core_watchdog_severity_levels();
  81. foreach ($severities as $key => $value) {
  82. $options[] = "$value($key)";
  83. }
  84. $option = drush_choice($options, dt('Select a message type or severity level.'));
  85. if ($option === FALSE) {
  86. return drush_user_abort();
  87. }
  88. $ntypes = count($types);
  89. if ($option < $ntypes) {
  90. drush_set_option('type', $types[$option]);
  91. }
  92. else {
  93. drush_set_option('severity', $option - $ntypes);
  94. }
  95. drush_core_watchdog_show_many();
  96. }
  97. /**
  98. * Command callback.
  99. */
  100. function drush_core_watchdog_show($arg = NULL) {
  101. if (is_numeric($arg)) {
  102. drush_core_watchdog_show_one($arg);
  103. }
  104. else {
  105. drush_core_watchdog_show_many($arg);
  106. }
  107. }
  108. /**
  109. * Print a watchdog message.
  110. *
  111. * @param $wid
  112. * The id of the message to show.
  113. */
  114. function drush_core_watchdog_show_one($wid) {
  115. $rsc = drush_db_select('watchdog', '*', 'wid = :wid', array(':wid' => $wid), 0, 1);
  116. $result = drush_db_fetch_object($rsc);
  117. if (!$result) {
  118. return drush_set_error(dt('Watchdog message #!wid not found.', array('!wid' => $wid)));
  119. }
  120. $result = core_watchdog_format_result($result, TRUE);
  121. foreach ($result as $key => $value) {
  122. $uc = ucfirst($key);
  123. $upper->$uc = $value;
  124. }
  125. drush_print_table(drush_key_value_to_array_table($upper));
  126. print "\n";
  127. }
  128. /**
  129. * Print a table of watchdog messages.
  130. *
  131. * @param $filter
  132. * String to filter the message's text by.
  133. */
  134. function drush_core_watchdog_show_many($filter = NULL) {
  135. $count = drush_get_option('count', 10);
  136. $type = drush_get_option('type');
  137. $severity = drush_get_option('severity');
  138. $tail = drush_get_option('tail', FALSE);
  139. $full = drush_get_option('full', FALSE);
  140. $where = core_watchdog_query($type, $severity, $filter);
  141. if ($where === FALSE) {
  142. return drush_log(dt('Aborting.'));
  143. }
  144. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], 0, $count, 'wid', 'DESC');
  145. if ($rsc === FALSE) {
  146. return drush_log(dt('Aborting.'));
  147. }
  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)) {
  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. // We will 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: remove the header and reset the rows on each iteration.
  167. $tbl->_headers = NULL;
  168. // Obtain the last wid.
  169. $last = array_pop($table);
  170. $last_wid = $last[0];
  171. // Adapt the where snippet.
  172. if ($where['where'] != '') {
  173. $where['where'] .= ' AND ';
  174. }
  175. $where['where'] .= 'wid > :wid';
  176. // sleep-delay
  177. $sleep_delay = drush_get_option('sleep-delay', 1);
  178. while (TRUE) {
  179. $where['args'][':wid'] = $last_wid;
  180. $table = array();
  181. // Reset table rows.
  182. $tbl->_data = array();
  183. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], NULL, NULL, 'wid', 'ASC');
  184. while ($result = drush_db_fetch_object($rsc)) {
  185. $row = core_watchdog_format_result($result, $full);
  186. $table[] = array($row->wid, $row->date, $row->severity, $row->type, $row->message);
  187. #$tbl->addRow(array($row->wid, $row->date, $row->severity, $row->type, $row->message));
  188. $last_wid = $row->wid;
  189. }
  190. $tbl->addData($table);
  191. print $tbl->_buildTable();
  192. sleep($sleep_delay);
  193. }
  194. }
  195. else {
  196. print "\n";
  197. }
  198. }
  199. /**
  200. * Format a watchdog database row.
  201. *
  202. * @param $result
  203. * Array. A database result object.
  204. * @param $full
  205. * Boolean. Return extended message details.
  206. * @return
  207. * Array. The result object with some attributes themed.
  208. */
  209. function core_watchdog_format_result($result, $full = FALSE) {
  210. // Severity.
  211. drush_include_engine('drupal', 'environment');
  212. $severities = core_watchdog_severity_levels();
  213. $result->severity = $severities[$result->severity];
  214. // Date.
  215. $result->date = format_date($result->timestamp, 'custom', 'd/M H:i');
  216. unset($result->timestamp);
  217. // Message.
  218. if (drush_drupal_major_version() > 5) {
  219. $variables = unserialize($result->variables);
  220. if (is_array($variables)) {
  221. $result->message = strtr($result->message, $variables);
  222. }
  223. unset($result->variables);
  224. }
  225. $message_length = 188;
  226. // Print all the data available
  227. if ($full) {
  228. // Possible empty values.
  229. if (empty($result->link)) {
  230. unset($result->link);
  231. }
  232. if (empty($result->referer)) {
  233. unset($result->referer);
  234. }
  235. // Username.
  236. if ($account = user_load($result->uid)) {
  237. $result->username = $account->name;
  238. }
  239. else {
  240. $result->username = dt('Anonymous');
  241. }
  242. unset($result->uid);
  243. $message_length = PHP_INT_MAX;
  244. }
  245. $result->message = truncate_utf8(strip_tags(decode_entities($result->message)), $message_length, FALSE, FALSE);
  246. return $result;
  247. }
  248. /**
  249. * Command callback.
  250. *
  251. * @param $arg
  252. * The id of the message to delete or 'all'.
  253. */
  254. function drush_core_watchdog_delete($arg = NULL) {
  255. if ($arg == 'all') {
  256. drush_print(dt('All watchdog messages will be deleted.'));
  257. if (!drush_confirm(dt('Do you really want to continue?'))) {
  258. return drush_user_abort();
  259. }
  260. drush_db_delete('watchdog');
  261. drush_log(dt('All watchdog messages have been deleted.'), 'ok');
  262. }
  263. else if (is_numeric($arg)) {
  264. drush_print(dt('Watchdog message #!wid will be deleted.', array('!wid' => $arg)));
  265. if(!drush_confirm(dt('Do you really want to continue?'))) {
  266. return drush_user_abort();
  267. }
  268. $affected_rows = drush_db_delete('watchdog', 'wid=:wid', array(':wid' => $arg));
  269. if ($affected_rows == 1) {
  270. drush_log(dt('Watchdog message #!wid has been deleted.', array('!wid' => $arg)), 'ok');
  271. }
  272. else {
  273. return drush_set_error(dt('Watchdog message #!wid does not exist.', array('!wid' => $arg)));
  274. }
  275. }
  276. else {
  277. $type = drush_get_option('type');
  278. $severity = drush_get_option('severity');
  279. if ((is_null($arg))&&(is_null($type))&&(is_null($severity))) {
  280. return drush_set_error(dt('No options provided.'));
  281. }
  282. $where = core_watchdog_query($type, $severity, $arg, 'OR');
  283. if ($where === FALSE) {
  284. // Drush set error was already called by core_watchdog_query
  285. return FALSE;
  286. }
  287. 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'])))));
  288. if(!drush_confirm(dt('Do you really want to continue?'))) {
  289. return drush_user_abort();
  290. }
  291. $affected_rows = drush_db_delete('watchdog', $where['where'], $where['args']);
  292. drush_log(dt('!affected_rows watchdog messages have been deleted.', array('!affected_rows' => $affected_rows)), 'ok');
  293. }
  294. }
  295. /**
  296. * Build a WHERE snippet based on given parameters.
  297. *
  298. * @param $type
  299. * String. Valid watchdog type.
  300. * @param $severity
  301. * Int or String for a valid watchdog severity message.
  302. * @param $filter
  303. * String. Value to filter watchdog messages by.
  304. * @param $criteria
  305. * ('AND', 'OR'). Criteria for the WHERE snippet.
  306. * @return
  307. * False or array with structure ('where' => string, 'args' => array())
  308. */
  309. function core_watchdog_query($type = NULL, $severity = NULL, $filter = NULL, $criteria = 'AND') {
  310. $args = array();
  311. $conditions = array();
  312. if ($type) {
  313. $types = core_watchdog_message_types();
  314. if (array_search($type, $types) === FALSE) {
  315. $msg = "Unrecognized message type: !type.\nRecognized types are: !types.";
  316. return drush_set_error('WATCHDOG_UNRECOGNIZED_TYPE', dt($msg, array('!type' => $type, '!types' => implode(', ', $types))));
  317. }
  318. $conditions[] = "type = :type";
  319. $args[':type'] = $type;
  320. }
  321. if (!is_null($severity)) {
  322. drush_include_engine('drupal', 'environment');
  323. $severities = core_watchdog_severity_levels();
  324. if (isset($severities[$severity])) {
  325. $level = $severity;
  326. }
  327. elseif (($key = array_search($severity, $severities)) !== FALSE) {
  328. $level = $key;
  329. }
  330. else {
  331. $level = FALSE;
  332. }
  333. if ($level === FALSE) {
  334. foreach ($severities as $key => $value) {
  335. $levels[] = "$value($key)";
  336. }
  337. $msg = "Unknown severity level: !severity.\nValid severity levels are: !levels.";
  338. return drush_set_error(dt($msg, array('!severity' => $severity, '!levels' => implode(', ', $levels))));
  339. }
  340. $conditions[] = 'severity = :severity';
  341. $args[':severity'] = $level;
  342. }
  343. if ($filter) {
  344. $conditions[] = "message LIKE :filter";
  345. $args[':filter'] = '%'.$filter.'%';
  346. }
  347. $where = implode(" $criteria ", $conditions);
  348. return array('where' => $where, 'args' => $args);
  349. }
  350. /**
  351. * Helper function to obtain the message types based on drupal version.
  352. *
  353. * @return
  354. * Array of watchdog message types.
  355. */
  356. function core_watchdog_message_types() {
  357. if (drush_drupal_major_version() == 5) {
  358. return _watchdog_get_message_types();
  359. }
  360. else {
  361. return _dblog_get_message_types();
  362. }
  363. }