LogFilterPlugin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Example to disable all debug messages and those containing 'About to push':
  24. * addPlugin('LogFilter', array(
  25. * 'priority' => array(LOG_DEBUG => false),
  26. * 'regex' => array('/About to push/' => false)
  27. * ));
  28. *
  29. * @todo add an admin panel
  30. *
  31. * @package LogFilterPlugin
  32. * @maintainer Brion Vibber <brion@status.net>
  33. */
  34. class LogFilterPlugin extends Plugin
  35. {
  36. public $default = true; // Set to false to require opting things in
  37. public $priority = array(); // override by priority: array(LOG_ERR => true, LOG_DEBUG => false)
  38. public $regex = array(); // override by regex match of message: array('/twitter/i' => false)
  39. function onPluginVersion(array &$versions)
  40. {
  41. $versions[] = array('name' => 'LogFilter',
  42. 'version' => GNUSOCIAL_VERSION,
  43. 'author' => 'Brion Vibber',
  44. 'homepage' => 'http://status.net/wiki/Plugin:LogFilter',
  45. 'rawdescription' =>
  46. // TRANS: Plugin description.
  47. _m('Provides server-side setting to filter log output by type or keyword.'));
  48. return true;
  49. }
  50. /**
  51. * Hook for the StartLog event in common_log().
  52. * If a message doesn't pass our filters, we'll abort it.
  53. *
  54. * @param string $priority
  55. * @param string $msg
  56. * @param string $filename
  57. * @return boolean hook result code
  58. */
  59. function onStartLog(&$priority, &$msg, &$filename)
  60. {
  61. if ($this->filter($priority, $msg)) {
  62. // Let it through
  63. return true;
  64. } else {
  65. // Abort -- this line will go to /dev/null :)
  66. return false;
  67. }
  68. }
  69. /**
  70. * Do the filtering...
  71. *
  72. * @param string $priority
  73. * @param string $msg
  74. * @return boolean true to let the log message be processed
  75. */
  76. function filter($priority, $msg)
  77. {
  78. $state = $this->default;
  79. if (array_key_exists($priority, $this->priority)) {
  80. $state = $this->priority[$priority];
  81. }
  82. foreach ($this->regex as $regex => $override) {
  83. if (preg_match($regex, $msg)) {
  84. $state = $override;
  85. }
  86. }
  87. return $state;
  88. }
  89. }