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

@@ -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();
}
}
?>