Refactor of logging code to suport multiple log engines

This commit is contained in:
2011-04-24 10:37:15 +01:00
parent ab127e93e8
commit e1cb31e5ca
11 changed files with 450 additions and 139 deletions

View File

@@ -2,48 +2,76 @@
class SihnonFramework_Log {
const LEVEL_DEBUG = 'DEBUG';
const LEVEL_INFO = 'INFO';
const LEVEL_WARNING = 'WARNING';
const LEVEL_ERROR = 'ERROR';
const LEVEL_DEBUG = 'debug';
const LEVEL_INFO = 'info';
const LEVEL_WARNING = 'warning';
const LEVEL_ERROR = 'error';
const CATEGORY_DEFAULT = 'default';
const ORDER_ASC = 'ASC';
const ORDER_DESC = 'DESC';
private static $hostname = '';
protected $plugins = array();
private $backend;
private $progname;
public function __construct($backend, $options = array(), $progname = '') {
$this->progname = $progname;
public function __construct(SihnonFramework_Config $config) {
// Get a list of the logging plugins to be used
$plugins = $config->get('logging.plugins');
$this->backend = Sihnon_Log_PluginFactory::create($backend, $options);
foreach ($plugins as $plugin) {
// Get a list of all the instances of this plugin to be used
$instances = $config->get("logging.{$plugin}");
foreach ($instances as $instance) {
$this->plugins[$plugin][] = array(
'name' => $instance,
'backend' => Sihnon_Log_PluginFactory::create($config, $plugin, $instance),
'severity' => $config->get("logging.{$plugin}.{$instance}.severity"),
'category' => $config->get("logging.{$plugin}.{$instance}.category"),
);
}
}
SihnonFramework_LogEntry::info($this, "Logging started");
}
public function log($level, $message) {
$this->backend->log($level, time(), 0, self::$hostname, $this->progname, 0, $message);
public function log(SihnonFramework_LogEntry $entry) {
foreach ($this->plugins as $plugin => $instances) {
foreach ($instances as $instance) {
if (in_array($entry->level(), $instance['severity'])) {
if (in_array($entry->category(), $instance['category'])) {
$instance['backend']->log($entry);
}
}
}
}
}
public function debug($message) {
return $this->log(self::LEVEL_DEBUG, $message);
public function recentEntries($entry_class, $instance_name, $order_field, $order_direction = self::ORDER_DESC, $limit = 10) {
// Look for the right instance
foreach ($this->plugins as $plugin => $instances) {
foreach ($instances as $instance) {
if ($instance['name'] == $instance_name) {
return $instance['backend']->recent($entry_class, $order_field, $order_direction, $limit);
}
}
}
return array();
}
public function info($message) {
return $this->log(self::LEVEL_INFO, $message);
}
public function warning($message) {
return $this->log(self::LEVEL_WARNING, $message);
}
public function error($message) {
return $this->log(self::LEVEL_ERROR, $message);
}
public static function initialise() {
self::$hostname = trim(`hostname`);
public function recentEntriesByField($entry_class, $instance_name, $field, $value, $type, $order_field, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
// Look for the right instance
foreach ($this->plugins as $plugin => $instances) {
foreach ($instances as $instance) {
if ($instance['name'] == $instance_name) {
return $instance['backend']->recentByField($entry_class, $field, $value, $type, $order_field, $order_direction, $limit);
}
}
}
return array();
}
}
SihnonFramework_Log::initialise();
?>