From b6e5b1d294030f5dd9d386b15d8cef7fcbea3768 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 24 Nov 2010 10:13:03 -0800 Subject: [PATCH] Add LogFilter plugin: quickie way to filter out/in log output based on priority or keyword matches. Should be helpful for folks who can't easily adjust their syslog filtering. --- plugins/LogFilter/LogFilterPlugin.php | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 plugins/LogFilter/LogFilterPlugin.php diff --git a/plugins/LogFilter/LogFilterPlugin.php b/plugins/LogFilter/LogFilterPlugin.php new file mode 100644 index 0000000000..e2153e959c --- /dev/null +++ b/plugins/LogFilter/LogFilterPlugin.php @@ -0,0 +1,94 @@ +. + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Example to disable all debug messages and those containing 'About to push': + * addPlugin('LogFilter', array( + * 'priority' => array(LOG_DEBUG => false), + * 'regex' => array('/About to push/' => false) + * )); + * + * @todo add an admin panel + * + * @package LogFilterPlugin + * @maintainer Brion Vibber + */ +class LogFilterPlugin extends Plugin +{ + public $default = true; // Set to false to require opting things in + public $priority = array(); // override by priority: array(LOG_ERR => true, LOG_DEBUG => false) + public $regex = array(); // override by regex match of message: array('/twitter/i' => false) + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'LogFilter', + 'version' => STATUSNET_VERSION, + 'author' => 'Brion Vibber', + 'homepage' => 'http://status.net/wiki/Plugin:LogFilter', + 'rawdescription' => + _m('Provides server-side setting to filter log output by type or keyword.')); + + return true; + } + + /** + * Hook for the StartLog event in common_log(). + * If a message doesn't pass our filters, we'll abort it. + * + * @param string $priority + * @param string $msg + * @param string $filename + * @return boolean hook result code + */ + function onStartLog(&$priority, &$msg, &$filename) + { + if ($this->filter($priority, $msg)) { + // Let it through + return true; + } else { + // Abort -- this line will go to /dev/null :) + return false; + } + } + + /** + * Do the filtering... + * + * @param string $priority + * @param string $msg + * @return boolean true to let the log message be processed + */ + function filter($priority, $msg) + { + $state = $this->default; + if (array_key_exists($priority, $this->priority)) { + $state = $this->priority[$priority]; + } + foreach ($this->regex as $regex => $override) { + if (preg_match($regex, $msg)) { + $state = $override; + } + } + return $state; + } +}