Bug fixes to reduce logging-related crashes

* Provide a method for plugins to report failure to initialise
* Check plugins initialised properly before adding to list of logging
backends
* Check the logger is available before trying to log a message
(might cause bugs if errors are logged very early in initialisation)
This commit is contained in:
2011-08-20 11:34:29 +01:00
parent fc33112097
commit 2781b63b68
4 changed files with 47 additions and 7 deletions

View File

@@ -18,6 +18,9 @@ class SihnonFramework_Log_Plugin_FlatFile extends SihnonFramework_Log_PluginBase
$this->filename = $filename;
$this->format = $format;
// Verify the given log file can be written to
$this->verifyLogfile();
$this->fp = fopen($this->filename, 'a');
}
@@ -32,6 +35,26 @@ class SihnonFramework_Log_Plugin_FlatFile extends SihnonFramework_Log_PluginBase
return new self($instance, $filename, $format);
}
protected function verifyLogfile() {
// Check that the file exists, or can be created
$file_ok = false;
if (preg_match('#php://(stderr|stdout)#', $this->filename)) {
$file_ok = true;
} else if (file_exists($this->filename) && is_writable($this->filename)) {
$file_ok = true;
} else {
$directory = dirname($this->filename);
if (file_exists($directory) && is_writeable($directory)) {
$file_ok = true;
}
}
if (!$file_ok) {
throw new SihnonFramework_Exception_LogFileNotWriteable($this->filename);
}
}
/**
* Records a new entry in the storage backend used by this logging plugin
*