commit c5a6fb4017cbd222863cb7048d5649e259b468fa
parent 5fdec55078b4157de54be552405fe7bb5d758eff
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 14 Apr 2012 01:46:09 +0200
Added basic logging mechanism
Diffstat:
3 files changed, 238 insertions(+), 2 deletions(-)
diff --git a/config.php.template b/config.php.template
@@ -136,4 +136,21 @@ Configuration::setOption("partkeepr.category.path_separator", " ➤ ");
*
* As soon as you set HTTP auth, you can no longer login and logout in PartKeepr, as this is handled by your web server.
*/
-Configuration::setOption("partkeepr.auth.http", false);-
\ No newline at end of file
+Configuration::setOption("partkeepr.auth.http", false);
+
+/**
+ * Set to true if logging is wanted.
+ *
+ * If you leave as-is or set to false, no log will be written at all. If you set this to true, a log will be written
+ * to the file specified by partkeepr.logger.outputfile. You need to make sure that the path to the file exists and
+ * that the file is writable.
+ */
+Configuration::setOption("partkeepr.logger.enable", false);
+
+/**
+ * Specifies the log file for the logger. If you enable logging and specify no outputfile, the default will be
+ * /tmp/partkeepr.log.
+ *
+ * You need to make sure that the path to the file exists and that the file is writable.
+ */
+Configuration::setOption("partkeepr.logger.outputfile", "/tmp/partkeepr.log");+
\ No newline at end of file
diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Logger/Logger.php b/src/backend/de/RaumZeitLabor/PartKeepr/Logger/Logger.php
@@ -0,0 +1,121 @@
+<?php
+namespace de\RaumZeitLabor\PartKeepr\Logger;
+
+/**
+ * This class implements a simple logging mechanism.
+ *
+ * Might be replaced with some more advanced logging framework, but it serves its purpose for now.
+ */
+use de\RaumZeitLabor\PartKeepr\Util\Configuration;
+
+class Logger {
+ /**
+ * Loglevel "debug"
+ *
+ * You want to use this loglevel for debugging messages.
+ * @var string
+ */
+ const LOGLEVEL_DEBUG = "debug";
+
+ /**
+ * Loglevel "info"
+ *
+ * You want to use this loglevel for informational messages where the program flow can continue normally and no
+ * impacts occur.
+ *
+ * @var string
+ */
+ const LOGLEVEL_INFO = "info";
+
+ /**
+ * Loglevel "warning"
+ *
+ * You want to use this loglevel for messages where program flow can continue normally, but a minor
+ * impact can occur.
+ *
+ * @var string
+ */
+ const LOGLEVEL_WARNING = "warning";
+
+ /**
+ * Loglevel "error"
+ *
+ * You want to use this loglevel for messages where program flow could be interrupted and major impacts occur.
+ *
+ * @var string
+ */
+ const LOGLEVEL_ERROR = "error";
+
+ /**
+ * Loglevel "critical"
+ *
+ * You want to use this loglevel for messages where program flow could be interrupted and major impacts occur.
+ *
+ * @var string
+ */
+ const LOGLEVEL_CRITICAL = "critical";
+
+ /**
+ * Logs a message.
+ *
+ * @param string $message The message to log
+ * @param string $severity One of the Logger::LOGLEVEL_* constants
+ */
+ public static function log ($message, $severity = Logger::LOGLEVEL_INFO) {
+ if (Configuration::getOption("partkeepr.logger.enable", false) === false) {
+ // No logging wanted
+ return;
+ }
+
+ $logFormat = "[%-19s] [%-8s] %s";
+
+ $outputFile = Configuration::getOption("partkeepr.logger.outputfile", sys_get_temp_dir() . "/partkeepr.log");
+ $fp = fopen($outputFile, "a");
+
+ $date = date("Y-m-d H:i:s");
+
+ fputs($fp, sprintf($logFormat, $date, $severity, $message)."\n");
+
+ fclose($fp);
+ }
+
+ /**
+ * Logs a message with the Logger::LOGLEVEL_DEBUG severity.
+ * @param string $message
+ */
+ public static function logDebug ($message) {
+ self::log($message, Logger::LOGLEVEL_DEBUG);
+ }
+
+ /**
+ * Logs a message with the Logger::LOGLEVEL_INFO severity.
+ * @param string $message
+ */
+ public static function logInfo ($message) {
+ self::log($message, Logger::LOGLEVEL_INFO);
+ }
+
+ /**
+ * Logs a message with the Logger::LOGLEVEL_WARNING severity.
+ * @param string $message
+ */
+ public static function logWarning ($message) {
+ self::log($message, Logger::LOGLEVEL_WARNING);
+ }
+
+ /**
+ * Logs a message with the Logger::LOGLEVEL_ERROR severity.
+ * @param string $message
+ */
+ public static function logError ($message) {
+ self::log($message, Logger::LOGLEVEL_ERROR);
+ }
+
+ /**
+ * Logs a message with the Logger::LOGLEVEL_CRITICAL severity.
+ * @param string $message
+ */
+ public static function logCritical ($message) {
+ self::log($message, Logger::LOGLEVEL_CRITICAL);
+ }
+}+
\ No newline at end of file
diff --git a/tests/Logger/LoggerTest.php b/tests/Logger/LoggerTest.php
@@ -0,0 +1,96 @@
+<?php
+namespace de\RaumZeitLabor\PartKeepr\Tests\Logger;
+
+use de\RaumZeitLabor\PartKeepr\Logger\Logger,
+ de\RaumZeitLabor\PartKeepr\Util\Configuration;
+
+class LoggerTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * Specifies the temporary logging file to be used for our tests
+ * @var string
+ */
+ private $tempName;
+
+ /**
+ * @inheritdoc
+ */
+ public function setUp () {
+ $this->tempName = tempnam(sys_get_temp_dir(), "pk");
+
+ Configuration::setOption("partkeepr.logger.outputfile", $this->tempName);
+ Configuration::setOption("partkeepr.logger.enable", true);
+ }
+
+ /**
+ * Tests the logging
+ */
+ public function testLogging () {
+ $this->outputLog(Logger::LOGLEVEL_CRITICAL, "test");
+ $this->outputLog(Logger::LOGLEVEL_ERROR, "test");
+ $this->outputLog(Logger::LOGLEVEL_WARNING, "test");
+ $this->outputLog(Logger::LOGLEVEL_INFO, "test");
+ $this->outputLog(Logger::LOGLEVEL_DEBUG, "test");
+ }
+
+ public function testLoggingWithHelperMethods () {
+ $this->outputLog(Logger::LOGLEVEL_CRITICAL, "test", true);
+ $this->outputLog(Logger::LOGLEVEL_ERROR, "test", true);
+ $this->outputLog(Logger::LOGLEVEL_WARNING, "test", true);
+ $this->outputLog(Logger::LOGLEVEL_INFO, "test", true);
+ $this->outputLog(Logger::LOGLEVEL_DEBUG, "test", true);
+ }
+
+ /**
+ * Tests that no output file is written if logging is disabled
+ */
+ public function testDisabledLogging () {
+ unlink($this->tempName);
+ Configuration::setOption("partkeepr.logger.enable", false);
+
+ Logger::log("test", Logger::LOGLEVEL_CRITICAL);
+
+
+ $this->assertEquals(false, file_exists($this->tempName));
+ }
+
+ /**
+ * Writes a specific string with a specific severity to the log file and checks if the file was written correctly.
+ * @param string $severity The severity
+ * @param string $string The string
+ * @param $useHelperMethods boolean True to use the helper methods
+ */
+ public function outputLog ($severity, $string, $useHelperMethods = false) {
+ unlink($this->tempName);
+
+ if ($useHelperMethods == true) {
+ $helperMethod = "log".ucfirst($severity);
+ Logger::$helperMethod($string);
+ } else {
+ Logger::log($string, $severity);
+ }
+
+
+ $data = file_get_contents($this->tempName);
+ $this->matchLogLine($data, $severity, $string);
+ }
+
+ /**
+ * Splits the given line into tokens and tries to find out if the log message is correct.
+ * @param string $line The line to test
+ * @param string $severity The given severity
+ * @param string $text The logged text
+ */
+ public function matchLogLine ($line, $severity, $text) {
+ // Match the date section
+ $tokens = explode(" ", $line);
+
+ $this->assertEquals(11, strlen($tokens[0]));
+ $this->assertEquals(9, strlen($tokens[1]));
+
+ $severityMatch = "[" . str_pad($severity, 8) . "]";
+ $this->assertNotEquals(false, strpos($line, $severityMatch));
+
+ $this->assertNotEquals(false, strpos($line, $text));
+ }
+}+
\ No newline at end of file