diff --git a/Alpha/Controller/FeedController.php b/Alpha/Controller/FeedController.php index 4901fc7e..02ac11a6 100644 --- a/Alpha/Controller/FeedController.php +++ b/Alpha/Controller/FeedController.php @@ -209,7 +209,8 @@ public function doGET($request) $response->setBody($feed->render()); // log the request for this news feed - $feedLog = new LogProviderFile($config->get('app.file.store.dir').'logs/feeds.log'); + $feedLog = new LogProviderFile(); + $feedLog->setPath($config->get('app.file.store.dir').'logs/feeds.log'); $feedLog->writeLine(array($this->ActiveRecordType, $this->type, date('Y-m-d H:i:s'), $request->getUserAgent(), $request->getIP())); } catch (IllegalArguementException $e) { self::$logger->error($e->getMessage()); diff --git a/Alpha/Controller/LogController.php b/Alpha/Controller/LogController.php index 6be5271f..b415df71 100644 --- a/Alpha/Controller/LogController.php +++ b/Alpha/Controller/LogController.php @@ -120,7 +120,9 @@ public function doGET($request) $body .= View::displayPageHead($this); - $log = new LogProviderFile($this->logPath); + $log = new LogProviderFile(); + $log->setPath($this->logPath); + if (preg_match('/alpha.*/', basename($this->logPath))) { $body .= $log->renderLog(array('Date/time', 'Level', 'Class', 'Message', 'Client', 'IP', 'Server hostname')); } diff --git a/Alpha/Controller/SearchController.php b/Alpha/Controller/SearchController.php index c7500509..db82a2ff 100644 --- a/Alpha/Controller/SearchController.php +++ b/Alpha/Controller/SearchController.php @@ -148,7 +148,8 @@ public function doGET($request) $body .= View::displayPageHead($this); // log the user's search query in a log file - $log = new LogProviderFile($config->get('app.file.store.dir').'logs/search.log'); + $log = new LogProviderFile(); + $log->setPath($config->get('app.file.store.dir').'logs/search.log'); $log->writeLine(array($params['query'], date('Y-m-d H:i:s'), $request->getUserAgent(), $request->getIP())); $KPI->logStep('log search query'); diff --git a/Alpha/Util/Logging/KPI.php b/Alpha/Util/Logging/KPI.php index c70e4696..c462ea18 100644 --- a/Alpha/Util/Logging/KPI.php +++ b/Alpha/Util/Logging/KPI.php @@ -169,7 +169,8 @@ public function log() $this->duration = $this->endTime - $this->startTime; - $logfile = new LogProviderFile($config->get('app.file.store.dir').'logs/kpi-'.$this->name->getValue().'.csv'); + $logfile = new LogProviderFile(); + $logfile->setPath($config->get('app.file.store.dir').'logs/kpi-'.$this->name->getValue().'.csv'); $logfile->setMaxSize($config->get('app.log.file.max.size')); @@ -190,7 +191,8 @@ public function logStep($stepName) $this->duration = $this->endTime - $this->startTime; - $logfile = new LogProviderFile($config->get('app.file.store.dir').'logs/kpi-'.$this->name->getValue().'.csv'); + $logfile = new LogProviderFile(); + $logfile->setPath($config->get('app.file.store.dir').'logs/kpi-'.$this->name->getValue().'.csv'); $logfile->setMaxSize($config->get('app.log.file.max.size')); diff --git a/Alpha/Util/Logging/LogProviderFactory.php b/Alpha/Util/Logging/LogProviderFactory.php new file mode 100644 index 00000000..6cc3a8bb --- /dev/null +++ b/Alpha/Util/Logging/LogProviderFactory.php @@ -0,0 +1,84 @@ + + * @license http://www.opensource.org/licenses/bsd-license.php The BSD License + * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework). + * All rights reserved. + * + *
+ * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the + * following conditions are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other + * materials provided with the distribution. + * * Neither the name of the Alpha Framework nor the names + * of its contributors may be used to endorse or promote + * products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *+ */ +class LogProviderFactory +{ + /** + * A static method that attempts to return a LogProviderInterface instance + * based on the name of the provider class supplied. + * + * @param $providerName The class name of the provider class (fully qualified). + * + * @throws Alpha\Exception\IllegalArguementException + * + * @return Alpha\Util\Logging\LogProviderInterface + * + * @since 2.0 + */ + public static function getInstance($providerName) + { + $config = ConfigProvider::getInstance(); + + if (class_exists($providerName)) { + $instance = new $providerName(); + + if (!$instance instanceof LogProviderInterface) { + throw new IllegalArguementException('The class ['.$providerName.'] does not implement the expected LogProviderInterface intwerface!'); + } + + $instance->setMaxSize($config->get('app.log.file.max.size')); + + return $instance; + } else { + throw new IllegalArguementException('The class ['.$providerName.'] is not defined anywhere!'); + } + } +} diff --git a/Alpha/Util/Logging/LogProviderFile.php b/Alpha/Util/Logging/LogProviderFile.php index 8f698d99..a18c0af9 100644 --- a/Alpha/Util/Logging/LogProviderFile.php +++ b/Alpha/Util/Logging/LogProviderFile.php @@ -46,7 +46,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -class LogProviderFile implements LogProvider +class LogProviderFile implements LogProviderInterface { /** * The log file path. @@ -68,13 +68,13 @@ class LogProviderFile implements LogProvider private $MaxSize = 5; /** - * The constructor. + * Set the file path. * * @param string $path * - * @since 1.0 + * @since 2.0 */ - public function __construct($path) + public function setPath($path) { $this->path = $path; } @@ -98,21 +98,8 @@ public function writeLine($line) { $config = ConfigProvider::getInstance(); - try { - $fp = fopen($this->path, 'a+'); - fputcsv($fp, $line, ',', '"', '\\'); - - if ($this->checkFileSize() >= $this->MaxSize) { - $this->backupFile(); - } - } catch (\Exception $e) { + if ($this->path != '') { try { - $logsDir = $config->get('app.file.store.dir').'logs'; - - if (!file_exists($logsDir)) { - mkdir($logsDir, 0766); - } - $fp = fopen($this->path, 'a+'); fputcsv($fp, $line, ',', '"', '\\'); @@ -120,9 +107,24 @@ public function writeLine($line) $this->backupFile(); } } catch (\Exception $e) { - // TODO log to PHP system log file instead - echo 'Unable to write to the log file ['.$this->path.'], error ['.$e->getMessage().']'; - exit; + try { + $logsDir = $config->get('app.file.store.dir').'logs'; + + if (!file_exists($logsDir)) { + mkdir($logsDir, 0766); + } + + $fp = fopen($this->path, 'a+'); + fputcsv($fp, $line, ',', '"', '\\'); + + if ($this->checkFileSize() >= $this->MaxSize) { + $this->backupFile(); + } + } catch (\Exception $e) { + // TODO log to PHP system log file instead + echo 'Unable to write to the log file ['.$this->path.'], error ['.$e->getMessage().']'; + exit; + } } } } diff --git a/Alpha/Util/Logging/LogProvider.php b/Alpha/Util/Logging/LogProviderInterface.php similarity index 98% rename from Alpha/Util/Logging/LogProvider.php rename to Alpha/Util/Logging/LogProviderInterface.php index 7c4131fe..851bce02 100644 --- a/Alpha/Util/Logging/LogProvider.php +++ b/Alpha/Util/Logging/LogProviderInterface.php @@ -44,7 +44,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -interface LogProvider +interface LogProviderInterface { /** * Writes a line of data to the log. diff --git a/Alpha/Util/Logging/Logger.php b/Alpha/Util/Logging/Logger.php index cb2ea176..1a5e1540 100644 --- a/Alpha/Util/Logging/Logger.php +++ b/Alpha/Util/Logging/Logger.php @@ -110,9 +110,8 @@ public function __construct($classname) $this->classname = $classname; $this->level = $config->get('app.log.trace.level'); $this->debugClasses = explode(',', $config->get('app.log.trace.debug.classes')); - $this->logProvider = new LogProviderFile($config->get('app.file.store.dir').'logs/'.$config->get('app.log.file')); - // TODO: move the setMaxSize call to the new log factory - $this->logProvider->setMaxSize($config->get('app.log.file.max.size')); + $this->logProvider = LogProviderFactory::getInstance('Alpha\Util\Logging\LogProviderFile'); + $this->logProvider->setPath($config->get('app.file.store.dir').'logs/'.$config->get('app.log.file')); $this->request = new Request(array('method' => 'GET')); // hard-coding to GET here is fine as we don't log HTTP method (yet). }