LogFilterPlugin.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const PLUGIN_VERSION = '2.0.0';
  37. public $default = true; // Set to false to require opting things in
  38. public $priority = array(); // override by priority: array(LOG_ERR => true, LOG_DEBUG => false)
  39. public $regex = array(); // override by regex match of message: array('/twitter/i' => false)
  40. public function onPluginVersion(array &$versions): bool
  41. {
  42. $versions[] = array('name' => 'LogFilter',
  43. 'version' => self::PLUGIN_VERSION,
  44. 'author' => 'Brion Vibber',
  45. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/LogFilter',
  46. 'rawdescription' =>
  47. // TRANS: Plugin description.
  48. _m('Provides server-side setting to filter log output by type or keyword.'));
  49. return true;
  50. }
  51. /**
  52. * Hook for the StartLog event in common_log().
  53. * If a message doesn't pass our filters, we'll abort it.
  54. *
  55. * @param string $priority
  56. * @param string $msg
  57. * @param string $filename
  58. * @return boolean hook result code
  59. */
  60. function onStartLog(&$priority, &$msg, &$filename)
  61. {
  62. if ($this->filter($priority, $msg)) {
  63. // Let it through
  64. return true;
  65. } else {
  66. // Abort -- this line will go to /dev/null :)
  67. return false;
  68. }
  69. }
  70. /**
  71. * Do the filtering...
  72. *
  73. * @param string $priority
  74. * @param string $msg
  75. * @return boolean true to let the log message be processed
  76. */
  77. function filter($priority, $msg)
  78. {
  79. $state = $this->default;
  80. if (array_key_exists($priority, $this->priority)) {
  81. $state = $this->priority[$priority];
  82. }
  83. foreach ($this->regex as $regex => $override) {
  84. if (preg_match($regex, $msg)) {
  85. $state = $override;
  86. }
  87. }
  88. return $state;
  89. }
  90. }