Refactor of logging code to suport multiple log engines
This commit is contained in:
@@ -1,5 +1,15 @@
|
|||||||
<?php
|
<?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
|
* Sihnon Library path
|
||||||
*
|
*
|
||||||
@@ -8,7 +18,8 @@
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
define('Sihnon_Lib', '../lib/');
|
define('Sihnon_Lib', '/home/ben/projects/handbrake-cluster-webui/lib/');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sihnon Database Support
|
* Sihnon Database Support
|
||||||
|
|||||||
@@ -2,48 +2,76 @@
|
|||||||
|
|
||||||
class SihnonFramework_Log {
|
class SihnonFramework_Log {
|
||||||
|
|
||||||
const LEVEL_DEBUG = 'DEBUG';
|
const LEVEL_DEBUG = 'debug';
|
||||||
const LEVEL_INFO = 'INFO';
|
const LEVEL_INFO = 'info';
|
||||||
const LEVEL_WARNING = 'WARNING';
|
const LEVEL_WARNING = 'warning';
|
||||||
const LEVEL_ERROR = 'ERROR';
|
const LEVEL_ERROR = 'error';
|
||||||
|
|
||||||
private static $hostname = '';
|
const CATEGORY_DEFAULT = 'default';
|
||||||
|
|
||||||
private $backend;
|
const ORDER_ASC = 'ASC';
|
||||||
private $progname;
|
const ORDER_DESC = 'DESC';
|
||||||
|
|
||||||
public function __construct($backend, $options = array(), $progname = '') {
|
protected $plugins = array();
|
||||||
$this->progname = $progname;
|
|
||||||
|
|
||||||
$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) {
|
public function log(SihnonFramework_LogEntry $entry) {
|
||||||
$this->backend->log($level, time(), 0, self::$hostname, $this->progname, 0, $message);
|
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) {
|
public function recentEntries($entry_class, $instance_name, $order_field, $order_direction = self::ORDER_DESC, $limit = 10) {
|
||||||
return $this->log(self::LEVEL_DEBUG, $message);
|
// 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) {
|
public function recentEntriesByField($entry_class, $instance_name, $field, $value, $type, $order_field, $order_direction = SihnonFramework_Log::ORDER_DESC, $limit = 30) {
|
||||||
return $this->log(self::LEVEL_INFO, $message);
|
// 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 array();
|
||||||
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`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SihnonFramework_Log::initialise();
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -3,25 +3,45 @@
|
|||||||
interface SihnonFramework_Log_IPlugin extends Sihnon_IPlugin {
|
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
|
* Records a new entry in the storage backend used by this logging plugin
|
||||||
*
|
*
|
||||||
* @param string $level Severity of the log entry
|
* @param SihnonFramework_LogEntry $entry LogEntry object describing information to be recorded
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
77
source/lib/SihnonFramework/Log/Plugin/Console.class.php
Normal file
77
source/lib/SihnonFramework/Log/Plugin/Console.class.php
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?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
|
* Name of this plugin
|
||||||
@@ -11,39 +11,108 @@ class SihnonFramework_Log_Plugin_Database extends Sihnon_PluginBase implements S
|
|||||||
private $database;
|
private $database;
|
||||||
private $table;
|
private $table;
|
||||||
|
|
||||||
protected function __construct($options) {
|
protected function __construct($instance, $database, $table) {
|
||||||
$this->database = $options['database'];
|
parent::__construct($instance);
|
||||||
$this->table = Sihnon_Main::instance()->config()->get('logging.database.table');
|
|
||||||
|
$this->database = $database;
|
||||||
|
$this->table = $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create($options) {
|
public static function create(SihnonFramework_Config $config, $instance) {
|
||||||
return new self($options);
|
$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
|
* Records a new entry in the storage backend used by this logging plugin
|
||||||
*
|
*
|
||||||
* @param string $level Severity of the log entry
|
* @param SihnonFramework_LogEntry $entry Log Entry object containing the information to be recorded
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
|
public function log(SihnonFramework_LogEntry $entry) {
|
||||||
$this->database->insert("INSERT INTO {$this->table} (level,ctime,pid,hostname,progname,line,message) VALUES(:level, :ctime, :pid, :hostname, :progname, :line, :message)",
|
$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(
|
||||||
array('name' => 'level', 'value' => $level, 'type' => PDO::PARAM_STR),
|
array('name' => $field, 'value' => $value, 'type' => $pdo_type),
|
||||||
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)
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
foreach ($records as $record) {
|
||||||
|
$entries[] = $entry_class::fromArray($record);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?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
|
* Name of this plugin
|
||||||
@@ -9,10 +9,15 @@ class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements S
|
|||||||
const PLUGIN_NAME = "FlatFile";
|
const PLUGIN_NAME = "FlatFile";
|
||||||
|
|
||||||
protected $filename;
|
protected $filename;
|
||||||
|
protected $format;
|
||||||
protected $fp;
|
protected $fp;
|
||||||
|
|
||||||
protected function __construct($options) {
|
protected function __construct($instance, $filename, $format) {
|
||||||
$this->filename = Sihnon_Main::instance()->config()->get('logging.flatfile.filename');
|
parent::__construct($instance);
|
||||||
|
|
||||||
|
$this->filename = $filename;
|
||||||
|
$this->format = $format;
|
||||||
|
|
||||||
$this->fp = fopen($this->filename, 'a');
|
$this->fp = fopen($this->filename, 'a');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,25 +25,64 @@ class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements S
|
|||||||
fclose($this->fp);
|
fclose($this->fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function create($options) {
|
public static function create(SihnonFramework_Config $config, $instance) {
|
||||||
return new self($options);
|
$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
|
* Records a new entry in the storage backend used by this logging plugin
|
||||||
*
|
*
|
||||||
* @param string $level Severity of the log entry
|
* @param SihnonFramework_LogEntry $entry Log Entry object containing the information to be recorded
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
|
public function log(SihnonFramework_LogEntry $entry) {
|
||||||
$log_entry = implode(',', array($level, $ctime, $pid, $hostname, $progname, $line, $message)) . "\n";
|
$fields = $entry->fields();
|
||||||
fwrite($this->fp, $log_entry, strlen($log_entry));
|
$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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
15
source/lib/SihnonFramework/Log/PluginBase.class.php
Normal file
15
source/lib/SihnonFramework/Log/PluginBase.class.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
self::ensureScanned();
|
||||||
|
|
||||||
if (! self::isValidPlugin($plugin)) {
|
if (! self::isValidPlugin($plugin)) {
|
||||||
@@ -22,7 +22,7 @@ class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory {
|
|||||||
|
|
||||||
$classname = self::classname($plugin);
|
$classname = self::classname($plugin);
|
||||||
|
|
||||||
return call_user_func(array($classname, 'create'), $options);
|
return call_user_func(array($classname, 'create'), $config, $instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,89 +1,105 @@
|
|||||||
<?php
|
<?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 $level;
|
||||||
|
protected $category;
|
||||||
protected $ctime;
|
protected $ctime;
|
||||||
protected $pid;
|
protected $pid;
|
||||||
protected $hostname;
|
protected $file;
|
||||||
protected $progname;
|
|
||||||
protected $line;
|
protected $line;
|
||||||
protected $message;
|
protected $message;
|
||||||
|
|
||||||
protected function __construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message) {
|
public static function initialise() {
|
||||||
$this->id = $id;
|
self::$hostname = gethostname();
|
||||||
|
self::$progname = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function __construct($level, $category, $ctime, $pid, $file, $line, $message) {
|
||||||
$this->level = $level;
|
$this->level = $level;
|
||||||
|
$this->category = $category;
|
||||||
$this->ctime = $ctime;
|
$this->ctime = $ctime;
|
||||||
$this->pid = $pid;
|
$this->pid = $pid;
|
||||||
$this->hostname = $hostname;
|
$this->file = $file;
|
||||||
$this->progname = $progname;
|
|
||||||
$this->line = $line;
|
$this->line = $line;
|
||||||
$this->message = $message;
|
$this->message = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromDatabaseRow($row) {
|
public static function fromArray($row) {
|
||||||
$called_class = get_called_class();
|
return new self(
|
||||||
return new $called_class(
|
|
||||||
$row['id'],
|
|
||||||
$row['level'],
|
$row['level'],
|
||||||
|
$row['category'],
|
||||||
$row['ctime'],
|
$row['ctime'],
|
||||||
$row['pid'],
|
$row['pid'],
|
||||||
$row['hostname'],
|
$row['file'],
|
||||||
$row['progname'],
|
|
||||||
$row['line'],
|
$row['line'],
|
||||||
$row['message']
|
$row['message']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromId($id) {
|
public function fields() {
|
||||||
$called_class = get_called_class();
|
return array_keys(static::$types);
|
||||||
$database = Sihnon_Main::instance()->database();
|
}
|
||||||
return $called_class::fromDatabaseRow(
|
|
||||||
$database->selectOne('SELECT * FROM '.static::$table_name.' WHERE id=:id', array(
|
public function types() {
|
||||||
array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT)
|
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() {
|
public function level() {
|
||||||
return $this->level;
|
return $this->level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function category() {
|
||||||
|
return $this->category;
|
||||||
|
}
|
||||||
|
|
||||||
public function ctime() {
|
public function ctime() {
|
||||||
return $this->ctime;
|
return $this->ctime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function hostname() {
|
||||||
|
return static::$hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function progname() {
|
||||||
|
return static::$progname;
|
||||||
|
}
|
||||||
|
|
||||||
public function pid() {
|
public function pid() {
|
||||||
return $this->pid;
|
return $this->pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hostname() {
|
public function file() {
|
||||||
return $this->hostname;
|
return $this->file;
|
||||||
}
|
|
||||||
|
|
||||||
public function progname() {
|
|
||||||
return $this->progname;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function line() {
|
public function line() {
|
||||||
@@ -94,6 +110,39 @@ abstract class SihnonFramework_LogEntry {
|
|||||||
return $this->message;
|
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();
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -35,9 +35,7 @@ class SihnonFramework_Main {
|
|||||||
'filename' => Sihnon_ConfigFile)
|
'filename' => Sihnon_ConfigFile)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->log = new Sihnon_Log($this->config->get('logging.plugin'), array(
|
$this->log = new Sihnon_Log($this->config);
|
||||||
'database' => $this->database)
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->cache = new Sihnon_Cache($this->config);
|
$this->cache = new Sihnon_Cache($this->config);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user