From 04de50cecda8eefdb46cf914d759caff39b503ff Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 6 Aug 2011 14:51:51 +0100 Subject: [PATCH] Add Syslog logging backend --- .../Log/Plugin/Syslog.class.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 source/lib/SihnonFramework/Log/Plugin/Syslog.class.php diff --git a/source/lib/SihnonFramework/Log/Plugin/Syslog.class.php b/source/lib/SihnonFramework/Log/Plugin/Syslog.class.php new file mode 100644 index 0000000..1b968c0 --- /dev/null +++ b/source/lib/SihnonFramework/Log/Plugin/Syslog.class.php @@ -0,0 +1,80 @@ + LOG_DEBUG, + SihnonFramework_Log::LEVEL_INFO => LOG_INFO, + SihnonFramework_Log::LEVEL_WARNING => LOG_WARNING, + SihnonFramework_Log::LEVEL_ERROR => LOG_ERR, + ); + + protected $format; + + protected function __construct($instance, $format) { + parent::__construct($instance); + + $this->format = $format; + openlog(SihnonFramework_LogEntry::localProgname(), LOG_PID|LOG_ODELAY, LOG_LOCAL0); + } + + public function __destruct() { + closelog(); + } + + public static function create(SihnonFramework_Config $config, $instance) { + $format = $config->get("logging.".static::PLUGIN_NAME.".{$instance}.format"); + + return new self($instance, $format); + } + + /** + * Records a new entry in the storage backend used by this logging plugin + * + * @param SihnonFramework_LogEntry $entry Log Entry object containing the information to be recorded + */ + public function log(SihnonFramework_LogEntry $entry) { + $fields = $entry->fields(); + $values = $entry->values(); + $formatted_entry = str_replace(array_map(function($name) { return "%{$name}%"; }, $fields ), $values, $this->format); + + syslog(static::$level_map[$entry->level()], $formatted_entry); + } + + /** + * Returns an array of recent log messages written using the plugin + * + * @param string $entry_class Class name of the LogEntry class to use to reinstanciate the contents of the log entry + * @param string $order_field Field name to order log entries by before selecting + * @param string $order_direction Order in which to sort log entries before selecting. Use the SihnonFramework_Log::ORDER_* constants. + * @param int $limit Maximum number of log entries to retrieve + * @return array(SihnonFramework_LogEntry) + */ + public function recent($entry_class, $order_field, $order_direction, $limit = 30) { + throw new SihnonFramework_Exception_NotImplemented(); + } + + /** + * Returns an array of recent log messages matching a particular field/value written using the plugin + * + * @param string $entry_class Class name of the LogEntry class to use to reinstanciate the contents of the log entry + * @param string $field Field to match log entries on + * @param mixed $value Value to match log entries on + * @param string $order_field Field name to order log entries by before selecting + * @param string $order_direction Order in which to sort log entries before selecting. Use the SihnonFramework_Log::ORDER_* constants. + * @param int $limit Maximum number of log entries to retrieve + * @return array(SihnonFramework_LogEntry) + */ + public function recentByField($entry_class, $field, $value, $order_field, $order_direction, $limit = 30) { + throw new SihnonFramework_Exception_NotImplemented(); + } + +}; + +?> \ No newline at end of file