Fix log handling of hostname and progname

Log classes previously assumed all logs to be sourced from the local
machine with no program name set. This change adds support to read/set
the program name, and ensures the hostname/program name are properly
tracked into and out of the database.
This commit is contained in:
2011-08-06 14:49:11 +01:00
parent f7b5947863
commit 87b0789137

View File

@@ -2,8 +2,8 @@
class SihnonFramework_LogEntry { class SihnonFramework_LogEntry {
protected static $hostname; protected static $localHostname;
protected static $progname; protected static $localProgname;
protected static $types = array( protected static $types = array(
'level' => 'string', 'level' => 'string',
@@ -20,20 +20,24 @@ class SihnonFramework_LogEntry {
protected $level; protected $level;
protected $category; protected $category;
protected $ctime; protected $ctime;
protected $hostname;
protected $progname;
protected $pid; protected $pid;
protected $file; protected $file;
protected $line; protected $line;
protected $message; protected $message;
public static function initialise() { public static function initialise() {
self::$hostname = gethostname(); static::$localHostname = gethostname();
self::$progname = ''; static::$localProgname = '';
} }
protected function __construct($level, $category, $ctime, $pid, $file, $line, $message) { protected function __construct($level, $category, $ctime, $hostname, $progname, $pid, $file, $line, $message) {
$this->level = $level; $this->level = $level;
$this->category = $category; $this->category = $category;
$this->ctime = $ctime; $this->ctime = $ctime;
$this->hostname = $hostname;
$this->progname = $progname;
$this->pid = $pid; $this->pid = $pid;
$this->file = $file; $this->file = $file;
$this->line = $line; $this->line = $line;
@@ -45,6 +49,8 @@ class SihnonFramework_LogEntry {
$row['level'], $row['level'],
$row['category'], $row['category'],
$row['ctime'], $row['ctime'],
$row['hostname'],
$row['progname'],
$row['pid'], $row['pid'],
$row['file'], $row['file'],
$row['line'], $row['line'],
@@ -52,6 +58,14 @@ class SihnonFramework_LogEntry {
); );
} }
public static function localProgname() {
return static::$localProgname;
}
public static function setLocalProgname($progname) {
static::$localProgname = $progname;
}
public function fields() { public function fields() {
return array_keys(static::$types); return array_keys(static::$types);
} }
@@ -65,8 +79,8 @@ class SihnonFramework_LogEntry {
$this->level, $this->level,
$this->category, $this->category,
$this->ctime, $this->ctime,
static::$hostname, $this->hostname,
static::$progname, $this->progname,
$this->pid, $this->pid,
$this->file, $this->file,
$this->line, $this->line,
@@ -112,13 +126,13 @@ class SihnonFramework_LogEntry {
protected static function log($logger, $severity, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) { protected static function log($logger, $severity, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
$backtrace = debug_backtrace(false); $backtrace = debug_backtrace(false);
$entry = new self($severity, $category, time(), getmypid(), $backtrace[1]['file'], $backtrace[1]['line'], $message); $entry = new static($severity, $category, time(), static::$localHostname, static::$localProgname, getmypid(), $backtrace[1]['file'], $backtrace[1]['line'], $message);
$logger->log($entry); $logger->log($entry);
} }
public static function logInternal($logger, $severity, $file, $line, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) { public static function logInternal($logger, $severity, $file, $line, $message, $category = SihnonFramework_Log::CATEGORY_DEFAULT) {
$entry = new self($severity, $category, time(), getmypid(), $file, $line, $message); $entry = new static($severity, $category, time(), static::$localHostname, static::$localProgname, getmypid(), $file, $line, $message);
$logger->log($entry); $logger->log($entry);
} }