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

@@ -1,5 +1,15 @@
<?php
/**
* Sihnon Framework Library path
*
* Specifies the absolute or relative path to the Sihnon Framework library directory, relative to the webui root directory.
* Path must end with a trailing slash.
*
* @var string
*/
define('SihnonFramework_Lib', '../../sihnon-php/lib/source/lib/');
/**
* Sihnon Library path
*
@@ -8,7 +18,8 @@
*
* @var string
*/
define('Sihnon_Lib', '../lib/');
define('Sihnon_Lib', '/home/ben/projects/handbrake-cluster-webui/lib/');
/**
* Sihnon Database Support

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';
private static $hostname = '';
const CATEGORY_DEFAULT = 'default';
private $backend;
private $progname;
const ORDER_ASC = 'ASC';
const ORDER_DESC = 'DESC';
public function __construct($backend, $options = array(), $progname = '') {
$this->progname = $progname;
protected $plugins = array();
$this->backend = Sihnon_Log_PluginFactory::create($backend, $options);
public function __construct(SihnonFramework_Config $config) {
// Get a list of the logging plugins to be used
$plugins = $config->get('logging.plugins');
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 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);
}
}
}
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`);
return array();
}
}
SihnonFramework_Log::initialise();
?>

View File

@@ -3,25 +3,45 @@
interface SihnonFramework_Log_IPlugin extends Sihnon_IPlugin {
/**
* Returns a new instance of the Plugin class
* Creates a new instance of the Logging Plugin using the specified instance name, reading in the necessary config
*
* @param array(string=>mixed) $options Configuration options for the Plugin object
* @param SihnonFramework_Config $config Config option to retrieve plugin configuration
* @param string $instance Name of the instance to create
* @return SihnonFramework_Log_IPlugin
*/
public static function create($options);
public static function create(SihnonFramework_Config $config, $instance);
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
* @param SihnonFramework_LogEntry $entry LogEntry object describing information to be recorded
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message);
public function log(SihnonFramework_LogEntry $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);
/**
* 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);
}
?>

View File

@@ -0,0 +1,77 @@
<?php
class SihnonFramework_Log_Plugin_Console extends SihnonFramework_Log_PluginBase implements SihnonFramework_Log_IPlugin {
/**
* Name of this plugin
* @var string
*/
const PLUGIN_NAME = "Console";
protected $format;
protected function __construct($instance, $format) {
parent::__construct($instance);
$this->format = $format;
}
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();
$fields_map = array_combine($fields, $values);
// Make some alterations for ease of display
$fields_map['timestamp'] = date('d/m/y H:i:s', $fields_map['ctime']);
// split the map back out again now the modifications have been made
$fields = array_keys($fields_map);
$values = array_values($fields_map);
$formatted_entry = str_replace(array_map(function($name) { return "%{$name}%"; }, $fields ), $values, $this->format);
echo $formatted_entry . "\n";
}
/**
* 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();
}
};
?>

View File

@@ -1,6 +1,6 @@
<?php
class SihnonFramework_Log_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
class SihnonFramework_Log_Plugin_Database extends SihnonFramework_Log_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin
@@ -11,39 +11,108 @@ class SihnonFramework_Log_Plugin_Database extends Sihnon_PluginBase implements S
private $database;
private $table;
protected function __construct($options) {
$this->database = $options['database'];
$this->table = Sihnon_Main::instance()->config()->get('logging.database.table');
protected function __construct($instance, $database, $table) {
parent::__construct($instance);
$this->database = $database;
$this->table = $table;
}
public static function create($options) {
return new self($options);
public static function create(SihnonFramework_Config $config, $instance) {
$database = SihnonFramework_Main::instance()->database();
$table = $config->get("logging.".self::PLUGIN_NAME.".{$instance}.table", 'SihnonFramework_Exception_MissingParameter');
return new self($instance, $database, $table);
}
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
* @param SihnonFramework_LogEntry $entry Log Entry object containing the information to be recorded
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
$this->database->insert("INSERT INTO {$this->table} (level,ctime,pid,hostname,progname,line,message) VALUES(:level, :ctime, :pid, :hostname, :progname, :line, :message)",
public function log(SihnonFramework_LogEntry $entry) {
$fields = $entry->fields();
$types = $entry->types();
$values = $entry->values();
$bindings = array();
for ($i = 0, $l = count($fields); $i < $l; ++$i) {
$type = '';
switch ($types[$i]) {
case 'int':
$type = PDO::PARAM_INT;
break;
case 'bool':
$type = PDO::PARAM_BOOL;
break;
default:
$type = PDO::PARAM_STR;
break;
}
$bindings[] = array(
'name' => $fields[$i],
'value' => $values[$i],
'type' => $type,
);
}
$field_list = join(', ', $fields);
$bindings_list = join(', ', array_walk($this->fields, create_function('$value', 'return ":{$value}";')));
$this->database->insert("INSERT INTO {$this->table} ({$field_list}) VALUES({$bindings_list})", $bindings);
}
/**
* 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 int $limit Maximum number of log entries to retrieve
* @return array(SihnonFramework_LogEntry)
*
* @todo customise order field instead of using hardcoded ctime
*/
public function recent($entry_class, $order_field, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
$entries = array();
$records = $this->database->selectList("SELECT * FROM `{$this->table}` ORDER BY `{$order_field}` {$order_direction} LIMIT {$limit}", array());
foreach ($records as $record) {
$entries[] = $entry_class::fromArray($record);
}
return $entries;
}
public function recentByField($entry_class, $field, $value, $type, $order_field, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
$entries = array();
$pdo_type = '';
switch ($type) {
case 'int':
$pdo_type = PDO::PARAM_INT;
break;
case 'bool':
$pdo_type = PDO::PARAM_BOOL;
break;
default:
$pdo_type = PDO::PARAM_STR;
break;
}
$records = $this->database->selectList("SELECT * FROM `{$this->table}` WHERE `{$field}`=:{$field} ORDER BY `{$order_field}` {$order_direction} LIMIT {$limit}",
array(
array('name' => 'level', 'value' => $level, 'type' => PDO::PARAM_STR),
array('name' => 'ctime', 'value' => $ctime, 'type' => PDO::PARAM_INT),
array('name' => 'pid', 'value' => $pid, 'type' => PDO::PARAM_INT),
array('name' => 'hostname', 'value' => $hostname, 'type' => PDO::PARAM_STR),
array('name' => 'progname', 'value' => $progname, 'type' => PDO::PARAM_STR),
array('name' => 'line', 'value' => $line, 'type' => PDO::PARAM_INT),
array('name' => 'message', 'value' => $message, 'type' => PDO::PARAM_STR)
array('name' => $field, 'value' => $value, 'type' => $pdo_type),
)
);
foreach ($records as $record) {
$entries[] = $entry_class::fromArray($record);
}
return $entries;
}
}
?>

