Skip to content

Commit

Permalink
#11 - added the LogProviderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
alphadevx committed Sep 17, 2015
1 parent c9b1da6 commit 91c7d87
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 30 deletions.
3 changes: 2 additions & 1 deletion Alpha/Controller/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion Alpha/Controller/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
3 changes: 2 additions & 1 deletion Alpha/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 4 additions & 2 deletions Alpha/Util/Logging/KPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand All @@ -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'));

Expand Down
84 changes: 84 additions & 0 deletions Alpha/Util/Logging/LogProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Alpha\Util\Logging;

use Alpha\Exception\IllegalArguementException;
use Alpha\Util\Config\ConfigProvider;
use Alpha\Util\Logging\Logger;

/**
* A factory for creating log provider implementations that implement the
* LogProviderInterface interface.
*
* @since 2.0
*
* @author John Collins <dev@alphaframework.org>
* @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.
*
* <pre>
* 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.
* </pre>
*/
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!');
}
}
}
44 changes: 23 additions & 21 deletions Alpha/Util/Logging/LogProviderFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* </pre>
*/
class LogProviderFile implements LogProvider
class LogProviderFile implements LogProviderInterface
{
/**
* The log file path.
Expand All @@ -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;
}
Expand All @@ -98,31 +98,33 @@ 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, ',', '"', '\\');

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;
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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* </pre>
*/
interface LogProvider
interface LogProviderInterface
{
/**
* Writes a line of data to the log.
Expand Down
5 changes: 2 additions & 3 deletions Alpha/Util/Logging/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
}
Expand Down

0 comments on commit 91c7d87

Please sign in to comment.