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. ),
  37. 'examples' => array(
  38. 'drush watchdog-show' => 'Show a listing of most recent 10 messages.',
  39. 'drush watchdog-show 64' => 'Show in detail message with id 64.',
  40. 'drush watchdog-show "cron run succesful"' => 'Show a listing of most recent 10 messages containing the string "cron run succesful".',
  41. 'drush watchdog-show --count=46' => 'Show a listing of most recent 46 messages.',
  42. 'drush watchdog-show --severity=notice' => 'Show a listing of most recent 10 messages with a severity of notice.',
  43. 'drush watchdog-show --type=php' => 'Show a listing of most recent 10 messages of type php.',
  44. 'drush watchdog-show --tail' => 'Show a listing of most recent 10 messages and continue showing messages as they are registered in the watchdog.',
  45. '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.',
  46. ),
  47. 'aliases' => array('wd-show', 'ws'),
  48. );
  49. $items['watchdog-delete'] = array(
  50. 'description' => 'Delete watchdog messages.',
  51. 'drupal dependencies' => drush_drupal_major_version() >= 6 ? array('dblog') : array('watchdog'),
  52. 'arguments' => array(
  53. '--severity' => 'Delete messages of a given severity level.',
  54. '--type' => 'Delete messages of a given type.',
  55. ),
  56. 'examples' => array(
  57. 'drush watchdog-delete all' => 'Delete all messages.',
  58. 'drush watchdog-delete 64' => 'Delete messages with id 64.',
  59. 'drush watchdog-delete "cron run succesful"' => 'Delete messages containing the string "cron run succesful".',
  60. 'drush watchdog-delete --severity=notice' => 'Delete all messages with a severity of notice.',
  61. 'drush watchdog-delete --type=cron' => 'Delete all messages of type cron.',
  62. ),
  63. 'aliases' => array('wd-del', 'wd-delete'),
  64. 'deprecated-aliases' => array('wd'),
  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_log(dt('Aborting.'));
  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. $where = core_watchdog_query($type, $severity, $filter);
  139. if ($where === FALSE) {
  140. return drush_log(dt('Aborting.'));
  141. }
  142. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], 0, $count, 'wid', 'DESC');
  143. if ($rsc === FALSE) {
  144. return drush_log(dt('Aborting.'));
  145. }
  146. $header = array(dt('Id'), dt('Date'), dt('Severity'), dt('Type'), dt('Message'));
  147. while ($result = drush_db_fetch_object($rsc)) {
  148. $row = core_watchdog_format_result($result);
  149. $table[] = array($row->wid, $row->date, $row->severity, $row->type, $row->message);
  150. }
  151. if (empty($table)) {
  152. return drush_log(dt('No log messages available.'), 'ok');
  153. }
  154. else {
  155. drush_log(dt('Most recent !count watchdog log messages:', array('!count' => $count)));
  156. if ($tail) {
  157. $table = array_reverse($table);
  158. }
  159. array_unshift($table, $header);
  160. $tbl = drush_print_table($table, TRUE);
  161. }
  162. if ($tail) {
  163. // We will reuse the table object to display each line generated while in tail mode.
  164. // To make it possible some hacking is done on the object: remove the header and reset the rows on each iteration.
  165. $tbl->_headers = NULL;
  166. // Obtain the last wid.
  167. $last = array_pop($table);
  168. $last_wid = $last[0];
  169. // Adapt the where snippet.
  170. if ($where['where'] != '') {
  171. $where['where'] .= ' AND ';
  172. }
  173. $where['where'] .= 'wid > :wid';
  174. // sleep-delay
  175. $sleep_delay = drush_get_option('sleep-delay', 1);
  176. while (TRUE) {
  177. $where['args'][':wid'] = $last_wid;
  178. $table = array();
  179. // Reset table rows.
  180. $tbl->_data = array();
  181. $rsc = drush_db_select('watchdog', '*', $where['where'], $where['args'], NULL, NULL, 'wid', 'ASC');
  182. while ($result = drush_db_fetch_object($rsc)) {
  183. $row = core_watchdog_format_result($result);
  184. $table[] = array($row->wid, $row->date, $row->severity, $row->type, $row->message);
  185. #$tbl->addRow(array($row->wid, $row->date, $row->severity, $row->type, $row->message));
  186. $last_wid = $row->wid;
  187. }
  188. $tbl->addData($table);
  189. print $tbl->_buildTable();
  190. sleep($sleep_delay);
  191. }
  192. }
  193. else {
  194. print "\n";
  195. }
  196. }
  197. /**
  198. * Format a watchdog database row.
  199. *
  200. * @param $result
  201. * Array. A database result object.
  202. * @return
  203. * Array. The result object with some attributes themed.
  204. */
  205. function core_watchdog_format_result($result, $full = FALSE) {
  206. // Severity.
  207. drush_include_engine('drupal', 'environment');
  208. $severities = core_watchdog_severity_levels();
  209. $result->severity = $severities[$result->severity];
  210. // Date.
  211. $result->date = format_date($result->timestamp, 'custom', 'd/M H:i');
  212. unset($result->timestamp);
  213. // Message.
  214. if (drush_drupal_major_version() > 5) {
  215. $variables = unserialize($result->variables);
  216. if (is_array($variables)) {
  217. $result->message = strtr($result->message, $variables);
  218. }
  219. unset($result->variables);
  220. }
  221. $result->message = truncate_utf8(strip_tags(decode_entities($result->message)), 188, FALSE, FALSE);
  222. // This data is only used in a detailed view.
  223. if ($full) {
  224. // Possible empty values.
  225. if (empty($result->link)) {
  226. unset($result->link);
  227. }
  228. if (empty($result->referer)) {
  229. unset($result->referer);
  230. }
  231. // Username.
  232. if ($account = user_load($result->uid)) {
  233. $result->username = $account->name;
  234. }
  235. else {
  236. $result->username = dt('Anonymous');
  237. }
  238. unset($result->uid);
  239. }
  240. return $result;
  241. }
  242. /**
  243. * Command callback.
  244. *
  245. * @param $arg
  246. * The id of the message to delete or 'all'.
  247. */
  248. function drush_core_watchdog_delete($arg = NULL) {
  249. if ($arg == 'all') {
  250. drush_print(dt('All watchdog messages will be deleted.'));
  251. if (!drush_confirm(dt('Do you really want to continue?'))) {
  252. return drush_log(dt('Aborting.'));
  253. }
  254. $affected_rows = drush_db_delete('watchdog');
  255. drush_log(dt('!affected watchdog messages have been deleted.', array('!affected' => $affected_rows)), 'ok');
  256. }
  257. else if (is_numeric($arg)) {
  258. drush_print(dt('Watchdog message #!wid will be deleted.', array('!wid' => $arg)));
  259. if(!drush_confirm(dt('Do you really want to continue?'))) {
  260. return drush_log(dt('Aborting.'));
  261. }
  262. $affected_rows = drush_db_delete('watchdog', 'wid=:wid', array(':wid' => $arg));
  263. if ($affected_rows == 1) {
  264. drush_log(dt('Watchdog message #!wid has been deleted.', array('!wid' => $arg)), 'ok');
  265. }
  266. else {
  267. return drush_set_error(dt('Watchdog message #!wid does not exist.', array('!wid' => $arg)));
  268. }
  269. }
  270. else {
  271. $type = drush_get_option('type');
  272. $severity = drush_get_option('severity');
  273. if ((is_null($arg))&&(is_null($type))&&(is_null($severity))) {
  274. return drush_set_error(dt('No options provided.'));
  275. }
  276. $where = core_watchdog_query($type, $severity, $filter, 'OR');
  277. if ($where === FALSE) {
  278. return drush_log(dt('Aborting.'), 'error');
  279. }
  280. drush_print(dt('All messages matching "!where" will be deleted.', array('!where' => strtr($where['where'], $where['args']))));
  281. if(!drush_confirm(dt('Do you really want to continue?'))) {
  282. return drush_log(dt('Aborting.'));
  283. }
  284. $affected_rows = drush_db_delete('watchdog', $where['where'], $where['args']);
  285. drush_log(dt('!affected_rows watchdog messages have been deleted.', array('!affected_rows' => $affected_rows)), 'ok');
  286. }
  287. }
  288. /**
  289. * Build a WHERE snippet based on given parameters.
  290. *
  291. * @param $type
  292. * String. Valid watchdog type.
  293. * @param $severity
  294. * Int or String for a valid watchdog severity message.
  295. * @param $filter
  296. * String. Value to filter watchdog messages by.
  297. * @param $criteria
  298. * ('AND', 'OR'). Criteria for the WHERE snippet.
  299. * @return
  300. * False or array with structure ('where' => string, 'args' => array())
  301. */
  302. function core_watchdog_query($type = NULL, $severity = NULL, $filter = NULL, $criteria = 'AND') {
  303. $args = array();
  304. $conditions = array();
  305. if ($type) {
  306. $types = core_watchdog_message_types();
  307. if (array_search($type, $types) === FALSE) {
  308. $msg = "Unknown message type: !type.\nValid types are: !types.";
  309. return drush_set_error(dt($msg, array('!type' => $type, '!types' => implode(', ', $types))));
  310. }
  311. $conditions[] = "type = :type";
  312. $args[':type'] = $type;
  313. }
  314. if (!is_null($severity)) {
  315. drush_include_engine('drupal', 'environment');
  316. $severities = core_watchdog_severity_levels();
  317. if (isset($severities[$severity])) {
  318. $level = $severity;
  319. }
  320. elseif (($key = array_search($severity, $severities)) !== FALSE) {
  321. $level = $key;
  322. }
  323. else {
  324. $level = FALSE;
  325. }
  326. if ($level === FALSE) {
  327. foreach ($severities as $key => $value) {
  328. $levels[] = "$value($key)";
  329. }
  330. $msg = "Unknown severity level: !severity.\nValid severity levels are: !levels.";
  331. return drush_set_error(dt($msg, array('!severity' => $severity, '!levels' => implode(', ', $levels))));
  332. }
  333. $conditions[] = 'severity = :severity';
  334. $args[':severity'] = $level;
  335. }
  336. if ($filter) {
  337. $conditions[] = "message LIKE :filter";
  338. $args[':filter'] = '%'.$filter.'%';
  339. }
  340. $where = implode(" $criteria ", $conditions);
  341. return array('where' => $where, 'args' => $args);
  342. }
  343. /**
  344. * Helper function to obtain the message types based on drupal version.
  345. *
  346. * @return
  347. * Array of watchdog message types.
  348. */
  349. function core_watchdog_message_types() {
  350. if (drush_drupal_major_version() == 5) {
  351. return _watchdog_get_message_types();
  352. }
  353. else {
  354. return _dblog_get_message_types();
  355. }
  356. }