View File

@@ -1,6 +1,6 @@
<?php
class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
class SihnonFramework_Log_Plugin_FlatFile extends SihnonFramework_Log_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin
@@ -9,10 +9,15 @@ class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements S
const PLUGIN_NAME = "FlatFile";
protected $filename;
protected $format;
protected $fp;
protected function __construct($options) {
$this->filename = Sihnon_Main::instance()->config()->get('logging.flatfile.filename');
protected function __construct($instance, $filename, $format) {
parent::__construct($instance);
$this->filename = $filename;
$this->format = $format;
$this->fp = fopen($this->filename, 'a');
}
@@ -20,25 +25,64 @@ class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements S
fclose($this->fp);
}
public static function create($options) {
return new self($options);
public static function create(SihnonFramework_Config $config, $instance) {
$filename = $config->get("logging.".self::PLUGIN_NAME.".{$instance}.filename");
$format = $config->get("logging.".self::PLUGIN_NAME.".{$instance}.format");
return new self($instance, $filename, $format);
}
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
* @param SihnonFramework_LogEntry $entry Log Entry object containing the information to be recorded
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
$log_entry = implode(',', array($level, $ctime, $pid, $hostname, $progname, $line, $message)) . "\n";
fwrite($this->fp, $log_entry, strlen($log_entry));
public function log(SihnonFramework_LogEntry $entry) {
$fields = $entry->fields();
$values = $entry->values();
$fields_map = array_combine($fields, $values);
// Make some alterations for ease of display
$fields_map['timestamp'] = date('d/m/y H:i:s', $fields_map['ctime']);
// split the map back out again now the modifications have been made
$fields = array_keys($fields_map);
$values = array_values($fields_map);
$formatted_entry = str_replace(array_map(function($name) { return "%{$name}%"; }, $fields), $values, $this->format) . "\n";
fwrite($this->fp, $formatted_entry, strlen($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();
}
}
?>

View File

@@ -0,0 +1,15 @@
<?php
class SihnonFramework_Log_PluginBase extends SihnonFramework_PluginBase {
protected $instance;
protected function __construct($instance) {
$this->instance = $instance;
}
public function instance() {
return $this->instance;
}
}

View File

@@ -13,7 +13,7 @@ class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory {
}
public static function create($plugin, $options) {
public static function create(SihnonFramework_Config $config, $plugin, $instance) {
self::ensureScanned();
if (! self::isValidPlugin($plugin)) {
@@ -22,7 +22,7 @@ class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory {
$classname = self::classname($plugin);
return call_user_func(array($classname, 'create'), $options);
return call_user_func(array($classname, 'create'), $config, $instance);
}
}

View File

@@ -1,89 +1,105 @@
<?php
abstract class SihnonFramework_LogEntry {
class SihnonFramework_LogEntry {
protected static $table_name = "";
protected static $hostname;
protected static $progname;
protected static $types = array(
'level' => 'string',
'category' => 'string',
'ctime' => 'int',
'hostname' => 'string',
'progname' => 'string',
'pid' => 'int',
'file' => 'string',
'line' => 'int',
'message' => 'string',
);
protected $id;
protected $level;
protected $category;
protected $ctime;
protected $pid;
protected $hostname;
protected $progname;
protected $file;
protected $line;
protected $message;
protected function __construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message) {
$this->id = $id;
public static function initialise() {
self::$hostname = gethostname();
self::$progname = '';
}
protected function __construct($level, $category, $ctime, $pid, $file, $line, $message) {
$this->level = $level;
$this->category = $category;
$this->ctime = $ctime;
$this->pid = $pid;
$this->hostname = $hostname;
$this->progname = $progname;
$this->file = $file;
$this->line = $line;
$this->message = $message;
}
public static function fromDatabaseRow($row) {
$called_class = get_called_class();
return new $called_class(
$row['id'],
public static function fromArray($row) {
return new self(
$row['level'],
$row['category'],
$row['ctime'],
$row['pid'],
$row['hostname'],
$row['progname'],
$row['file'],
$row['line'],
$row['message']
);
}
public static function fromId($id) {
$called_class = get_called_class();
$database = Sihnon_Main::instance()->database();
return $called_class::fromDatabaseRow(
$database->selectOne('SELECT * FROM '.static::$table_name.' WHERE id=:id', array(
array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT)
)
)
public function fields() {
return array_keys(static::$types);
}
public function types() {
return array_values(static::$types);
}
public function values() {
return array(
$this->level,
$this->category,
$this->ctime,
static::$hostname,
static::$progname,
$this->pid,
$this->file,
$this->line,
$this->message,
);
}
public static function recent($limit = 100) {
$entries = array();
$database = Sihnon_Main::instance()->database();
foreach ($database->selectList('SELECT * FROM '.static::$table_name.' ORDER BY ctime DESC LIMIT :limit', array(
array('name' => 'limit', 'value' => $limit, 'type' => PDO::PARAM_INT)
)) as $row) {
$entries[] = static::fromDatabaseRow($row);
}
return $entries;
}
public function id() {
return $this->id;
}
public function level() {
return $this->level;
}
public function category() {
return $this->category;
}
public function ctime() {
return $this->ctime;
}
public function hostname() {
return static::$hostname;
}
public function progname() {
return static::$progname;
}
public function pid() {
return $this->pid;
}
public function hostname() {
return $this->hostname;
}
public function progname() {
return $this->progname;
public function file() {
return $this->file;
}
public function line() {
@@ -94,6 +110,39 @@ abstract class SihnonFramework_LogEntry {
return $this->message;
}
protected static function log($logger, $severity, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
$backtrace = debug_backtrace(false);
$entry = new self($severity, $category, time(), getmypid(), $backtrace[1]['file'], $backtrace[1]['line'], $message);
$logger->log($entry);
}
public static function debug($logger, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
static::log($logger, SihnonFramework_Log::LEVEL_DEBUG, $message, $category);
}
public static function info($logger, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
static::log($logger, SihnonFramework_Log::LEVEL_INFO, $message, $category);
}
public static function warning($logger, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
static::log($logger, SihnonFramework_Log::LEVEL_WARNING, $message, $category);
}
public static function error($logger, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
static::log($logger, SihnonFramework_Log::LEVEL_ERROR, $message, $category);
}
public static function recentEntries($log, $instance, $order_fields, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
return $log->recentEntries(get_called_class(), $instance, $order_field, $order_direction, $limit);
}
public static function recentEntriesByField($log, $instance, $field, $value, $order_field, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
return $log->recentEntriesByField(get_called_class(), $instance, $field, $value, static::$types[$field], $order_field, $order_direction, $limit);
}
};
SihnonFramework_LogEntry::initialise();
?>

View File

@@ -35,9 +35,7 @@ class SihnonFramework_Main {
'filename' => Sihnon_ConfigFile)
);
$this->log = new Sihnon_Log($this->config->get('logging.plugin'), array(
'database' => $this->database)
);
$this->log = new Sihnon_Log($this->config);
$this->cache = new Sihnon_Cache($this->config);
}