Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue #497: Added debug mode to Logger

* Added Logger::logDebug method which writes to log only when THINKUP_CFG['debug'] = true
* Added a couple debug-level messages to the Twitter crawler
* Docblocks and style tweaks
  • Loading branch information...
commit 2b43c270c08fe31b3c29983a12e7ec9d4ba84213 1 parent 812b970
@amygdala amygdala authored ginatrapani committed
View
35 tests/TestOfLogger.php
@@ -103,6 +103,8 @@ public function testFullVerbosity() {
public function testMessageTypes() {
$config = Config::getInstance();
+ // set debugging to true
+ $config->setValue('debug', true);
$logger_file = $config->getValue('log_location');
$logger = Logger::getInstance();
$this->assertIsA($logger, 'Logger');
@@ -112,12 +114,14 @@ public function testMessageTypes() {
$logger->logInfo("This is an info message", __METHOD__.','.__LINE__);
$logger->logError("This is an error message", __METHOD__.','.__LINE__);
$logger->logSuccess("This is a success message", __METHOD__.','.__LINE__);
+ $logger->logDebug("This is a debug message", __METHOD__.','.__LINE__);
$messages = null;
$messages = file($config->getValue('log_location'));
- $this->assertPattern('/This is an info message/', $messages[sizeof($messages) - 3]);
- $this->assertPattern('/This is an error message/', $messages[sizeof($messages) - 2]);
- $this->assertPattern('/This is a success message/', $messages[sizeof($messages) - 1]);
+ $this->assertPattern('/This is an info message/', $messages[sizeof($messages) - 4]);
+ $this->assertPattern('/This is an error message/', $messages[sizeof($messages) - 3]);
+ $this->assertPattern('/This is a success message/', $messages[sizeof($messages) - 2]);
+ $this->assertPattern('/This is a debug message/', $messages[sizeof($messages) - 1]);
$logger->setVerbosity(Logger::USER_MSGS);
$logger->logUserInfo("This is a user info message", __METHOD__.','.__LINE__);
@@ -126,6 +130,7 @@ public function testMessageTypes() {
$logger->logError("This is an info message", __METHOD__.','.__LINE__);
$logger->logUserSuccess("This is a user success message", __METHOD__.','.__LINE__);
$logger->logSuccess("This is an info message", __METHOD__.','.__LINE__);
+ $logger->logDebug("This is a debug message", __METHOD__.','.__LINE__);
$messages = null;
$messages = file($config->getValue('log_location'));
@@ -138,6 +143,29 @@ public function testMessageTypes() {
$logger->close();
}
+ public function testAllMsgsNoDebug() {
+ $config = Config::getInstance();
+ // set debugging to false
+ $config->setValue('debug', false);
+ $logger_file = $config->getValue('log_location');
+ $logger = Logger::getInstance();
+ $this->assertIsA($logger, 'Logger');
+
+ $logger = Logger::getInstance();
+ $logger->setVerbosity(Logger::ALL_MSGS);
+ $logger->logInfo("This is an info message", __METHOD__.','.__LINE__);
+ $logger->logError("This is an error message", __METHOD__.','.__LINE__);
+ $logger->logSuccess("This is a success message", __METHOD__.','.__LINE__);
+ // with debug set to false, this should not log anything
+ $logger->logDebug("This is a debug message", __METHOD__.','.__LINE__);
+
+ $messages = null;
+ $messages = file($config->getValue('log_location'));
+ $this->assertPattern('/This is an info message/', $messages[sizeof($messages) - 3]);
+ $this->assertPattern('/This is an error message/', $messages[sizeof($messages) - 2]);
+ $this->assertPattern('/This is a success message/', $messages[sizeof($messages) - 1]);
+ }
+
public function testHTMLOutput() {
$config = Config::getInstance();
$logger_file = $config->getValue('log_location');
@@ -163,6 +191,7 @@ public function testHTMLOutput() {
$logger->logError("This is an info message", __METHOD__.','.__LINE__);
$logger->logUserSuccess("This is a user success message", __METHOD__.','.__LINE__);
$logger->logSuccess("This is an info message", __METHOD__.','.__LINE__);
+ $logger->logDebug("This is a debugging message", __METHOD__.','.__LINE__);
$messages = null;
$messages = file($config->getValue('log_location'));
View
28 webapp/_lib/model/class.Logger.php
@@ -71,11 +71,19 @@ class Logger {
*/
const SUCCESS = 2;
/**
+ * @var int debugging messages -- log only
+ */
+ const DEBUG = 3;
+ /**
*
* @var int Log verbosity level (either self::ALL_MSGS or self::USER_MSGS)
*/
var $verbosity = 0;
/**
+ * @var bool whether to output debug-level log stmts
+ */
+ var $debug = false;
+ /**
* @var bool Whether or not output should be HTML
*/
var $html_output = false;
@@ -83,10 +91,11 @@ class Logger {
* Open the log file; Append to any prior file
* @param str $location
*/
- public function __construct($location) {
+ public function __construct($location, $debug = false) {
if ( $location != false ) {
$this->log = $this->openFile($location, 'a');
}
+ $this->debug = $debug;
}
/**
@@ -95,7 +104,8 @@ public function __construct($location) {
public static function getInstance() {
if (!isset(self::$instance)) {
$config = Config::getInstance();
- self::$instance = new Logger($config->getValue('log_location'));
+ $debug = $config->getValue('debug') ? true : false;
+ self::$instance = new Logger($config->getValue('log_location'), $debug);
}
return self::$instance;
}
@@ -139,6 +149,9 @@ private function logStatus($status_message, $classname, $verbosity = self::ALL_M
case self::SUCCESS:
$status_signature .= 'SUCCESS| ';
break;
+ case self::DEBUG:
+ $status_signature .= 'DEBUG | ';
+ break;
default:
$status_signature .= 'INFO | ';
}
@@ -189,6 +202,17 @@ public function logInfo($status_message, $classname) {
}
/**
+ * Write debug message to log if 'debug' config var is set to 'true'.
+ * @param str $status_message
+ * @param str $classname
+ */
+ public function logDebug($status_message, $classname) {
+ if ($this->debug) {
+ $this->logStatus($status_message, $classname, self::ALL_MSGS, self::DEBUG);
+ }
+ }
+
+ /**
* Write error message to log.
* @param str $status_message
* @param str $classname
View
11 webapp/plugins/twitter/model/class.TwitterCrawler.php
@@ -19,8 +19,7 @@
*
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
- */
-/**
+ *
* Twitter Crawler
*
* Retrieves tweets, replies, users, and following relationships from Twitter.com
@@ -1134,8 +1133,8 @@ private function maintFavsFetch ($starting_fav_id, $fcount, $mpage, $older_favs_
// fav was already stored, so take no action. This could happen both because some
// of the favs on the given page were processed last time, or because a separate process,
// such as a UserStream process, is also watching for and storing favs.
- // $status_message = "have already stored fav ". $tweet['post_id'];
- // $this->logger->logInfo($status_message, __METHOD__.','.__LINE__);
+ $status_message = "have already stored fav ". $tweet['post_id'];
+ $this->logger->logDebug($status_message, __METHOD__.','.__LINE__);
}
// keep track of the highest fav id we've encountered
@@ -1241,7 +1240,7 @@ private function archivingFavsFetch ($fcount, $last_fav_id, $last_page_fetched_f
$this->logger->logInfo("added favorite: ". $tweet['post_id'], __METHOD__.','.__LINE__);
} else {
$status_message = "have already stored favorite: ". $tweet['post_id'];
- $this->logger->logInfo($status_message, __METHOD__.','.__LINE__);
+ $this->logger->logDebug($status_message, __METHOD__.','.__LINE__);
}
// $this->logger->logInfo("current last fav id is: $last_fav_id", __METHOD__.','.__LINE__);
@@ -1370,7 +1369,7 @@ public function cleanUpMissedFavsUnFavs() {
$fcount++;
} else {
$status_message = "have already stored fav ". $fav['post_id'];
- $this->logger->logInfo($status_message, __METHOD__.','.__LINE__);
+ $this->logger->logDebug($status_message, __METHOD__.','.__LINE__);
}
}
// now for each favorited tweet in the database within the fetched range, check whether it's still
Please sign in to comment.
Something went wrong with that request. Please try again.