You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debug_logger.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Debug Logger
  4. *
  5. * Enhanced logging for debugging purposes. It is not recommened
  6. * to be enabled on production systems without testing because of
  7. * the somewhat increased memory, cpu and disk i/o overhead.
  8. *
  9. * Debug Logger listens for existing console("message") calls and
  10. * introduces start and end tags as well as free form tagging
  11. * which can redirect messages to files. The resulting log files
  12. * provide timing and tag quantity results.
  13. *
  14. * Enable the plugin in config.inc.php and add your desired
  15. * log types and files.
  16. *
  17. * @author Ziba Scott
  18. * @website http://roundcube.net
  19. *
  20. * Example:
  21. *
  22. * config.inc.php:
  23. *
  24. * // $config['debug_logger'][type of logging] = name of file in log_dir
  25. * // The 'master' log includes timing information
  26. * $config['debug_logger']['master'] = 'master';
  27. * // If you want sql messages to also go into a separate file
  28. * $config['debug_logger']['sql'] = 'sql';
  29. *
  30. * index.php (just after $RCMAIL->plugins->init()):
  31. *
  32. * console("my test","start");
  33. * console("my message");
  34. * console("my sql calls","start");
  35. * console("cp -r * /dev/null","shell exec");
  36. * console("select * from example","sql");
  37. * console("select * from example","sql");
  38. * console("select * from example","sql");
  39. * console("end");
  40. * console("end");
  41. *
  42. *
  43. * logs/master (after reloading the main page):
  44. *
  45. * [17-Feb-2009 16:51:37 -0500] start: Task: mail.
  46. * [17-Feb-2009 16:51:37 -0500] start: my test
  47. * [17-Feb-2009 16:51:37 -0500] my message
  48. * [17-Feb-2009 16:51:37 -0500] shell exec: cp -r * /dev/null
  49. * [17-Feb-2009 16:51:37 -0500] start: my sql calls
  50. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  51. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  52. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  53. * [17-Feb-2009 16:51:37 -0500] end: my sql calls - 0.0018 seconds shell exec: 1, sql: 3,
  54. * [17-Feb-2009 16:51:37 -0500] end: my test - 0.0055 seconds shell exec: 1, sql: 3,
  55. * [17-Feb-2009 16:51:38 -0500] end: Task: mail. - 0.8854 seconds shell exec: 1, sql: 3,
  56. *
  57. * logs/sql (after reloading the main page):
  58. *
  59. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  60. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  61. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  62. */
  63. class debug_logger extends rcube_plugin
  64. {
  65. function init()
  66. {
  67. require_once(__DIR__ . '/runlog/runlog.php');
  68. $this->runlog = new runlog();
  69. if(!rcmail::get_instance()->config->get('log_dir')){
  70. rcmail::get_instance()->config->set('log_dir',INSTALL_PATH.'logs');
  71. }
  72. $log_config = rcmail::get_instance()->config->get('debug_logger',array());
  73. foreach ($log_config as $type => $file){
  74. $this->runlog->set_file(rcmail::get_instance()->config->get('log_dir').'/'.$file, $type);
  75. }
  76. $start_string = "";
  77. $action = rcmail::get_instance()->action;
  78. $task = rcmail::get_instance()->task;
  79. if($action){
  80. $start_string .= "Action: ".$action.". ";
  81. }
  82. if($task){
  83. $start_string .= "Task: ".$task.". ";
  84. }
  85. $this->runlog->start($start_string);
  86. $this->add_hook('console', array($this, 'console'));
  87. $this->add_hook('authenticate', array($this, 'authenticate'));
  88. }
  89. function authenticate($args){
  90. $this->runlog->note('Authenticating '.$args['user'].'@'.$args['host']);
  91. return $args;
  92. }
  93. function console($args){
  94. $note = $args[0];
  95. $type = $args[1];
  96. if(!isset($args[1])){
  97. // This could be extended to detect types based on the
  98. // file which called console. For now only rcube_imap/rcube_storage is supported
  99. $bt = debug_backtrace();
  100. $file = $bt[3]['file'];
  101. switch(basename($file)){
  102. case 'rcube_imap.php':
  103. $type = 'imap';
  104. break;
  105. case 'rcube_storage.php':
  106. $type = 'storage';
  107. break;
  108. default:
  109. $type = FALSE;
  110. break;
  111. }
  112. }
  113. switch($note){
  114. case 'end':
  115. $type = 'end';
  116. break;
  117. }
  118. switch($type){
  119. case 'start':
  120. $this->runlog->start($note);
  121. break;
  122. case 'end':
  123. $this->runlog->end();
  124. break;
  125. default:
  126. $this->runlog->note($note, $type);
  127. break;
  128. }
  129. return $args;
  130. }
  131. function __destruct()
  132. {
  133. if ($this->runlog)
  134. $this->runlog->end();
  135. }
  136. }