From 87b0789137eca370acf758bb7562c1e6f68f4791 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 6 Aug 2011 14:49:11 +0100 Subject: [PATCH] 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. --- source/lib/SihnonFramework/LogEntry.class.php | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/source/lib/SihnonFramework/LogEntry.class.php b/source/lib/SihnonFramework/LogEntry.class.php index f93e25f..3f37e0f 100644 --- a/source/lib/SihnonFramework/LogEntry.class.php +++ b/source/lib/SihnonFramework/LogEntry.class.php @@ -2,8 +2,8 @@ class SihnonFramework_LogEntry { - protected static $hostname; - protected static $progname; + protected static $localHostname; + protected static $localProgname; protected static $types = array( 'level' => 'string', @@ -20,20 +20,24 @@ class SihnonFramework_LogEntry { protected $level; protected $category; protected $ctime; + protected $hostname; + protected $progname; protected $pid; protected $file; protected $line; protected $message; public static function initialise() { - self::$hostname = gethostname(); - self::$progname = ''; + static::$localHostname = gethostname(); + 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->category = $category; $this->ctime = $ctime; + $this->hostname = $hostname; + $this->progname = $progname; $this->pid = $pid; $this->file = $file; $this->line = $line; @@ -45,6 +49,8 @@ class SihnonFramework_LogEntry { $row['level'], $row['category'], $row['ctime'], + $row['hostname'], + $row['progname'], $row['pid'], $row['file'], $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() { return array_keys(static::$types); } @@ -65,8 +79,8 @@ class SihnonFramework_LogEntry { $this->level, $this->category, $this->ctime, - static::$hostname, - static::$progname, + $this->hostname, + $this->progname, $this->pid, $this->file, $this->line, @@ -112,13 +126,13 @@ class SihnonFramework_LogEntry { 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); + $entry = new static($severity, $category, time(), static::$localHostname, static::$localProgname, getmypid(), $backtrace[1]['file'], $backtrace[1]['line'], $message); $logger->log($entry); } 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); }