Browse files

abstract the error logging call; change private var naming pattern to…

… match other files
  • Loading branch information...
1 parent 8c5ceba commit ba92c929219b0dec9b60bd9873754a18008fd4f2 @zzhong zzhong committed Jul 24, 2011
Showing with 25 additions and 8 deletions.
  1. +14 −8 AB2/Logger/MultiLogger.php
  2. +11 −0 AB2/SystemLogger.php
View
22 AB2/Logger/MultiLogger.php
@@ -2,27 +2,33 @@
require_once "AB2/Logger.php";
/**
- * Logger that passes calls through to multiple loggers.
+ * Logger that passes calls through to multiple loggers. This is a best-effort
+ * implementation: if a logger throws an exception during the log() call,
+ * the multilogger will log the error and move on to the next logger, if any.
*
*/
class AB2_Logger_MultiLogger implements AB2_Logger {
- private $loggers;
-
- public function __construct($loggers) {
+ private $_loggers;
+ private $_sysLogger;
+
+ public function __construct($loggers, $sysLogger) {
if (!is_array($loggers)) {
throw new InvalidArgumentException('we need an array of loggers here');
}
- $this->loggers = $loggers;
+ if (is_null($sysLogger)) {
+ throw new InvalidArgumentException('we need a systen logger to report possible errors.');
+ }
+ $this->_loggers = $loggers;
+ $this->_sysLogger = $sysLogger;
}
public function log($testKey, $variantKey, $subjectKey) {
- foreach ($this->loggers as $logger) {
+ foreach ($this->_loggers as $logger) {
try {
$logger->log($testKey, $variantKey, $subjectKey);
} catch (Exception $err) {
- // if one logger fails, log the error and go on to the next one
- Logger::log_error("AB Logger failed: $err", 'AB');
+ $this->_sysLogger->error("AB2_Logger failed: $err");
}
}
}
View
11 AB2/SystemLogger.php
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * This is for logging errors, etc. This is how the AB2 framework integrates
+ * with the error handling facility of your app.
+ */
+interface AB2_SystemLogger {
+ public function info($msg);
+
+ public function error($msg);
+}

0 comments on commit ba92c92

Please sign in to comment.